SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru
    Join Date
    Mar 2006
    Posts
    701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    function problem

    Code:
    function findsubcat($database_cdl_cms, $cdl_cms,$cat)
      {
       mysql_select_db($database_cdl_cms, $cdl_cms);
     $query="select
     coalesce(cat_id,'-1') id from cats where id=$cat;";
      $variety = mysql_query($query, $cdl_cms) 
    or die(mysql_error());
    $row_variety = mysql_fetch_assoc($variety);
    
    $pr=$row_variety['cat_id'];
    if ($pr=='-1')
    {   $a=$cat;
    return   $a;
    }
    else  findsubcat($database_cdl_cms, $cdl_cms,$pr);
      }
    I have this code, this function at my page.
    My problem is when the function is running for the second time (the $pr is not -1 ) and then the $pr is -1 does not retutn anything, my code at page is
    Code:
    $subcat
    = findsubcat($database_cdl_cms, $cdl_cms,$_GET['cat']);

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Add return to the function call inside the function. Because the result of the second function call will be returned to the first function, not to the main script.
    PHP Code:
    function findsubcat($database_cdl_cms$cdl_cms$cat) {
      
    mysql_select_db($database_cdl_cms$cdl_cms);
      
    $query 
        
    "select
           coalesce(cat_id, '-1') id 
         from cats 
         where id=
    $cat";
      
    $variety mysql_query($query$cdl_cms) or die(mysql_error());
      
    $row_variety mysql_fetch_assoc($variety);

      
    $pr $row_variety['cat_id'];
      if (
    $pr == '-1') {
        
    $a $cat;
        return 
    $a;
      }
      else  return 
    findsubcat($database_cdl_cms$cdl_cms$pr);



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •