0

私は大学のソフトウェアに取り組んでおり、重要なポイントで立ち往生しています。StudentID、名前、およびロール番号を返すクエリを実行しています。また、教師が入力したタームの割り当て数に基づいて、割り当てマーク エントリ用のテキスト ボックスを含むテーブル列を表示する必要があります。たとえば、ユーザー入力が 3 の場合、課題 1、課題 2、課題 3 の 3 つの列を学生名とロール番号と共に表示したいと考えています。テーブルの行には、すべての生徒の名前と、3 つの課題すべての点数を入力するためのテキスト ボックスが表示されます。また、私は ajax リクエストを介してこのテーブルを表示しています。これは、php がオンザフライで出力を作成し、ユーザーに表示する必要があることを意味します。これにはネストされたループを使用する必要があると想定していますが、その方法はわかりません。列はユーザ​​ー入力に基づいて作成する必要があり、行は mysql クエリの結果に基づいて作成する必要があります。

私のコードは次のとおりです。

$userinput = 3; // hypothetical
$q6 = mysql_query("select * from students");
while($row = mysql_fetch_array($q6)){
$data[] = $row; 
}

echo "<table class='reglist' border='1' width='100%'>";
// start i loop
for($i=1;$i<=$userinput;$i++){
echo "<tr>
<th style='font-size:14px;'>S.No</th>
<th style='font-size:14px;'>Roll No</th>
<th style='font-size:14px;'>Name</th>
<th style='font-size:14px;width:50px;'>Assignment 1</th> // THESE COLUMNS SHUD BE BASED         ON THE USER INPUT.  
</tr>"; }

ユーザー入力に基づいてテーブル ヘッドを作成し、mysql によって返された学生の数に応じて行を表示する方法について、ここで立ち往生しています。

助けてください。

4

2 に答える 2

0

コード フローは次のようになります。

<?php
$userinput = 3;
// Assuming your columnnames for your students table were s_no, roll_no, name, assignment1, assignment2
$query= mysql_query("select * from students");
?>

<table>
<tr>
    <th style='font-size:14px;'>S.No</th>
    <th style='font-size:14px;'>Roll No</th>
    <th style='font-size:14px;'>Name</th>
    <?php
    for($i=0; $i<$userinput; $i++)
    {
    ?>
        <th style='font-size:14px;width:50px;'>Assignment <?php echo $i+1;?> </th> <!--this will display Assignment 1, Assignment 2 -->
    <?
    }//end for
    ?>
</tr>

<tr id="wrapper"> <!--assign an id here so you can use this on your ajax request -->
    <?php
        while($row = mysql_fetch_array($query)){
    ?>
            <td><?php echo $row['s_no'];?></td>
            <td><?php echo $row['roll_no'];?></td>
            <td><?php echo $row['name'];?></td>
            <?php
            for($i=0; $i<$userinput; $i++)
            {
            ?>
                <td> I dont know what are you trying to display here.</td>
            <?php
            }//end for
            ?>

    <?php
        }//end while
    ?>
</tr> 

</table>

データベースに 3 人の学生がいるとします。上記のコードは、次のようなものを表示します。これは、上記のコードに基づく出力例です。

于 2012-11-06T06:10:40.080 に答える
0

このコードを試してください:

$q6 = mysql_query("select * from students");

echo "<table class='reglist' border='1' width='100%'>";
echo "<tr>
<th style='font-size:14px;'>S.No</th>
<th style='font-size:14px;'>Roll No</th>
<th style='font-size:14px;'>Name</th>
<th style='font-size:14px;width:50px;'>Assignment 1</th>
<th style='font-size:14px;'>Assigmnent 2</th>
</tr>";

while ($row = mysql_fetch_array($q6)) {
    echo "<tr>
    <th style='font-size:14px;'>{$row['stud_num']}</th>
    <th style='font-size:14px;'>{$row['roll_num']}</th>
    <th style='font-size:14px;'>{$row['name']}</th>
    <th style='font-size:14px;width:50px;'>{$row['assign_one']}</th>
    <th style='font-size:14px;'>{$row['assign_two']}</th>
    </tr>";

}
于 2012-11-06T06:07:21.530 に答える