Tommy Arnold
Tommy Arnold

Reputation: 3389

check checkboxes from mysql result

I have a table (type, typeID, toID) I would like to create an array of the results and check/uncheck the relevant checkboxes, the code I have is not working it only checks the first checkbox.

  <ul class="fields">
  <?php
//
        $query = mysql_query("SELECT typeID FROM assign WHERE type='shades' AND    toID='$row[ID]'")or die(mysql_error());
        $item = mysql_fetch_array($query);
echo'
 <li>
           <label>
             <input type="checkbox" class="checkbox" name="shades[]" ';
    if(in_array("2",$item)){
                         echo" checked=\"checked\" ";};
    echo'value="2"/>Front Shade</label>
</li>
        <li>
    <label><input type="checkbox" class="checkbox" name="shades[]" ';
    if(in_array("4",$item)){echo" checked=\"checked\" ";};
    echo'value="4"/>1 Pair of End Shades</label>
  <li>
    <label><input type="checkbox" class="checkbox" name="shades[]" ';
    if(in_array("1",$item)){echo" checked=\"checked\" ";};
    echo'value="1"/>Left End Shade</label>
  </li>
   <li>
    <label><input type="checkbox" class="checkbox" name="shades[]" ';
    if(in_array("3",$item)){echo" checked=\"checked\" ";};
    echo'value="3"/>Right End Shade</label>
  </li>
';


?>
</ul>

Upvotes: 1

Views: 1107

Answers (2)

Mikhail
Mikhail

Reputation: 859

Why don't you use loop while for result-set? Do you need only first element from result-set?

Upvotes: 0

Pekkasso
Pekkasso

Reputation: 419

mysql_fetch_array() fetches one row from the result. Each row will contain one typeID. You will have to loop through the rows and do something. The code below will add all ID's to an array that you can search in.

$typeIdArray = array();

while($row = mysql_fetch_array($query)) {
    $typeIdArray[] = $row['typeID'];
}

Upvotes: 1

Related Questions