selecing items in listbox that are allready stored in sql

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

Post Reply
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

selecing items in listbox that are allready stored in sql

Post by psychotomus »

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>
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: selecing items in listbox that are allready stored in sql

Post by Stryks »

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

Re: selecing items in listbox that are allready stored in sql

Post by psychotomus »

works like charm. thanks =)
Post Reply