Tutorial :PHP won't return data from function


I'm a bit of a PHP newbie so go easy on me :)

I am trying to return a category name for a searched entry, after determining whether the category is the lowest child.

My sql statement returns multiple duplicate entries (for each category and sub category that it is in - i.e. if an entry is in Cars > Parts > Engine > Crankshafts, it would appear 4 times, any of you who use Expression Engine should know that it does this :) )

This is the SQL statement which returns the results:

SELECT wd.field_id_5, wd.field_id_7, wd.field_id_14, wd.field_id_15, wd.field_id_18, wd.field_id_20, wt.entry_id, wt.title, wt.url_title, cp.cat_id, c.cat_name, c.cat_url_title, c.parent_id FROM exp_weblog_data AS wd LEFT JOIN exp_weblog_titles AS wt ON wd.entry_id = wt.entry_id LEFT JOIN exp_category_posts AS cp ON wt.entry_id = cp.entry_id LEFT JOIN exp_categories as c ON cp.cat_id = c.cat_id WHERE wt.title LIKE '%$term%'  

The following code is to try to derive the deepest child category, so as not to display the entry 4 times.

foreach ($query->result as $row)  {      $entry_id = $row['entry_id'];    $title = $row['title'];    $url_title = $row['url_title'];    $cat_id = $row['cat_id'];    $cat_name = $row['cat_name'];    $category_url = $row['cat_url_title'];    $parent = $row['parent_id'];    $image = $row['field_id_7'];    $image_path = "example.com" . $image;    $location = $row['field_id_14'];    $country = $row['field_id_20'];    $currency = $row['field_id_18'];    $price = $row['field_id_5'];    $postage = $row['field_id_15'];      // if the  entry id doesnt exist in array already    // add into array (all details)    if ( ! array_key_exists($entry_id, $entries) )    {      $entries[$entry_id] = array($title, $url_title, $cat_id, $cat_name, $category_url, $parent, $image, $image_path, $location, $country, $currency, $price, $postage);    }  }    $count = 0;    // for each entry in array, run function to find lowest child and display  function determine_child($entry_id, $cat_id, $cat_name)  {    global $DB, $cat_name;      $sql = "SELECT c.cat_id, c.cat_name FROM exp_categories AS c INNER JOIN exp_category_posts AS cp ON c.cat_id = cp.cat_id WHERE c.parent_id = '{$cat_id}' AND cp.entry_id = '{$entry_id}'";    $query = $DB->query($sql);      if ( $query->num_rows > 0 )    {        foreach($query->result as $cat_row)      {        $entry_id = $entry_id;        $cat_id = $cat_row['cat_id'];        $cat_name = $cat_row['cat_name'];          determine_child($entry_id, $cat_id, $cat_name);      }    }    else    {      return $cat_name;    }  } // END FUNCTION      foreach ( $entries as $key => $val)  {    $entry_id = $key;    $cat_id = $val[2];    $cat_name = $val[3];    $cat_name = determine_child($entry_id, $cat_id, $cat_name);      echo $val[0] . " - " . $cat_name . "<br />";  }  

The above code echos out the correct category, but wont return it to the $cat_name variable in the foreach loop at the bottom.

I hope i've been clear enough, and thanks for any help!


There's a few things that are a little confusing here.

a) You shouldn't be using global $cat_name in your function, since you are passing it as a parameter that changes several times, not having one global variable.

b) Whenever you call determine_child with a category that does have children, your method doesn't return anything.

c) What exactly do you want to do in the case that there are multiple lowest level categories? For example, your determine_child method loops through all children - should it only take the first lowest level category it finds, or should it print out them all?

If the answer to c is that you want it to just print out one category, then you can simply add a return statement in your if condition: return determine_child($entry_id, $cat_id, $cat_name); - though it would be better to get rid of the for loop and just use the first query result instead.

If the answer to c is something different, that makes it quite a different problem - so would need to know exactly what you would like the output to be.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »