Jump to content

Updating a dynamic menu problem


moonshine

Recommended Posts

I'm trying to figure it out why my code it's only updating the last menu or submenu of a menu. The menu and submenu are stored in the same database and the parent_id is equal to the id for the sub-menus . Parent_id = 0 means it's a main menu. Help!

 

 

 $con = mysql_connect($server, $user_name, $password);

 

    $db_found = mysql_select_db($database, $con);

 

if(isset($_POST['update']))

{

 

$sql = "UPDATE `menus` SET Menu_bar = '".$_POST['menu_bar']."' WHERE ID = ".$_POST['hidden'];

mysql_query( $sql, $con );

}

 

$resultMainMenu = mysql_query("SELECT * FROM menus WHERE Parent_id=0 ") or die(mysql_error());

echo '<form action=page.php method=post>';

while($row = mysql_fetch_assoc($resultMainMenu))


echo '<ul>';

    echo "<li><input type=submit name=update value=update><input type=hidden name=hidden value='".$row['ID']."'><input type=text name=menu_bar value='".$row['Menu_bar']."'><br/>" ; // echo main menu

    

$resultSubmenu = mysql_query("SELECT * FROM menus WHERE Parent_id = " . $row['ID']) or die(mysql_error());

echo '<ul>';

        while($rowSub = mysql_fetch_assoc($resultSubmenu))

{   

            echo "<li><input type=submit name=update value=update><input type=hidden name=hidden value='".$rowSub['ID']."'><input type=text name=menu_bar value='".$rowSub['Menu_bar']."'><br/></li>"; // echo sub menu

}

echo '</ul>';

    

echo '</li>';

echo'</ul>';

}

echo '</form>';

   ?> 

Edited by moonshine
Link to comment
Share on other sites

Show us your database table structures.

All of the parent menus in the menus table have a Parent_id = 0;

You're getting the sub-menu of each parent menu by looking for the Parent_id = $row['ID'].

So we can assume from this that the $row['ID'] of each parent matches the Parent_id of their sub-menu.

Link to comment
Share on other sites

