Gleasonator Posted July 8, 2008 Share Posted July 8, 2008 Hi all. In my database called "test" I have a table called "navbar". Under that table, I have three fields: "ID" (primary key, auto inc), "Name", and "Link". What I need to do it use PHP to submit a query to the database and grab every row in the table, and for every row, output a <li> tag. For example, let's say I have these rows: Row 1: ID = 1 Name = Home Link = home.php Row 2: ID = 2 Name = Contact Us Link = contact.php I want to output HTML like this: <ul> <li><a href="home.php">Home</a></li> <li><a href="contact.php">Contact Us</a></li> ...etc. If there were more rows in the database, they'd be here in the same format. </ul> I would greatly appreciate it if anyone could help. I'm pretty new to PHP. Link to comment https://forums.phpfreaks.com/topic/113669-solved-how-to-output-dynamic-content-using-phpsql/ Share on other sites More sharing options...
ratcateme Posted July 8, 2008 Share Posted July 8, 2008 <?php $con = mysql_connect(); mysql_select_db('test'); $query="SELECT * FROM `navbar`;"; $result=mysql_query($query,$con) or die(mysql_error($con)); if(mysql_num_rows($result)==0) die("error"); echo "<ul>\n"; while($row = mysql_fetch_assoc($result)){ echo "<li><a href=\"{$row['Link']}\">{$row['Name']}</a></li>\n"; } echo "</ul>"; Scott. Link to comment https://forums.phpfreaks.com/topic/113669-solved-how-to-output-dynamic-content-using-phpsql/#findComment-584160 Share on other sites More sharing options...
Gleasonator Posted July 8, 2008 Author Share Posted July 8, 2008 <?php $con = mysql_connect(); mysql_select_db('test'); $query="SELECT * FROM `navbar`;"; $result=mysql_query($query,$con) or die(mysql_error($con)); if(mysql_num_rows($result)==0) die("error"); echo "<ul>\n"; while($row = mysql_fetch_assoc($result)){ echo "<li><a href=\"{$row['Link']}\">{$row['Name']}</a></li>\n"; } echo "</ul>"; Scott. Scott, you are awesome. Can I buy you a drink? EDIT: Oh, you're 16... a Coke? Link to comment https://forums.phpfreaks.com/topic/113669-solved-how-to-output-dynamic-content-using-phpsql/#findComment-584170 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.