0

I want to return some data from my table as navbar links, my table holds this information:

capture

Then I coded this:

<?php 
$result = $db->query("SELECT * FROM topnav ORDER BY 'table_id' ASC");   
while($row = $result->fetch_object()){
    echo "
    <li><a href='contact.html'>".$row->name_link."</a></li>
    <li><a href='about.html'>".$row->name_link."</a></li>
    <li><a href='news.html'>".$row->name_link."</a></li>
    <li><a href='news.html'>".$row->name_link."</a></li>
    <li><a href='news.html'>".$row->name_link."</a></li>
    <li><a href='blog.html'>".$row->name_link."</a></li>
    <li class='active'><a href='index.html'>".$row->name_link."</a></li>
    ";  
}
?>

But this is wrong because it prints this as result:

capture 2

And I want each item to be printed once and then another name_link appears.

So how can I do that?

0

2 Answers 2

0

Assuming the value from name_link column is the same of html file, then:

<?php 
$result = $db->query("SELECT * FROM topnav ORDER BY 'table_id' ASC");   
while($row = $result->fetch_object()){
    echo "<li><a href='".$row->name_link.".html'>".$row->name_link."</a></li>";  
}
?>
Sign up to request clarification or add additional context in comments.

Comments

0

This would seem to be more like what you need - create one <li> per row returned from your table. (I've assumed href-link is the field in your table which should be used as the URL.)

<?php 
$result = $db->query("SELECT * FROM topnav ORDER BY 'table_id' ASC");   
while($row = $result->fetch_object()){
    echo "<li><a href='".$row->href_link."'>".$row->name_link."</a></li>";  
}
?>

What you were doing before is repeating your entire menu for every row in your table, which doesn't make any sense.

Comments