祝所有人快乐周日
一个简单的问题:我正在努力以智能的方式查询我的服务器。我想在服务器中查询数据库中的最新新闻文章,并获取标题,内容,日期等。
我还想在JQuery标签小部件中输出结果,这样用户一次可以看到三个......所以,这基本上意味着在每三个结果之间我需要输出一个以确保这些东西将是在适当的时候被JQuery选中。
目前我不知道该怎么做。我不知道如何将结果分解为单独的组并在我需要的地方插入代码。我尝试构建一个数组..我试图重置指针/光标等等唉无济于事。所以我基本上多次运行查询(见下文),这显然是一种非常糟糕的方法来实现预期的结果。任何想法如何更优雅/更有效地做到这一点?
感谢您的帮助
<?php
$con = mysqli_connect('*****','*****','*****','ajax_demo');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM user2 ORDER BY id DESC LIMIT 3";
$sql2= "SELECT * FROM user2 ORDER BY id DESC LIMIT 3,3";
$sql3= "SELECT * FROM user2 ORDER BY id DESC LIMIT 6,3";
$sql4= "SELECT * FROM user2 ORDER BY id DESC LIMIT 9,3";
$sql5= "SELECT * FROM user2 ORDER BY id DESC LIMIT 12,3";
$result = mysqli_query($con,$sql);
$result2 = mysqli_query($con,$sql2);
$result3 = mysqli_query($con,$sql3);
$result4 = mysqli_query($con,$sql4);
$result5 = mysqli_query($con,$sql5);
echo "<div class=\"\" id=\"content1\">\n";
while($row = mysqli_fetch_array($result)) {
{
echo '<div class="snippet2"><a href="#item'.$row['id'].'">'.$row['Title'] . "</a><br/>";
echo $row['Date'] . "   |   ";
echo $row['Commodity']. "  | ";
echo "<img id=".$row['id']." onClick=\"reply_click(this.id)\" src=\"images/emico.png\" width=\"17\" height=\"12\" /><br />\n";
echo "<br/></div>";
}
}
mysqli_close($con);
echo "</div>\n";
echo "<div class=\"\" id=\"content2\">\n";
while($row = mysqli_fetch_array($result2)) {
{
echo '<div class="snippet2"><a href="#item'.$row['id'].'">'.$row['Title'] . "</a><br/>";
echo $row['Date'] . "   |   ";
echo $row['Commodity']. "  | ";
echo "<img id=".$row['id']." onClick=\"reply_click(this.id)\" src=\"images/emico.png\" width=\"17\" height=\"12\" /><br />\n";
echo "<br/></div>";
}
}
mysqli_close($con);
echo "</div>\n";
echo "<div class=\"\" id=\"content3\">\n";
while($row = mysqli_fetch_array($result3)) {
{
echo '<div class="snippet2"><a href="#item'.$row['id'].'">'.$row['Title'] . "</a><br/>";
echo $row['Date'] . "   |   ";
echo $row['Commodity']. "  | ";
echo "<img id=".$row['id']." onClick=\"reply_click(this.id)\" src=\"images/emico.png\" width=\"17\" height=\"12\" /><br />\n";
echo "<br/></div>";
}
}
mysqli_close($con);
echo "</div>\n";
echo "<div class=\"\" id=\"content4\">\n";
while($row = mysqli_fetch_array($result4)) {
{
echo '<div class="snippet2"><a href="#item'.$row['id'].'">'.$row['Title'] . "</a><br/>";
echo $row['Date'] . "   |   ";
echo $row['Commodity']. "  | ";
echo "<img id=".$row['id']." onClick=\"reply_click(this.id)\" src=\"images/emico.png\" width=\"17\" height=\"12\" /><br />\n";
echo "<br/></div>";
}
}
mysqli_close($con);
echo "</div>\n";
echo "<div class=\"\" id=\"content5\">\n";
while($row = mysqli_fetch_array($result5)) {
{
echo '<div class="snippet2"><a href="#item'.$row['id'].'">'.$row['Title'] . "</a><br/>";
echo $row['Date'] . "   |   ";
echo $row['Commodity']. "  | ";
echo "<img id=".$row['id']." onClick=\"reply_click(this.id)\" src=\"images/emico.png\" width=\"17\" height=\"12\" /><br />\n";
echo "<br/></div>";
}
}
echo "</div>\n";
mysqli_close($con);
?>
答案 0 :(得分:0)
您需要的只是一个查询,使用循环中的计数器跟踪
$ctr=0;
while($row = mysqli_fetch_array($result5)) {
if($ctr % 3===0){
/* do something every third pass*/
}
/* increment counter*/
$ctr++
}