Reputation: 115
<?php
$r=mysql_query("SELECT city.id as city_id, locality.id,locality.area_name ".
"FROM city, locality ".
"WHERE city.id = locality .city_id order by city_id");
while($rss=mysql_fetch_object($r))
{
?>
<legend><?=$rss->city_id?></legend>
<input type="checkbox" name="sub_type[]" id="sub_type" value="<?=$rss->id?>" />
<span><?=$rss->area_name?></span><br/>
<?php
}
?>
I am displaying area names with their city ID as a label. The problem is that this ID label is added for each area name due to the surrounding while loop. How can I prevent the ID label from being repeated multiple times, so that the checkboxes are grouped under the city ID they belong to?
Upvotes: 3
Views: 324
Reputation: 91415
How about:
<?php
$r=mysql_query("SELECT city.id as city_id, locality.id,locality.area_name ".
"FROM city, locality ".
"WHERE city.id = locality .city_id order by city_id");
$previous = '';
while($rss=mysql_fetch_object($r)) { ?>
<?php if (empty($previous) || $previous != $rss->city_id) { ?>
<legend><?=$rss->city_id?></legend>
<?php } ?>
<?php $previous = $rss->city_id; ?>
<input type="checkbox" name="sub_type[]" id="sub_type" value="<?=$rss->id?>" />
<span><?=$rss->area_name?></span><br/>
<?php
}
?>
Upvotes: 1
Reputation: 943561
Move the <legend>
so it is outside the while loop.
(And use a <label>
not a <span>
… you'll need to fix the id
of your inputs too, they have to be unique)
Upvotes: 1