douxingti9307 2013-09-19 10:18
浏览 38

Smarty:数组到表

<table class="table table-striped table-framed table-centered">
    <thead>
        <tr>
            {foreach from=$columns key=num item=columninfo}
                <th><font color="black">{$columninfo.columnname}</font></th>
            {/foreach}
            <th>&nbsp;</th>
        </tr>
    </thead>
    <tbody>
        {foreach from=$rows key=num item=rowinfo}
            <tr>
                <td><font color="black">{$rowinfo}</font></td>
                <td><a href="database.php?action=viewtable&table={$tableinfo.tablename}&database={$currentdatabase}"><input class="btn btn-danger" type="button" value="Delete" onclick="return confirm('Are you sure you would like to delete this row?');"/></td></a>
            </tr>
        {/foreach}
    </tbody>
</table>

I've been trying to make it output all the data in the arrays, but the second array, $rows, does not output anything.

The first one works great, and outputs all the names of the columns.

The second array looks like this.

Array ( [0] => Array ( [ID] => 3 [Name] => Eirik [Age] => 20 ) )

What I'm trying to do is to create a database management system where you can access your databases and see tables, columns and rows. Currently I'm only missing rows to complete. It fetches all the column names, and makes the table header, then goes over to rows. The problem here is that when I'm trying to print the rows into the table, I can't use the column name to access the array, due to the column names being different for every database.

  • 写回答

1条回答 默认 最新

  • duanquan1207 2013-09-19 10:27
    关注

    You're on the right track: Just follow the same pattern used inside the first foreach.

    $rowinfo is an array, and you need to access its elements if you want to print them. Simply write $rowinfo['ID'] (or $rowinfo.ID) to access the ID element of the $rowinfo array.

    Repeat for every element you want to print.

    A side note: you can use the cleaner PHP syntax for your foreach block:

    {foreach $rows as $num => $rowinfo}
    

    is equivalent to

    {foreach from=$rows key=num item=rowinfo}
    

    but in my opinion much more readable.

    Regarding the EDIT, how to print a dynamic array which has varying columns (names and possibly their number) you can try the following approach:

     {foreach $rows as $num => $rowinfo}
          <tr>
              <td>
              {foreach $rowinfo as $key => $value}
                  {$key}: {$value} {* /*of course you'll need to decide HOW to render them */ *}
              {/foreach}
              </td>
              <td>[...]</td></a>
          </tr>
     {/foreach}
    

    Basically you're cycling all the rows you've got in your second array with the outer foreach. Inside the inner one you're cycling over the content of a single row, picking its keys (the column name) and their associated values.

    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致