简体   繁体   English

用数组创建PHP表

[英]PHP table creation with arrays

I have a table with a first column called: "Fruit", and a last column called: "Total" 我有一个表,其第一列称为:“ Fruit”,最后一列称为:“ Total”

And any columns in between are dynamically created by the number of students. 中间的任何列都是根据学生人数动态创建的。 Below is what I'm looking to dynamically make, from what the database shows so far. 根据数据库显示的内容,以下是我要动态生成的内容。

<table border="1">
  <tr>
    <th>Fruit</th>
    <th>Sally</th>
    <th>John</th>
    <th>Total</th>
  </tr>
  <tr>
    <td>apples</td>
    <td>5</td>
    <td>3</td>
    <td>8</td>
  </tr>
  <tr>
    <td>bananas</td>
    <td>3</td>
    <td>5</td>
    <td>8</td>
  </tr>
  <tr>
    <td>Oranges</td>
    <td>3</td>
    <td>3</td>
    <td>6</td>
  </tr>
  <tr>
    <td></td>
    <td>11</td>
    <td>11</td>
    <td>22</td>
  </tr>
</table>

Here's the difficulties I run into. 这就是我遇到的困难。 The fruit names, students, and fruit consumed are pulled from the database. 水果名称,学生和食用的水果均从数据库中提取。 And I don't know how many fruit rows nor student columns there will be. 而且我不知道会有多少水果行和学生专栏。

This is as far as I got: 据我所知:

<table> 
    <tr> 
        <th>Fruit</th> 
        <?php $sqlS = db("SELECT s.*, sf.consumed FROM `tbl_students` s, `tbl_students_fruit` sf WHERE s.studentid = f.studentid ORDER BY s.studentname ASC"); 
            while($student = mysql_fetch_array($sqlS)){ ?>
        <th><?php echo $student['studentname'];?></th> 
        <?php } ?> 
        <th>Total</th> 
    </tr> 


  <?php $sqlF = db("SELECT * FROM `tbl_fruit` ORDER BY fruitname ASC"); 
        while($fruit = mysql_fetch_array($sqlF)){ ?>
    <tr> 
        <td><?php echo $fruit['fruitname'];?></th> 

        <td></td> 
        <td></td>
    </tr> 
   <? } ?>

</table>

As you can see, I'm creating the Fruit rows, and Student columns. 如您所见,我正在创建“水果”行和“学生”列。 But it's incomplete. 但这是不完整的。 I've only created the column headers, and not the columns below the headers. 我只创建了列标题,而没有创建标题下面的列。 From this point I am stuck on the guts of the table. 从这一点上,我一直陷在桌子的胆量上。

I am sure arrays are the way to go with this monstrosity. 我敢肯定,数组是解决这种怪诞的方法。 But the only way my feeble brain can make this work is to have more queries, which I'm sure is a very wrong way to do this. 但是我虚弱的大脑进行这项工作的唯一方法是进行更多的查询,我相信这是一种非常错误的方法。

If there were 3 students or 15 students, I can make them appear in the table th columns, but not in rows in their columns. 如果有3个学生或15个学生,我可以使它们出现在表格的第几列中,而不是出现在其列中的行中。

How does one traverse dynamic columns this way? 如何以这种方式遍历动态列?

And if my demo above is confusing, I don't blame you! 如果上面的演示令人困惑,我也不会怪你!

well, I am assuming that your db structure is as follows: 好吧,我假设您的数据库结构如下:

[tbl_students]
studentid, studentname

[tbl_fruit]
fruitid, fruitname

[tbl_students_fruit]
id, fruitid, studentid, consumed

http://pastebin.com/CxPUeXR0 http://pastebin.com/CxPUeXR0

I haven't tested it, so good luck 我没有测试过,祝你好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM