4 Replies - 699 Views - Last Post: 19 December 2013 - 11:35 AM Rate Topic: -----

#1 typhlosion  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 109
  • Joined: 09-December 13

recursive function how to output a unordered list

Posted 16 December 2013 - 04:18 AM

hi, how do I put the category names into a "ul" structure??

here is my recursive function:

function GET_SEED (){
  $sql = "SELECT * FROM categories ORDER BY categories_id";
  $result = mysql_query($sql);
  $row = mysql_fetch_array($result);
  return($row['categories_id']);
}

function GROW_TREE($cat_id) {
  global $count_x, $count_y, $tree_array;
  $sql = "SELECT * FROM categories WHERE parent_id ='" . $cat_id . "'";
  $result = mysql_query($sql) or die(mysql_error());
  while ($row = mysql_fetch_array($result)) {
	$sql2 = "SELECT * FROM categories_description WHERE parent_id ='" . $row['categories_id'] . "'";
	$result2 = mysql_query($sql2);
	$row2 = mysql_fetch_array($result2);
    if ($row['parent_id'] > 0) {
	  echo $row2['categories_name'];
	  $count_x++;
	} else {
	  echo $row2['categories_name'];
	  $count_y++;
	}
	$tree_array[$count_x][$count_y] = $cat_id;
	GROW_TREE($row['categories_id']);
  }
  $count_y = 0;
  return;
}


so that it will echo something like:
<ul>
  <li>sub level</li>
  <li>sub level</li>
  <li>sub level
    <ul>
      <li>sub sub sub level</li>
      <li>sub sub sub level</li>
      <li>sub sub sub level</li>
    </ul>
  </li>
</ul>


:helpsmilie:

This post has been edited by Dormilich: 16 December 2013 - 04:45 AM


Is This A Good Question/Topic? 0
  • +

Replies To: recursive function how to output a unordered list

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3554
  • View blog
  • Posts: 10,335
  • Joined: 08-June 10

Re: recursive function how to output a unordered list

Posted 16 December 2013 - 04:47 AM

if it weren’t for the globals, I’d propose the Composite Pattern.
Was This Post Helpful? 1
  • +
  • -

#3 typhlosion  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 109
  • Joined: 09-December 13

Re: recursive function how to output a unordered list

Posted 16 December 2013 - 05:11 AM

Bit cheeky I know, but how would you code a recursive "tree" function in PHP?

The main problem I have is I am also building a simple CMS at the moment and I would like to return just data and then put that data into what ever structure I want.

But this is for a website I am doing at the moment and can be bespoke I need it simple displayed as a "ul" structure so that I can write some jQuery to create a simple animated drop down menu

This post has been edited by Dormilich: 16 December 2013 - 01:41 PM

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3554
  • View blog
  • Posts: 10,335
  • Joined: 08-June 10

Re: recursive function how to output a unordered list

Posted 16 December 2013 - 06:36 AM

Quote

Bit cheeky I know, but how would you code a recursive "tree" function in PHP?

Composite Pattern with Prepared Statements.

probably something along

- create Composite wrapper (root node) object
- pass in the DB object for a child node query
then
- run Composite pattern
- return result

and inside the Composite’s render method
- fetch child node data from db
- create Composite objects
- call render method
- return result
Was This Post Helpful? 1
  • +
  • -

#5 typhlosion  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 109
  • Joined: 09-December 13

Re: recursive function how to output a unordered list

Posted 19 December 2013 - 11:35 AM

wow this gives me a lot more to think about lol

I am trying to raise a CMS from the depth of hell and this is just yet enougher reality slap across the face, however the main reason I am building this CMS is to improve my coding skills so I will start developing somthing along those lines you gave me and see how far I get, however I can't even relate that into pseudo code, is there a simpler way of putting that for a coding novice like myself.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1