0

i am working on a college software and am stuck at a crucial point. i am running a query which will return studentid, names and rollno(s) and i need to display table columns with text boxes for assignment marks entry based on the teacher's input of number of assignments for a term. For instance, if user input is 3, i want to display 3 columns namely, Assignment 1, Assignment 2, Assignment 3 along with student name and rollno. the table rows will then have all students names with text boxes for entering marks for all three assignments. Also, i am displaying this table through ajax request which means php will have to create the output on the fly and display to user. I am assuming a nested loop will have to be used for this but am not sure how. Columns have to be created based on user input and rows should be based on mysql query result.

My code is:

$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>"; }

I am stuck here as to how to create table heads based on user input and then display rows according to the number of students returned by mysql.

Please help.

coder101
  • 1,601
  • 2
  • 21
  • 41

2 Answers2

0

Try this code :

$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>";

}
Lao
  • 168
  • 1
  • 8
0

The code flow should be something like this:

<?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>

Assuming you have 3 students in the database. The code above will display something this: This is the sample output base on the code above

user1149244
  • 711
  • 4
  • 10
  • 27