将变量赋值给数组值并填充

时间:2014-11-19 11:18:26

标签: php mysql arrays

我在MySQL查询后的WHILE循环中有以下代码:

$values=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30);

echo "<tr>
        <td class='results'>$values</td>
        <td class='results'>working query</td>
    </tr>"

我需要$ values变量来填充,并从查询中获得的每一行增加1。

期望的结果:

1 | data
2 | data
3 | data

2 个答案:

答案 0 :(得分:0)

你没有发布while循环,所以我假设它看起来像。您只需为DB返回的每一行添加一个简单的计数器变量并增加:

$i = 1;
while ($row = mysqli_fetch_assoc($res)) {
    echo "<tr>
        <td class='results'>$i</td>
        <td class='results'>working query</td>
    </tr>";
    $i++;
}

答案 1 :(得分:0)

请使用该代码

    <table>
    <?php

        $values=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30);
    foreach($values as $v){
    echo "<tr>
            <td class='results'>$v</td>
            <td>|</td>
            <td class='results'>working query</td>
        </tr>";

    }
    ?>
    </table>