如何使用smarty添加两个td后添加新的tr

时间:2016-06-16 17:52:36

标签: php html smarty

我聪明的代码是这样的我想在添加两个td后添加新的tr $ k 是这个代码的计数器变量我不能在每两个td之后添加新的tr

    {section name="sec" loop=$dataArray}
      <tr>
        {if ($k%2) ==  0}  
        <td>{$dataArray[sec].itemNm}</td>
        <td>{$dataArray[sec].rate}</td>
        <td>{$dataArray[sec].unitId}</td>
        <td>{$dataArray[sec].packing}</td>
      </tr>
      {/if}
  {/section}

My Php Select Query就像

$selectdata = "SELECT *,itemNm FROM price
                      JOIN item ON item.itemId = price.itemId
                      WHERE price.companyId = ".$companyId;
      $selectdataRes = mysql_query($selectdata);
      while($dataRow = mysql_fetch_array($selectdataRes))
      {
        $dataArray[$k]['priceId']  = $dataRow['priceId'];
        $dataArray[$k]['itemNm']  = $dataRow['itemNm'];
        $dataArray[$k]['rate']    = $dataRow['rate'];
        $dataArray[$k]['unitId']  = $dataRow['unitId'];
        $dataArray[$k]['packing'] = $dataRow['packing'];
        $k++;
      }

2 个答案:

答案 0 :(得分:0)

你增加$k mysql_fetch_array的每个循环,这意味着$k是行的编号(在HTML中也称为TR)。如果您的SQL查询返回4行,每行包含priceIditemNmrateunitIdpacking

通常在HTML中,为了在公共表中表示它,您将有4行TR(每行一个TR),每行数据都有一个TD列(每个数据一个TD)。

{section name="sec" loop=$dataArray}
    <tr>
        <td>{$dataArray[sec].itemNm}</td>
        <td>{$dataArray[sec].rate}</td>
        <td>{$dataArray[sec].unitId}</td>
        <td>{$dataArray[sec].packing}</td>
    </tr>
{/section}

如果您执行$k%2 == 0,则每两行(每两个TR)到达它,而不是每两个TD。如果你想关闭TR并且每两个TD而​​不是每两个TR打开新的TR,你必须在一个循环中处理你的TD并开始另一个这样的递增变量(例子既不是smarty也不是特别是任何语言,只是一个想法算法):

for($k=0;$k<$numLines;$k++)
{
    <tr>
        for($l=0;$l<$numColumns;$l++)
        {
            if($l > 0 && $l%2 == 0)
            {
                </tr><tr>
            }

            <td>$myData[$l]</td>
        }
    </tr>
}

希望它有所帮助:)

答案 1 :(得分:0)

Your question is very hard to understand...
Do you want to split every data row in two table rows?
Since you have a static template, you can add table rows inside your loop, whereever you want. I see no need to use multiple loops.

{section name="sec" loop=$dataArray}
  <tr>
    <td>{$dataArray[sec].itemNm}</td>
    <td>{$dataArray[sec].rate}</td>
  </tr>
  <tr>
    <td>{$dataArray[sec].unitId}</td>
    <td>{$dataArray[sec].packing}</td>
  </tr>
{/section}

if this does not fit your needs, maybe could you provide an example output as you need it?