0

嗨,我正在使用 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>
4

1 回答 1

0

我得到了解决方案,这里我的解决方案是:我接受$count=0并且我正在增加$count+=1每一行动态添加。我将一个数组中的子项目名称作为 $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;
}
?>
于 2013-05-02T15:17:15.420 回答