将每个数组插入单独的div中

时间:2013-03-31 11:50:44

标签: php

在开始之前我只需要一些建议。基本上我有一系列来自mysql的书籍数据 - ID,名称,类型,等级 它们以这样的数组打印出来

Array ( [book] => Array ( [0] => 1 [1] => book one [2] => horror [3] => 8.9 ) ) 
Array ( [book] => Array ( [0] => 2 [1] =>book two [2] => Comedy [3] => 8.3 ) ) 
Array ( [book] => Array ( [0] => 3 [1] =>book three [2] => Comedy [3] => 8.7 ) ) 

等等...... 我如何将每个数组放在单独的div中?

用于获取数据的PS PHP代码

$query=mysql_query("SELECT*FROM book ");
while($row = mysql_fetch_array($query)) {
$book= Array("book" => Array($row['id'], $row['name'],$row['genre'],$row['ratings']));echo"<br>";
print_r( $book);

由于

1 个答案:

答案 0 :(得分:2)

您可以使用foreach

foreach ($your_array['book'] as $v) {

  echo "<div>";
  echo "name: ". $v[1];
  echo "category: ". $v[2];
  ...
  echo "</div>";

}

它将沿着您的数组循环,让您使用其中的值。