PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
psychotomus
Forum Contributor
Posts: 487 Joined: Fri Jul 11, 2003 1:59 am
Post
by psychotomus » Mon Jun 30, 2008 7:43 pm
Its currently selecting all items and not the 3 items that should already be shown.
anyone know whats wrong?
Code: Select all
<select name="select[]" size="10" multiple="MULTIPLE" id="select[]">
<?
$cats[] = explode("|",$theme->cats);
$result = mysql_query("SELECT * FROM cats ORDER BY name ASC");
while($cat = mysql_fetch_object($result))
{
$found = false;
for($i=0; $i<count($cats);$i++)
{
if($cat->id == $cats[$i])
{
echo $cats[$i];
$found = true;
exit;
}
}
if($found == false)
{
?>
<option value="<?= $cat->id ?>"><?= $cat->name ?></option>
<?
}
else
{
?>
<option value="<?= $cat->id ?>" selected><?= $cat->name ?></option>
<?
}
}
?>
</select>
Stryks
Forum Regular
Posts: 746 Joined: Wed Jan 14, 2004 5:06 pm
Post
by Stryks » Mon Jun 30, 2008 8:34 pm
It's hard to say, not knowing what the data is ... but perhaps change ...
Code: Select all
<?
$cats[] = explode("|",$theme->cats);
$result = mysql_query("SELECT * FROM cats ORDER BY name ASC");
while($cat = mysql_fetch_object($result))
{
$found = false;
for($i=0; $i<count($cats);$i++)
{
if($cat->id == $cats[$i])
{
echo $cats[$i];
$found = true;
exit;
}
}
if($found == false)
{
?>
<option value="<?= $cat->id ?>"><?= $cat->name ?></option>
<?
}
else
{
?>
<option value="<?= $cat->id ?>" selected><?= $cat->name ?></option>
<?
}
}
?>
... to ...
Code: Select all
<?
$cats[] = explode("|",$theme->cats);
$result = mysql_query("SELECT * FROM cats ORDER BY name ASC");
while($cat = mysql_fetch_object($result))
{
if(in_array($cat->id, $cats)) {
?>
<option value="<?= $cat->id ?>" selected><?= $cat->name ?></option>
<?
} else {
?>
<option value="<?= $cat->id ?>"><?= $cat->name ?></option>
<?
}
}
?>
Is that doing the job?
Last edited by
Stryks on Mon Jun 30, 2008 10:08 pm, edited 1 time in total.
psychotomus
Forum Contributor
Posts: 487 Joined: Fri Jul 11, 2003 1:59 am
Post
by psychotomus » Mon Jun 30, 2008 9:39 pm
works like charm. thanks =)