我有以下问题。我需要为这个结果创建分页。对于创建分页,我需要只有一个while循环(一个sql查询)。现在我因为响应div而有三个查询。我需要保留这三个div。有什么想法吗?
谢谢。
<div class="col-md-4 col-sm-12">
<?php
require 'db.php';
mysql_query("SET NAMES utf8");
$query = "SELECT * FROM dot where schva=1 order by id desc";
$result = mysql_query($query) or die(mysql_error());
$item_num = 0;
$num_records = mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) {
if ($item_num % 3 == 0) {
echo '<div class="box1 space30">
<div class="media">
<div class="media-left">
<a href="dot/' . $row['id'] . '">
<img src="img/chat.png" alt="...">
</a>
</div>
<div class="media-body">
<p><a href="dot/' . $row['id'] . '" style="color:white;">' . $row['text'] . '</a></p>
</div>
</div>
<div class="divider space20"></div>
<div class="txt">
<p>' . $row['nad'] . '</p>
</div>
</div>';
} ?>
<?php
$item_num++;
}
?>
</div>
<div class="col-md-4 col-sm-12">
<?php
require 'db.php';
$query = "SELECT * FROM dot where schva=1 order by id desc";
$result = mysql_query($query) or die(mysql_error());
$item_num = 0;
$num_records = mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) {
if ($item_num % 3 == 1) {
echo '<div class="box1 space30">
<div class="media">
<div class="media-left">
<a href="dot/' . $row['id'] . '">
<img src="img/chat.png" alt="...">
</a>
</div>
<div class="media-body">
<p><a href="dot/' . $row['id'] . '" style="color:white;">' . $row['text'] . '</a></p>
</div>
</div>
<div class="divider space20"></div>
<div class="txt">
<p>' . $row['nad'] . '</p>
</div>
</div>';
} ?>
<?php
$item_num++;
}
?>
</div>
<div class="col-md-4 col-sm-12">
<?php
require 'db.php';
$query = "SELECT * FROM dot where schva=1 order by id desc";
$result = mysql_query($query) or die(mysql_error());
$item_num = 0;
$num_records = mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) {
if ($item_num % 3 == 2) {
echo '<div class="box1 space30">
<div class="media">
<div class="media-left">
<a href="dot/' . $row['id'] . '">
<img src="img/chat.png" alt="...">
</a>
</div>
<div class="media-body">
<p><a href="dot/' . $row['id'] . '" style="color:white;">' . $row['text'] . '</a></p>
</div>
</div>
<div class="divider space20"></div>
<div class="txt">
<p>' . $row['nad'] . '</p>
</div>
</div>';
} ?>
<?php
$item_num++;
}
?>
</div>
答案 0 :(得分:0)
您可以尝试使用此代码,这会有所帮助。首先使用mysqli进行查询。
<div class="col-md-4 col-sm-12">
<?php
require 'db.php';
mysql_query("SET NAMES utf8");
$query = "SELECT * FROM dot where schva=1 order by id desc";
$result = mysql_query($query) or die(mysql_error());
$item_num = 0;
$num_records = mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) {
if ($item_num % 3 == 0) {
//Code
}elseif($item_num % 3 == 1){
//Code
}elseif($item_num % 3 == 2){
//Code
}
$item_num++;
}
?>
</div>
答案 1 :(得分:0)
或者您可以尝试使用PDO:
版本取决于数据库中的记录:
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $pdo->prepare("SELECT * FROM dot");
$sth->execute();
$data = $sth->fetchAll(PDO::FETCH_ASSOC);
?>
<?php $item_num = 0; ?>
<?php foreach ($data as $row): ?>
<div class="col-md-4 col-sm-12">
<?php if ($item_num % 3 == 0): ?>
<?php endif; ?>
<?php if ($item_num % 3 == 1): ?>
<?php endif; ?>
<?php if ($item_num % 3 == 2): ?>
<?php endif; ?>
<?php $item_num++; ?>
</div>
<?php endforeach; ?>
具有固定数量div的版本:
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $pdo->prepare("SELECT * FROM dot");
$sth->execute();
$data = $sth->fetchAll(PDO::FETCH_ASSOC);
?>
<?php for ($i = 0; $i < 3; $i++): ?>
<?php $item_num = 0; ?>
<div class="col-md-4 col-sm-12">
<?php foreach ($data as $row): ?>
<?php if ($item_num % 3 == 0): ?>
<?php endif; ?>
<?php if ($item_num % 3 == 1): ?>
<?php endif; ?>
<?php if ($item_num % 3 == 2): ?>
<?php endif; ?>
<?php $item_num++; ?>
<?php endforeach; ?>
</div>
<?php endfor; ?>
答案 2 :(得分:0)
希望它会有所帮助。
$item_num = 0;
$num_records = mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) {
if ($item_num % 3 == 0) {
echo '<div class="box1 space30">
<div class="media">
<div class="media-left">
<a href="dot/' . $row['id'] . '">
<img src="img/chat.png" alt="...">
</a>
</div>
<div class="media-body">
<p><a href="dot/' . $row['id'] . '"
style="color:white;">' . $row['text'] . '</a></p>
</div>
</div>
<div class="divider space20"></div>
<div class="txt">
<p>' . $row['nad'] . '</p>
</div>
</div>';
}elseif($item_num % 3 == 1){
echo '<div class="box1 space30">
<div class="media">
<div class="media-left">
<a href="dot/' . $row['id'] . '">
<img src="img/chat.png" alt="...">
</a>
</div>
<div class="media-body">
<p><a href="dot/' . $row['id'] . '"
style="color:white;">' . $row['text'] . '</a></p>
</div>
</div>
<div class="divider space20"></div>
<div class="txt">
<p>' . $row['nad'] . '</p>
</div>
</div>';
}elseif($item_num % 3 == 2){
echo '<div class="box1 space30">
<div class="media">
<div class="media-left">
<a href="dot/' . $row['id'] . '">
<img src="img/chat.png" alt="...">
</a>
</div>
<div class="media-body">
<p><a href="dot/' . $row['id'] . '"
style="color:white;">' . $row['text'] . '</a></p>
</div>
</div>
<div class="divider space20"></div>
<div class="txt">
<p>' . $row['nad'] . '</p>
</div>
</div>';
}
$item_num++;
}
?>