Mysql... While... Loops...

Looking for volunteers to join your project? Need help with a script but can't afford to pay? Want to offer your services as a volunteer to build up your portfolio? This is the place for you...

Moderator: General Moderators

nmb
Forum Newbie
Posts: 21
Joined: Thu Jul 20, 2006 11:08 am

Post by nmb »

hi again.. yes that resolved the problem with the <ul></ul> tags.. mas the code still wrong..

the is the returned code:

Code: Select all

<ul> 
  <li>Cat 1</li> 
  <ul> 
    <li>Cat 1-1</li> 
    <li>Cat 1-2</li> 
    <li>Cat 1-3</li> 
    <ul> 
      <li>Cat 1-3-1</li> 
      <li>Cat 1-3-2</li> 
      <li>Cat 1-3-3</li> 
      <ul> 
        <li>Cat 1-3-3-1</li> 
      </ul> 
      <li>Cat 1-3-4</li> 
    </ul> 
    <li>Cat 1-4</li> 
  </ul> 
  <li>Cat 2</li> 
  <ul> 
    <li>Cat 2-1</li> 
    <li>Cat 2-2</li> 
    <li>Cat 2-3</li> 
  </ul> 
  <li>Cat 3</li> 
  <li>Cat 4</li> 
</ul>

the problem is the script close <li>'s tag before open the sub level <ul>'s.

the correct code is like this one:

Code: Select all

<ul> 
 <li>Cat 1 
  <ul> 
   <li>Cat 1-1</li> 
   <li>Cat 1-2</li> 
   <li>Cat 1-3 
    <ul> 
     <li>Cat 1-3-1</li> 
     <li>Cat 1-3-2</li> 
     <li>Cat 1-3-3 
      <ul> 
       <li>Cat 1-3-3-1</li> 
      </ul> 
     </li> 
     <li>Cat 1-3-4</li> 
    </ul> 
   </li> 
   <li>Cat 1-4</li> 
  </ul> 
 </li> 
 <li>Cat 2 
  <ul> 
   <li>Cat 2-1</li> 
   <li>Cat 2-2</li> 
   <li>Cat 2-3</li> 
  </ul> 
 </li> 
 <li>Cat 3</li> 
 <li>Cat 4</li> 
</ul>
this is the last problem.. i hope :p
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

The solution to this is pretty obvious. You're just trying to get someone to write this code for you without putting in any effort of your own whatsoever. As you just want someone to write this for you I'm moving this thread to Volunteer Work because that's clearly a more appropriate place for it.

If you followup with an actual problem with your code, or a question about how this works so you can solve it yourself, then I'll move it back to the PHP Code folder.
nmb
Forum Newbie
Posts: 21
Joined: Thu Jul 20, 2006 11:08 am

Post by nmb »

ok.. the problem is that i don't know to do it..
i don't know how to work with recursion..

..i was only trying to get help on this.. if you think that ok.. no prob. tnks
nmb
Forum Newbie
Posts: 21
Joined: Thu Jul 20, 2006 11:08 am

Post by nmb »

ok.. i have the code i need.

Code: Select all

function getTree($id_catsup=0,$level=0) {
  $html = "";
  $sql = "select * from categorias where id_catsup = '".$id_catsup."' order by id";
  $categoryresult = mysql_query($sql);
  if (mysql_num_rows($categoryresult) > 0) {
   $html .= "<ul>";
   while ($record = mysql_fetch_object($categoryresult)) {
    $html .= "<li>".$record->categoria_nome_pt;
    $html .= getTree($record->id,$level+1);
    $html .= "</li>";
   }
   $html .= "</ul>";
  }
  return $html;
 }
 $showcat = getTree(0);
thanks
Post Reply