CREATE TABLE `lab4`.`menus` (

`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`Menu_bar` VARCHAR( 100 ) NOT NULL ,
`Parent_id` INT NOT NULL

) ENGINE = MYISAM ;

 

ID  Menu_bar  Parent_id

1   Home        0

2   News        0

3   Events      2

4   Places      2

5   Contact     0

 

So Events and Places are the sub-menus of News. 

Yes all of the parent menus have Parent_id=0 and the sub-menus have the Parent_id equal to their parent unique ID . 

With only one while loop it works perfect but all of the items are dispalyed like a table and it should look like a menu .

Edited by moonshine
Link to comment
Share on other sites

Here is the html structure you're after.

<ul>
    <li>Home</li>
    <li>News
        <ul>
            <li>Events</li>
            <li>Places</li>
        </ul>
    </li>
    <li>Contact</li>
</ul>

Reworking your code.

echo '<ul>';
    echo "<li><input type=\"submit\" name=\"update\" value=\"update\"><input type=\"hidden\" name=\"hidden" value=\"".$row['ID']."\"><input type=\"text\" name=\"menu_bar\" value=\"".$row['Menu_bar']."\"><br/>"; // echo main menu
    
$resultSubmenu = mysql_query("SELECT * FROM menus WHERE Parent_id = " . $row['ID']) or die(mysql_error());

if( ! mysql_num_rows($resultSubmenu))
{
   echo '</li>';
}
else
{
   echo '<ul>';
   while($rowSub = mysql_fetch_assoc($resultSubmenu))
   {   
   echo "<li><input type=\"submit\" name=\"update\" value=\"update\"><input type=\"hidden\" name=\"hidden\" value="\". $rowSub['ID']."\"><input type=\"text\" name=\"menu_bar\" value="\".$rowSub['Menu_bar']."\"><br/></li>"; 
   // ec  ho sub menu
   }
   echo '</ul>';
   echo '</li>';
}

I believe I got that right - you'll have to test it - might have missed an escape or some other syntax error.

Edited by hansford
Link to comment
Share on other sites

Try this - if it doesn't work, then I'll have to create a dummy db structure on my local and run the code myself to see what's going on.

 $con = mysql_connect($server, $user_name, $password);
 
    $db_found = mysql_select_db($database, $con);
 
if(isset($_POST['update']))
{
 
$sql = "UPDATE `menus` SET Menu_bar = '".$_POST['menu_bar']."' WHERE ID = ".$_POST['hidden'];
mysql_query( $sql, $con );
}
 
$resultMainMenu = mysql_query("SELECT * FROM menus WHERE Parent_id=0 ") or die(mysql_error());
echo '<form action=\"page.php\" method=\"post\">';
echo '<ul>';
while($row = mysql_fetch_assoc($resultMainMenu))
{ 
echo "<li><input type=\"submit\" name=\"update\" value=\"update\"><input type=\"hidden\" name=\"hidden" value=\"".$row['ID']."\"><input type=\"text\" name=\"menu_bar\" value=\"".$row['Menu_bar']."\"><br/>"; // echo main menu
    
$resultSubmenu = mysql_query("SELECT * FROM menus WHERE Parent_id = " . $row['ID']) or die(mysql_error());

if( ! mysql_num_rows($resultSubmenu))
{
   echo '</li>';
}
else
{
   echo '<ul>';
   while($rowSub = mysql_fetch_assoc($resultSubmenu))
   {   
   echo "<li><input type=\"submit\" name=\"update\" value=\"update\"><input type=\"hidden\" name=\"hidden\" value=\"". $rowSub['ID']."\"><input type=\"text\" name=\"menu_bar\" value="\".$rowSub['Menu_bar']."\"><br/></li>"; 
   // echo sub menu
   } // end sub-menu loop
   
   echo '</ul>'; // end sub-menu ul
   echo '</li>'; // end main-menu li
}

} // end main-menu loop

echo '</ul>'; // end main-menu ul
echo '</form>';
   ?> 
Link to comment
Share on other sites

I ran the code myself. It works - all menu items and sub-menu items display.

It will not update as designed. You would need to make each one of those menu items with the "update" button, its own separate form. Otherwise, it's just defaulting to the last form element - which is the contact menu item. You can change that one, but the others get ignored because they all have the same form element names.

 $con = mysql_connect($server, $user_name, $password);
 
    $db_found = mysql_select_db($database, $con);
 
if(isset($_POST['update']))
{
$sql = "UPDATE `menus` SET Menu_bar = '".$_POST['menu_bar']."' WHERE ID = ".$_POST['hidden'];

if(!mysql_query( $sql, $con ))
{
    echo 'update error: ' . mysql_error();
}
}

$resultMainMenu = mysql_query("SELECT * FROM menus WHERE Parent_id=0 ") or die(mysql_error());
echo '<form action="page.php" method="post">';
echo '<ul>';
while($row = mysql_fetch_assoc($resultMainMenu))
{
echo '<li><input type="submit" name="update" value="update"><input type="hidden" name="hidden" value="'.$row['ID'].'"><input type="text" name="menu_bar" value="'.$row['Menu_bar'].'"><br/>'; // echo main menu

$resultSubmenu = mysql_query("SELECT * FROM menus WHERE Parent_id=" . $row['ID']) or die(mysql_error());

if( ! mysql_num_rows($resultSubmenu))
{
   echo '</li>';
}
else
{
   echo '<ul>';
   while($rowSub = mysql_fetch_assoc($resultSubmenu))
   {
   echo '<li><input type="submit" name="update" value="update"><input type="hidden" name="hidden" value="'. $rowSub['ID'].'"><input type="text" name="menu_bar" value="'.$rowSub['Menu_bar'].'"><br/></li>';
   // echo sub menu
   } // end sub-menu loop

   echo '</ul>'; // end sub-menu ul
   echo '</li>'; // end main-menu li
}

} // end main-menu loop

echo '</ul>'; // end main-menu ul
echo '</form>';
   ?>

And I know you're just getting your feet wet, but it would be better to get off on the right one.

You should be using PDO or mysqli to make your database connections/interactions as the way you're doing it has been deprecated because of serious security related issues and slated for removal. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.