简体   繁体   中英

check box if equal to value retrieved from database

I'm developing a website where students can subscribe to certain hours they have to attend, and each hour has an unique ID (pitid), the code below searches for the hours the user subscribed itself to, and I want the checkbox to be checked if the pitid is equal to a value retrieved with the code below. But i can't figure out how; maybe put the query result in an array and check for each pitid if it's equal to one of the values saved in the array...

!( http://i63.tinypic.com/14uj77k.png )

$user=$_SESSION['user'];
$q=mysql_query("SELECT timetable.pitid FROM timetable,hours WHERE llnr='".$user."' AND timetable.pitid=hours.pitid AND hours.location='olc' ");

for($i=0; $i<mysql_numrows($q);$i++){
    $check=mysql_result($q,$i,"pitid");
    echo"$check<br>";
 } 

This is the code that generates the list:

$user=$_SESSION['sess_user'];
$olchours= mysql_query("SELECT pitid,pit,hour,location,seats FROM hours     WHERE `hours`.`location` = 'olc' ORDER BY pitid");
echo'<table>
        <tr>
           <td width="100px">Select</td>
           <td width="100px">PIT.ID</td><td width="100px">Day</td>
           <td width="100px">hour</td><td width="100px">taken seats</td>
           <td width="300px">Available Seats</td>
        </tr>'; 
for($i=0; $i<mysql_numrows($olchours); $i++){
    $olcpitid=mysql_result($olchours,$i,"pitid");   
    $olcday=mysql_result($olchours,$i,"pit");   
    $olchour=mysql_result($olchours,$i,"hour");
    $olcseats=mysql_result($olchours,$i,"seats");       
    $olcq=mysql_query("SELECT COUNT(*) FROM hours,timetable WHERE hours.pitid='".$olcpitid."' AND hours.pitid=timetable.pitid");
    $olcrow=mysql_fetch_assoc($olcq);
    $olcmatches=$olcrow['COUNT(*)'];        
    $olcopen=$olcseats-$olcmatches;
        if(($i%8)==0){
            echo'<tr><td>&nbsp;</td></tr><tr><td><form method="POST" action=""><input type="checkbox" name="hourolc[]" value="'.$olcpitid.'">
            <td>'.$olcpitid.' </td><td>'.$olcday.' </td><td>'.$olchour.'</td><td>'.$olcmatches.'</td><td>'.$olcopen.'</td></tr>';
        }               
            else{
                echo'<tr><td><form method="POST" action=""><input type="checkbox" name="hourolc[]" value="'.$olcpitid.'" >
                <td>'.$olcpitid.' </td><td>'.$olcday.' </td><td>'.$olchour.'</td><td>'.$olcmatches.'</td><td>'.$olcopen.'</td></tr>';
            }


}
echo'</table><br>'; 

}

Try this, Read the comments for explanation.

$queryResult = array('', '', ''); // list from db
$to_be_matched_pitid = 3; // to be matched pitid

if(in_array($to_be_matched_pitid, $queryResult)) {
    echo 'array CONTAINS the pitid';
} else {
    echo 'array does not CONTAIN the pitid';
}

change

$check=mysql_result($q,$i,"pitid");

to

$check[]=mysql_result($q,$i,"pitid"); 

to store values in array

edit your if else

<?php 
    if(($i%8)==0){
        echo'<tr><td>&nbsp;</td></tr><tr><td><form method="POST" action=""><input type="checkbox" name="hourolc[]" value="'.$olcpitid.'"';if($your_db_value == $to_be_matched){echo 'checked="checked"'}echo'>
        <td>'.$olcpitid.' </td><td>'.$olcday.' </td><td>'.$olchour.'</td><td>'.$olcmatches.'</td><td>'.$olcopen.'</td></tr>';
    }               
        else{
            echo'<tr><td><form method="POST" action=""><input type="checkbox" name="hourolc[]" value="'.$olcpitid.'"';if($your_db_value == $to_be_matched){echo 'checked="checked"'} echo' >
            <td>'.$olcpitid.' </td><td>'.$olcday.' </td><td>'.$olchour.'</td><td>'.$olcmatches.'</td><td>'.$olcopen.'</td></tr>';
        }
    ?>  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM