需要使用php mysql从动态下拉列表中显示不同的值

时间:2013-05-02 06:49:50

标签: php mysql

您好我正在使用php和mysql创建学生标记条目列表。假设学生在数据库中使用“英语,数学,社交,科学和计算机”,现在我需要从下拉列表中显示这些值,因为每行需要自动显示不同的值。

<?php
    $user = $_SESSION['loginUserId'];
    $commonQuery = mysql_query("select sno from users where userId = '$user' and status='Active'");
    $count = mysql_num_rows($commonQuery);
    if($count == 1)
    {
        $commonQueryRes = mysql_fetch_array($commonQuery);
        $uId=$commonQueryRes['sno'];
        $subjectsCount = mysql_query("select COUNT(subjectName) as sub_count from subjects where userId='$uId' and status='Active' ");
        $subjectsCountRes = mysql_fetch_array($subjectsCount);
        $scount=$subjectsCount['sub_count'];
    }
     $bg = "light";
    for($i=1;$i<=$scount;$i++)
    {
        if ($bg == "light") $bg = "dark";
                else $bg = "light";
    ?>
    <tr class="<?php echo $bg; ?>">
    <td>
        <select name="subName[]">
            <option value="none">--select Subject--</option>
            <?php
                $subjectOptions = mysql_query("select subjectName from subjects where userId='$uId' and status='Active' ");
                while($subjectOptionsRes = mysql_fetch_array($subjectOptions))
                {
                    $sub = $subjectOptionsRes['subjectName'];
                    $sub_Sub_Options = mysql_query("select sno from subjects where userId='$uId' and subjectName='$sub'");
                    $sub_Sub_OptionsRes = mysql_fetch_array($sub_Sub_Options);
            ?>
            <option value="<?php echo $sub_Sub_OptionsRes['sno']; ?>"  selected="selected"><?php echo $subjectOptionsRes['subjectName']; ?></option>';
            <?php
                }
            ?>
        </select>
    </td>
    <td>
        <select name="result[]">
            <option value="none">--select Result--</option>
            <option value="pass"><?php echo "Pass";?></option>
            <option value="fail"><?php echo "Fail";?></option>
        </select>
    </td>
    <td><input type="text" name="quartely[]"  required size="5"/>(Kgs)</td>
    <td><input type="text" name="halfearly[]"  required size="5"/>(Kgs)</td>
    <td><input type="text" name="anually[]"  required size="5"/>(Kgs)</td>
    <td><input type="text" name="rank[]"  required size="5"/>(Kgs)</td>
<?php
}
?>

预期的输出应该是这样的,它应该是动态的:

<table>
          <thead>
            <tr>
              <th style="background: #5792C8;" rowspan="2">subject Name</th>
              <th style="background: #5792C8;" rowspan="2">Result</th>
              <th style="background: #5792C8;" colspan="4"> Progress </th>
            </tr>
            <tr>
                <th>Quarterly</th>
                <th>Halfearly</th>
                <th>Annually</th>
                <th>Rank</th>
            </tr>
          </thead>
          <tbody>
                        <tr class="dark">
                <td>
                    <select name="subjectName[]">
                        <option value="none">--select Subject--</option>
                                                <option value="6"  selected="selected">English</option>';
                                                <option value="7"  >Maths</option>';
                                                <option value="8"  >Science</option>';
                                                <option value="9"  >Social</option>';
                                                <option value="10"  >Computers</option>';
                                            </select>
                </td>
                <td>
                    <select name="result[]">
                        <option value="none">--select Result--</option>
                                                <option value="pass">Pass</option>
                                                <option value="fail">Fail</option>
                                            </select>
                </td>
                <td><input type="text" name="qua[]"  required size="5"/></td>
                <td><input type="text" name="half[]"  required size="5"/></td>
                <td><input type="text" name="anual[]"  required size="5"/></td>
                <td><input type="text" name="rank[]"  required size="5"/></td>
                            <tr class="light">
                <td>
                    <select name="subjectName[]">
                        <option value="none">--select Subject--</option>
                                                <option value="6"  >English</option>';
                                                <option value="7" selected="selected" >Maths</option>';
                                                <option value="8"  >Science</option>';
                                                <option value="9"  >Social</option>';
                                                <option value="10"  >Computers</option>';
                                            </select>
                </td>
                <td>
                    <select name="result[]">
                        <option value="none">--select Result--</option>
                                                <option value="pass">Pass</option>
                                                <option value="fail">Fail</option>
                                            </select>
                </td>
                <td><input type="text" name="qua[]"  required size="5"/></td>
                <td><input type="text" name="half[]"  required size="5"/></td>
                <td><input type="text" name="anual[]"  required size="5"/></td>
                <td><input type="text" name="rank[]"  required size="5"/></td>
                            <tr class="dark">
                <td>
                    <select name="subjectName[]">
                        <option value="none">--select Subject--</option>
                                                <option value="6"  >English</option>';
                                                <option value="7"  >Maths</option>';
                                                <option value="8" selected="selected" >Science</option>';
                                                <option value="9"  >Social</option>';
                                                <option value="10"  >Computers</option>';
                                            </select>
                </td>
                <td>
                    <select name="result[]">
                        <option value="none">--select Result--</option>
                                                <option value="pass">Pass</option>
                                                <option value="fail">Fail</option>
                                            </select>
                </td>
                <td><input type="text" name="qua[]"  required size="5"/></td>
                <td><input type="text" name="half[]"  required size="5"/></td>
                <td><input type="text" name="anual[]"  required size="5"/></td>
                <td><input type="text" name="rank[]"  required size="5"/></td>
</tr>
 </tbody>
        </table>

1 个答案:

答案 0 :(得分:0)

我得到了解决方案,我的解决方案是: 我采用了$count=0,我为动态添加的每行增加了$count+=1。我在一个数组中将subjuct名称作为$ sub_array []并执行if($sub_array[$count]==$subjectOptionsRes['subjectName']){echo 'selected="selected"';}

<?php
    $user = $_SESSION['loginUserId'];
    $commonQuery = mysql_query("select sno from users where userId = '$user' and status='Active'");
    $count = mysql_num_rows($commonQuery);
    if($count == 1)
    {
        $commonQueryRes = mysql_fetch_array($commonQuery);
        $uId=$commonQueryRes['sno'];
        $subjectsCount = mysql_query("select COUNT(subjectName) as sub_count from subjects where userId='$uId' and status='Active' ");
        $subjectsCountRes = mysql_fetch_array($subjectsCount);
        $scount=$subjectsCount['sub_count'];
    }
     $bg = "light";
     $count=0;
    for($i=1;$i<=$scount;$i++)
    {
        if ($bg == "light") $bg = "dark";
                else $bg = "light";
    ?>
    <tr class="<?php echo $bg; ?>">
    <td>
        <select name="subName[]">
            <option value="none">--select Subject--</option>
            <?php
                $subjectOptions = mysql_query("select subjectName from subjects where userId='$uId' and status='Active' ");
                while($subjectOptionsRes = mysql_fetch_array($subjectOptions))
                {
                    $sub = $subjectOptionsRes['subjectName'];
                    $sub_array[] = $subjectOptionsRes['subjectName'];
                    $sub_Sub_Options = mysql_query("select sno from subjects where userId='$uId' and subjectName='$sub'");
                    $sub_Sub_OptionsRes = mysql_fetch_array($sub_Sub_Options);
            ?>
            <option value="<?php echo $sub_Sub_OptionsRes['sno']; ?>"  if($sub_array[$count]==$subjectOptionsRes['subjectName']){echo 'selected="selected"';}><?php echo $subjectOptionsRes['subjectName']; ?></option>';
            <?php
                }
            ?>
        </select>
    </td>
    <td>
        <select name="result[]">
            <option value="none">--select Result--</option>
            <option value="pass"><?php echo "Pass";?></option>
            <option value="fail"><?php echo "Fail";?></option>
        </select>
    </td>
    <td><input type="text" name="quartely[]"  required size="5"/>(Kgs)</td>
    <td><input type="text" name="halfearly[]"  required size="5"/>(Kgs)</td>
    <td><input type="text" name="anually[]"  required size="5"/>(Kgs)</td>
    <td><input type="text" name="rank[]"  required size="5"/>(Kgs)</td>
<?php
$count+=1;
}
?>
相关问题