1

I have a function that I loop to to build an url. what I need is to get the result in a single string. This is the code.

// Create URL for accesories
function create_url($id) {
  global $db_categories;

  // Select category sublevels
  $sql_cat="SELECT parent_id, seo_id FROM $db_categories WHERE category_id = '".$id."'";
  $catres = mysql_query("$sql_cat") or die (mysql_error());

  while($selected_cat = mysql_fetch_array($catres)) {
    $cat_seo = $selected_cat['seo_id']. "/";
    $output .= $cat_seo;
    create_url($selected_cat['parent_id']);
  }      
  return $output;
}

// Call function
$result_cat = create_url($cat["category_id"]);
echo $result_cat;

This work fine if I use echo and will output (I only use $depth to track the results)

category1/
category2/
category3/
etc...

The problem is that I don't know how to return the result into a single string instead of echo it. When I use return it only outputs the first result. like this.

category1/

I want the return to output.

category1/category2/category3/

I can't for my life find a solution for this.

Thank's

perqedelius
  • 105
  • 1
  • 1
  • 10
  • 3
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://stackoverflow.com/a/14110189/1723893). – NullPoiиteя Jan 13 '13 at 11:18
  • I would have written a solution but I don't exactly know how you are using $depth. Cause you aren't. I didn't know if recursion was doing things or just the while loop. – Sanchit Jan 13 '13 at 11:25

0 Answers0