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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.