如果有超过8个结果,如何使用php将sql结果分成两列?
我在我的网站上有一个页脚,有两个相互链接,每个链接可以容纳8个链接。如何使用php将链接分为两个部分,但前提是结果超过8个。这就是我现在必须显示的内容如果有超过8个结果,如何使用php将sql结果分成两列?,php,mysql,Php,Mysql,我在我的网站上有一个页脚,有两个相互链接,每个链接可以容纳8个链接。如何使用php将链接分为两个部分,但前提是结果超过8个。这就是我现在必须显示的内容 <?php $footer_sql = "SELECT title, link,widget FROM footer WHERE widget='1'"; $result = $db->query($footer_sql); if ($result->num_rows > 0 AND $result->num_row
<?php
$footer_sql = "SELECT title, link,widget FROM footer WHERE widget='1'";
$result = $db->query($footer_sql);
if ($result->num_rows > 0 AND $result->num_rows < 8) {
while($row = $result->fetch_assoc()) {
echo "<li><a href='".$row["link"]."'>". $row["title"] . "</a></li>";
}
elseif ($result->num_rows > 0 AND $result->num_rows > 8) {
//// This is where the thing would go to show 2 columns
}
} else {
echo "Error";
}
?>
如果有8个链接会发生什么?在elseif中生成所需的输出。我不知道如何使用那样的运算符什么运算符?您已经对其进行了编码。现在只需输出您想要制作的HTML 2列。非常感谢您的帮助
<?php
// create array of your result
$footer_sql = "SELECT title, link,widget FROM footer WHERE widget='1'";
$result = $db->query($footer_sql);
if ($result->num_rows > 0)
$result = $result->fetch_assoc();
else
echo "Error";
//create array for both section
for($i=0;$i<count($result);$i++ )
{
if(i<8)
{
$section1[] = $result[$i];
}
else
{
$section2[] = $result[$i];
}
}
//now print to your html using foreach
foreach($section1 as $sec1)
{
//print in second col
echo "<li><a href='".$sec1["link"]."'>". $sec1["title"] . "</a></li>";
}
foreach($section2 as $sec2)
{
//print in second col
echo "<li><a href='".$sec2["link"]."'>". $sec2["title"] . "</a></li>";
}
?>