为每个数组项添加值

时间:2012-07-06 06:10:37

标签: php

我试图在数组内的每个帖子数据中放一个timeago,post数据来自mysql表。代码如下:

  $sql = "SELECT * FROM posts ORDER BY time DESC LIMIT 8"; 
  $query = mysql_query($sql)or die(mysql_error());

  $count = 0; // Initialize counter
$rows = array();
while($row = mysql_fetch_array( $query )) {
    $rows[++$count] = $row;
  }

行已放入数组中。 每个内部都有:user,postcontent,time(我用来计算timegao)

2 个答案:

答案 0 :(得分:0)

如下所示,PS:$counter不是必需的。

$rows = array();
while($row = mysql_fetch_array( $query )) {
  $row['timeago'] = timeago($row['time']);
  $rows[] = $row;
}

答案 1 :(得分:0)

$rows = array();
while($row = mysql_fetch_array( $query )) {
  $rows .= timeago($row['time']);
}