Jump to content

[SOLVED] Menu from mysql


jdrasq

Recommended Posts

Hello,

I have set up a menu css in a page, as shown

Code:

#topnav

{

float: left;

width: 100%;

background: #2E3C83;

border-top: 0px;

border-bottom: 0px;

}

 

#topnav li { display: inline; }

#topnav li a

{

display: block;

float: left;

padding: 7px;

text-align: center;

background: repeat-y 100% 0;

font-weight: bold;

color: #ffffff;

text-decoration: none;

}

#topnav li a:hover

{

color: #0E1D6D;

}

 

 

And then written on the page the list of links and url's in the following format,

 

Code:

<div id="topnav">

  <ul>

  <li><a href="URL1">Itemname 1</a></li>

  <li><a href="URL2">Itemname 2</a></li>

  <li><a href="URL3">Itemname 3</a></li>

  <li><a href="URL4">Itemname 4</a></li>

  </ul>

</div>

 

 

I was hoping to link this to a table in a mysql database so that i do not have to change it on every page when I add a new page. I put the data into a table but I'm not sure how to call it from the database, so far I have the following

Code:

   

<div id="topnav"> <ul>

        <?php

(connection information...)

$query="SELECT * FROM mainnav ORDER BY ID asc";

$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<ul>";

$i=0;

while ($i < $num) {

$Name=mysql_result($result,$i,"Name");

$URL=mysql_result($result,$i,"URL");

echo "<li><a href="$URL">$Name</a></li>";

$i++;

}

?>

</ul>

</div>   

 

 

but this is not working correctly, could anybody help or suggest an alternative method?

Sorry if I made that too long and complicated,

Thank you for any help,

JDRasq

Link to comment
https://forums.phpfreaks.com/topic/63562-solved-menu-from-mysql/
Share on other sites

Well, I don't know about the rest of the stuff, but you might want to redo your PHP Code to something like this:

 

 

<?php
echo "<ul>";

$query = "SELECT * FROM `mainnav` ORDER BY `ID` asc";
$result = mysql_query($query) or die("<li>Could Not Select Menus!</li>");

while ($row = mysql_fetch_assoc($result)) {
     echo '<li><a href="'. $row['URL']. '">'. $row['Name']. '[/url]</li>';
}

echo "</ul>";
?>

 

It will be easier to follow.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.