数字自动增加与其他功能(PHP)

时间:2016-03-27 08:20:42

标签: php while-loop

我希望queqe id自动从1开始增加

我有一个mysql表调用t1

mysql table t1数据如下:

+----------+------------------+-------------+
| ID       | Name             | Status      |
+----------+------------------+-------------+
| 1        | ABBCCC           | 1           |
| 2        | BASDASD          | 1           |
| 3        | ABBCCC           | 1           |
| 4        | ABBCCC           | 2           |
+-------------------------------------------+

我在php中循环数据如下:

$quserCA = DB::query("SELECT * FROM ".DB::table('jnbook_book')." WHERE Name = 'ABBCCC' ORDER BY id DESC LIMIT 20");
$nqCA = mysql_num_rows($quserCA);
while($ruserCA = DB::fetch($quserCA)){
    $CAlist[] = $ruserCA;
}
$x = 1;
while($x <= $nqCA) {
    //echo "The number is: $x <br>";
    $x++;
}

我在我的htm中循环这样:

<table>
  <tr>
    <td>Queqe ID</td><td>ID</td><td>Status</td>
  </tr>
  <!--{loop $CAlist $value}-->
  <tr>
    <td>{$x}</td><td>{$value[id]}</td><td>{$value[status]}</td>
  </tr>
  <!--{/loop}-->
</table>

但之后我的表输出如下所示

+---------------+-------------------+----------------+
| Queqe ID      | ID                | Status         |
+---------------+-------------------+----------------+
| 1             | 1                 | 1              |
| 1             | 3                 | 1              |
| 1             | 4                 | 2              |
+----------------------------------------------------+

实际上我想要的表输出如下

(我希望queqe id从1开始自动增加):

+----------+-----------------+-----------------+
| Queqe ID | ID              | Status          |
+----------+-----------------+-----------------+
| 1        | 1               | 1               |
| 2        | 3               | 1               |
| 3        | 4               | 2               |
+----------------------------------------------+

谢谢。

1 个答案:

答案 0 :(得分:1)

这应该是这样的:

$x = 1;
while($ruserCA = DB::fetch($quserCA)){
    // add a field, say `x` with number of a record:
    $ruserCA['x'] = $x++;

    $CAlist[] = $ruserCA;
}

在模板中:

<td>{$value[x]}</td><td>{$value[id]}</td><td>{$value[status]}</td>