Jump to content

foreach loop


doddsey_65

Recommended Posts

what im trying to do is echo all of the results from the database query but add bold to results with a pid of 0. here is my code:

 

$list = '';
$query = $link->query("SELECT * FROM ".TBL_PREFIX."forums
                        ORDER BY f_lid ASC");
$result = $query->fetchAll();

foreach($result as $key => $val)
{
    if($result[$key]['f_pid'] == 0)
    {
        $list .= '<b>'.$result[$key]['f_name'].'</b><br />';
    }
    $list .= $result[$key]['f_name'].'<br />';
}
echo $list;

 

this works fine but the ones with a pid of 0 are displayed twice. once as bold and then once normal like so:

 

General

General

New Features

 

Testing

Testing

Sandbox

Bugs

 

my question is how to prevent this.

 

Link to comment
https://forums.phpfreaks.com/topic/225938-foreach-loop/
Share on other sites


$list = '';
$query = $link->query("SELECT * FROM ".TBL_PREFIX."forums
                        ORDER BY f_lid ASC");
$result = $query->fetchAll();

foreach($result as $key => $val) {
    if($result[$key]['f_pid'] == 0)  {
        $list .= '<b>'.$result[$key]['f_name'].'</b><br />';
    } else {
       $list .= $result[$key]['f_name'].'<br />';
}
}
echo $list;

Link to comment
https://forums.phpfreaks.com/topic/225938-foreach-loop/#findComment-1166445
Share on other sites

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.