简体   繁体   English

如果等于从数据库检索到的值,则复选框

[英]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. 我正在开发一个网站,学生可以在该网站上预订他们必须参加的某些时间,并且每个小时都有一个唯一的ID(PID),下面的代码搜索用户自己订阅的时间,我希望选中该复选框如果pitid等于使用以下代码检索的值。 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... 也许将查询结果放入数组中,并检查每个pitid是否等于数组中保存的值之一...

!( http://i63.tinypic.com/14uj77k.png ) !( 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>';
        }
    ?>  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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