dourenzhou8740 2017-03-10 20:57
浏览 23
已采纳

如何创建动态表并显示结果?

I need to create a table using a variable number of records to insert on it. The number of records comes from a select in my database which returns the value of 3 columns and I store them in 3 vars. For example:

<?php
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql = $connection->query("SELECT column1, column2, column3 FROM table ");
while ($row = $sql->fetch()) {
    $var1[] = $row['column1'];
    $var2[] = $row['column2'];
    $var3[] = $row['column3'];
}
?>

Let's suppose that I have 4 rows in my table, so each of my vars are going to store 4 records and I need to create something like this in my screen:

*-----------------------------------------------------------*
| var1 record1 | var1 record2 | var1 record3 | var1 record4 |
| var2 record1 | var2 record2 | var2 record3 | var2 record4 |
| var3 record1 | var3 record2 | var3 record3 | var3 record4 |
*-----------------------------------------------------------*

How can I create something like a dynamic table to show my records in an organized way in the screen?

Edit 1:
Just to make it clearer, the table I want to display is in my php page. I don't know how many records the select will return, that's why it should be a dynamic table, but all my 3 vars will ALWAYS have the same amount of records, for example: If my table has 7 rows, then my 3 arrays are going to have 7 records each. The while loop is also working correctly.

  • 写回答

1条回答 默认 最新

  • duanli0687 2017-03-11 12:54
    关注

    Just did it with the following code:

    <?php $count = count($var1); ?>
    
    <div class='container' style="background-color: #dadfe0; text-align: center;">
    
        <?php for ($i = 0; $i < $count; $i++): ?>
            <div class='bigdiv<?php echo $i ?>' style="display: inline-block; margin: 20px">
                <div class='column1' style="margin-bottom: 15px">
                    <?php echo $var1[$i]; ?>
                </div>
                <div class='column2' style="margin-bottom: 15px">
                    <?php echo $var2[$i]; ?>
                </div>
                <div class='column3'>
                    <?php echo $var3[$i]; ?>
                </div>
            </div>
        <?php endfor; ?>
    
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度