Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to do last child element clickable in php submenu using mysql table.
please help me i have tried some code but it is working in some cases it is not actually going to else loop if it goes the else mywork will be done how to do that please help me.

What I have tried:

PHP
<pre><?php
mysql_connect('localhost','root','');
mysql_select_db("test");


function submenu($parentid=0){
    $sql=mysql_query("SELECT * FROM test WHERE refid=".$parentid);
    {
      echo '<ul>';
            while($row=mysql_fetch_array($sql))
            {
              if(mysql_num_rows($sql)>0)
              {
              //echo '<li><a href="'.$row['userdefined'].'">'.$row['name'].'</a>';
              echo '<li>'.$row['name'];
              submenu($row['id']);
              echo '</li>';}
              else{
               echo '<ul>';
               echo '<li><a href="'.$row['userdefined'].'">'.$row['name'].'</a>';
               submenu($row['id']);
               echo '</li>';
            }
          }
          echo '</ul>';
      }

}
submenu();

 ?>
Posted
Updated 12-Nov-17 11:45am
v2
Comments
Suvendu Shekhar Giri 9-Nov-17 4:14am    
If it is not going to the ELSE block then you need to check the query the mentioned refid and check if it's really getting a record.
Member 13153537 9-Nov-17 4:22am    
yes it is getting record.
i tried different code somehow it is working fine.i want only child elements clickable remaning should show only text i would i do that .please help me.i am new to php.i am posting my code.thanks in advance.

<?php
mysql_connect('localhost','root','');
mysql_select_db("test");


function submenu($parentid=0){
$sql=mysql_query("SELECT * FROM test WHERE refid=".$parentid);
{
echo '';
while($row=mysql_fetch_array($sql))
{
if(mysql_num_rows($sql)>0)
{

$sql1=mysql_query("SELECT * FROM test where refid=".$row['id']);
if(mysql_num_rows($sql1)>0){

echo ''.$row['name'];
submenu($row['id']);
echo '';}
else{

echo ''.$row['name'].'';
submenu($row['id']);
echo '';
}
}
echo '';
}

}
}
submenu();

?>

1 solution

How about moving away from these mysql_functions to mysqli or pdo? These functions are deprecated since PHP 5.5.0 and are completely removed in PHP 7.0.0. Otherwise, if you still need to do this, have you tried using an incrementing counter instead of using mysql_num_rows?
PHP
function submenu($parentid = 0)
{
    $sql = mysql_query("SELECT * FROM test WHERE refid=" . $parentid);
    
    echo '<ul>';
    
    $numRows = mysql_num_rows($sql);

    // I don't remember exactly, the counter should start either with a 0 or 1
    $i = 0;
    
    while($row = mysql_fetch_array($sql))
    {
        if($numRows > $i)
        {
            //echo '<li><a href="' . $row['userdefined'] . '">' . $row['name'] . '</a>';
            echo '<li>' . $row['name'];
            submenu($row['id']);
            echo '</li>';
        }
        else
        {
            echo '<li><a href="' . $row['userdefined'] . '">' . $row['name'] . '</a>';
            submenu($row['id']);
            echo '</li>';
        }
        $i++;
    }
    
    echo '</ul>';
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900