Jump to content

[SOLVED] How to output dynamic <li> content using PHP/SQL?


Gleasonator

Recommended Posts

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.

<?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.

<?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?

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.