我正在设计一个简单的博客,首先我读了6条记录(图片+标题),其余的我想读7到10。 首先我使用此代码:
$sql = "SELECT * FROM blogcontents ORDER BY bid DESC";
$result = mysql_query($sql) or die("Error Query [" . $sql . "]");
$num_rows = mysql_num_rows($result);
for ($i = 0; $i < 6; $i++)
{
$row = mysql_fetch_array($result);
echo '<li class="navblog"><a href="blogpage.php?id= ' . $row['bid'] . '"><div class="blogwrap"><img class="img-thumbnail img-responsive bphoto" src="images/blog/' . $row['btimg'] . '" alt="Smiley face" height="200" width="300"><span class="sizeoftitle">' . $row['btitle'] . '</span></div></a></li>';
}
但我不知道如何从7到10阅读。
答案 0 :(得分:0)
您可以使用limit和offset(而不是循环限制)来检索所需的行 有6行的第一次选择
"SELECT * FROM blogcontents ORDER BY bid DESC limit 6 ";
和第二个选择,其他4行从5开始(offeset为0)
"SELECT * FROM blogcontents ORDER BY bid DESC limit 5,4 ";
或在你的第一次循环后,其他人
while ( $row = mysql_fetch_array($result))
{
echo '<li class="navblog"><a href="blogpage.php?id= ' . $row['bid'] . '"><div class="blogwrap"><img class="img-thumbnail img-responsive bphoto" src="images/blog/' . $row['btimg'] . '" alt="Smiley face" height="200" width="300"><span class="sizeoftitle">' . $row['btitle'] . '</span></div></a></li>';
}
根据您在评论中的代码,这应该有效
$sql2 = "SELECT *
FROM blogcontents
LIMIT 5, 4";
$result2 = mysql_query($sql2) or die("Error Query [" . $sql2 . "]");
你不需要循环..你可以使用
while ( $row = mysql_fetch_array($result2)) {
echo '<p><a href="blogpage.php?id= ' . $row['bid'] . '">' . $row['btitle'] . '</a></p>';
} ?>
答案 1 :(得分:0)
请查看我在那里解释我的问题的图片......
答案 2 :(得分:0)
result_array()函数将mysql结果格式化为php数组。
function result_array($sql)
{
$result = array();
$query = mysql_query($sql) or die("Error: " . mysql_error());
while ($data = mysql_fetch_array($query)) {
$result[] = $data;
}
$rows = count($result);
if ($rows) {
$total_global_rows = count($result);
$total_inner_rows = count($result[0]);
$count_total_inner_rows = $total_inner_rows / 2;
for ($i = 0; $i < $total_global_rows; $i++) {
for ($j = 0; $j < $count_total_inner_rows; $j++) {
unset($result[$i][$j]);
}
}
}
return $result;
}
$sql = "SELECT * FROM blogcontents ORDER BY bid DESC LIMIT 10";
$blogPosts = result_array($sql);
//print till 6th and break the loop
foreach ($blogPosts as $key => $row) {
echo '<li class="navblog"><a href="blogpage.php?id= ' . $row['bid'] . '"><div class="blogwrap"><img class="img-thumbnail img-responsive bphoto" src="images/blog/' . $row['btimg'] . '" alt="Smiley face" height="200" width="300"><span class="sizeoftitle">' . $row['btitle'] . '</span></div></a></li>';
if ($key > 5) {
break;
}
}
//print from 7th to the end
foreach ($blogPosts as $key => $row) {
if ($key > 5) {
echo $row['btitle'];
}
}