0

I want to create a dynamic footer menu (not multilevel) with mysql.

My MySQL table is like below...

---------------------------------------------------
|  menu_id  |  menu_name  |  menu_url  | short_id  |
----------------------------------------------------
|  1        | Home        | index.php  | 1         |
----------------------------------------------------
|  2        | Contact Us  | contact.php| 3         |
----------------------------------------------------
|  3        | About Us    | abt.php    | 2         |
----------------------------------------------------

The HTML structure is like...

<div class="footer-menu">
 <ul>
    <li><a href="index.php">Home</a></li>
    <li><a href="abt.php">About Us</a></li>
    <li><a href="contact.php">Contact Us</a></li>
 </ul>
</div>

I have coded like...

mysql_select_db($db,$con);
$f_menu_qry = "SELECT * FROM footermenu ORDER BY short_id ASC";
$fm = mysql_query($f_menu_qry,$con);
$f_menu = mysql_fetch_assoc($fm);
$totrows = mysql_num_rows($fm);
$menu_name = $f_menu['menu_name'];
$menu_url = $f_menu['menu_url'];

    public function getFooterMenu(){
    global $fm, $f_menu, $menu_name, $menu_url, $totrows;
    $footer_menu = '';
    $cnt = 0;
     while ($clt_f_menu = mysql_fetch_assoc($fm)){
         $cnt++;
         $footer_menu = '<li><a href="'.$menu_url.'">'.$menu_name.'</a></li>';
         if($cnt == $totrows){
             return;
         }
     }
     echo $footer_menu;
    }

The output should be like Home / About Us / Contact Us

But the output is showing only Home.

I have tried different processes but those were showing errors(may be for my faulty code) but this time it is showing at least a link.

I am unable to do it.

What should I do ?

2 Answers 2

1

First of all , it's not a good practice to use old mysql extension. It's better to use PDO or MySQLi.

Any way, I've modified your code a bit. You can try this one. Hope it'll work..

mysql_select_db($db,$con);
$f_menu_qry = "SELECT * FROM footermenu ORDER BY short_id ASC";   
$fm = mysql_query($f_menu_qry,$con);

$clt_f_menu = array();
while($rows = mysql_fetch_array($fm))
{
    $clt_f_menu[$rows['menu_id']] = array(
            'menu_name' => $rows['menu_name'],
            'menu_url' => $rows['menu_url']
        );
}

echo '<div class="footer-menu">';
    echo '<ul>';
    foreach ($clt_f_menu as $key => $value) {
        echo '<li><a href="'.$value['menu_url'].'">'.$value['menu_name'].'</a></li>';
    }
    echo '</ul>';
echo '</div>'; 
Sign up to request clarification or add additional context in comments.

2 Comments

I have tried your code. It is showing About Us / Contact Us but not showing Home
Got my answer. Thank you @mi6crazyheart
0

There are two main aspects with

$footer_menu = '<li><a href="'.$menu_url.'">'.$menu_name.'</a></li>';

$footer_menu will have only the content from the last loop, because "=" will overwrite it in each loop and you are using global variables here which have the content from the first row only.

Try this:

mysql_select_db($db,$con);
echo getFooterMenu($con);

public function getFooterMenu($con){
  $sql = "SELECT * FROM footermenu ORDER BY short_id ASC";
  $mid = mysql_query($sql,$con);
  $footer_menu = '';
  while($rs = mysql_fetch_assoc($mid)) {
    $footer_menu.= '<li><a href="'.$rs["menu_url"].'">'.$rs["menu_name"].'</a></li>';
  }
  return $footer_menu;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.