Jump to content

foreach or while problem


Cflax

Recommended Posts

I am trying to make a basic navigation across the top of my pages in my navmenu showing all of the categories in my ob_category table. so if i add a category to the database later, the link in the navmenu is automatically added. this is what i was experimenting with at the moment:

 

<?php

$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

 

$query1 = "SELECT * FROM ob_category";

$data1 = mysqli_query($dbc, $query1);

$row1 = mysqli_fetch_array($data1);

foreach($row1['name'] as $value){

echo '<a href="viewlistings.php">' . $value . '</a> &#8226;';

}

?>

 

not working, I'm not worried about the links working quite yet, its the category names im most concerned about

any ideas??

Link to comment
https://forums.phpfreaks.com/topic/234573-foreach-or-while-problem/
Share on other sites

ya it did but i got it to work from what you said THANK YOU lol

 

<?php
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

$query1 = "SELECT name FROM ob_category";
$data1 = mysqli_query($dbc, $query1);
while($row=mysqli_fetch_array($data1)){
     echo '<a href="viewlistings.php">' . $row['name'] . '</a> •';
}
?>

 

now to figure out how to get rid of the bullet separator on the last category

now to figure out how to get rid of the bullet separator on the last category

You could do

$links = array();
while($row=mysqli_fetch_array($query)){
     $links[] =  '<a href="viewlistings.php">' . $row['name'] . '</a>';

echo implode(' •', $links);

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.