PHP foreach定位特定项目

I got a DB query that pulls out three items randomly from several categories. I want to show "Selected" if one of the items is from a specific category. Right now if one of the items is from that specific category the "Selected" is showing on all three items. How can I target the specific item only?

foreach ( $rows as $row ) {
if ($row->catid == 56) {
echo "Selected";
}  

enter image description here

foreach ( $rows as $row ) {
if ($row->catid == 56) {
echo "Selected";
break;
}  

Not sure what u want accomplish but i guess u want to show only one selected so u need to break loop if item is found

Your question isn't very clear, but I suspect you're trying to add the attribute to an option. You need to do this in the same loop that echoes the option.

foreach ($rows as $index => $row) {
    $selected = $row->catid == 56 ? "SELECTED" : "";
    $itemnum = $index + 1;
    echo "Item $itemnum - Category $row->catid $selected | ";
}