根据用户输入显示带有for循环的表列 [英] display table columns with for loop based on user input

查看:67
本文介绍了根据用户输入显示带有for循环的表列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究大学软件,并处于关键时刻.我正在运行一个查询,该查询将返回学生编号,姓名和名册编号,并且我需要根据教师对一个学期的作业数量输入,显示带有文本框的表格列,以输入作业标记.例如,如果用户输入为3,则我想显示3列,即作业1,作业2,作业3以及学生姓名和rollno.然后,表格行将显示所有学生姓名以及带有文本框的文本框,以便为所有三个作业输入分数.另外,我通过ajax请求显示此表,这意味着php将必须动态创建输出并显示给用户.我假设必须为此使用嵌套循环,但不确定如何使用.必须根据用户输入创建列,而行应基于mysql查询结果.

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.

我的代码是:

$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返回的学生数显示行,我陷入了困境.

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.

请帮助.

推荐答案

代码流应如下所示:

<?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个学生.上面的代码将显示以下内容:

Assuming you have 3 students in the database. The code above will display something this:

这篇关于根据用户输入显示带有for循环的表列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