如何让这段代码每页循环10次?

时间:2010-12-14 06:11:22

标签: php mysql loops

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("dbsql", $con);

$result = mysql_query("SELECT * FROM testimonial where approved='Yes'");

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Review</th>
<th>Date</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['full_name'] . "</td>";
  echo "<td>" . $row['review'] . "</td>";
    echo "<td>" . $row['date'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

2 个答案:

答案 0 :(得分:2)

如果您正在寻找每次加载页面时循环10次的特定数据块,只需使用for()循环

for($i=0;$i<10;$i++)
{
  // block of data
}

但我认为这不是你所要求的,因为它是不切实际的(据我所见)。

要打印10个结果,请添加

limit 10

到查询结尾。但是,如果您使用的是分页,则需要在某个位置启动限制(例如限制STARTING_NUMBER,NUM_OF_RESULTS)

祝你好运!
丹尼斯M。

答案 1 :(得分:1)

替换

SELECT * FROM testimonial where approved='Yes'

$offset = 0; //calculate your offset here as per page;

SELECT * FROM testimonial where approved='Yes' limit $offset, 10