gpt4 book ai didi

php - 每两行将返回的 sql 行拆分为多个新表行

转载 作者:行者123 更新时间:2023-11-29 11:04:45 24 4
gpt4 key购买 nike

我目前在一个表中有六个类别列表,我当前使用以下代码显示它们。因此,下面我将显示所有六个类别及其中的记录。

我使用相同的代码三次,因此第一组显示前两个类别,第二组显示第三和第四类别,最后一组代码显示类别五和六。

我想做的只是拥有一组下面的代码而不是三个,它将显示我的所有类别,每行两个,而不是我手动更改下面的代码以适应我的添加和删除类别。

我将如何实现这一点,因为我能做的最好的事情就是让所有类别出现在一行上,我将如何分割它,以便我可以选择每两个类别进入一个新行,这样我就可以出现无限的类别,而无需更改代码来始终容纳它们?

        $uid = get_current_user_id();
$stn_added_by = "";




echo "<table class='' cellspacing='0' width='100%'><tr>";
$sql3 = mysqli_query($con, "SELECT ID, category FROM knowledgebase_category ORDER BY category LIMIT 2");
while ($row3 = $sql3->fetch_assoc()){

$kbc_ID = $row3['ID'];
$kbc_category = $row3['category'];

echo "<td width='50%' valign='top'><h4 class='heading'>" . $row3['category'];
echo "</h4><br><table class='' cellspacing='0' width='100%'>";
$sql3a = mysqli_query($con, "SELECT kb_ID, kb_subject FROM knowledgebase_items WHERE kb_visible = 'Yes' and kb_category = '$kbc_ID' ORDER BY kb_subject LIMIT 5");
while ($row3a = $sql3a->fetch_assoc()){

echo "<tr><td width='50%'><blockquote class='message'><a class='hover' href='" . $row3a['kb_ID'] . "'>" . $row3a['kb_subject'] . "</a></blockquote></td></tr>";
}

echo "<tr><td width='50%'><span class='glyphicon glyphicon-forward' aria-hidden='true'></span> <a class='hover' href='category-" . $kbc_ID . "'>View All Items</a><br><br></td></tr>";
echo "</table>";

echo "</td>";
}
echo "</tr></table>";








echo "<table class='' cellspacing='0' width='100%'><tr>";
$sql3 = mysqli_query($con, "SELECT ID, category FROM knowledgebase_category ORDER BY category LIMIT 2,2");
while ($row3 = $sql3->fetch_assoc()){

$kbc_ID = $row3['ID'];
$kbc_category = $row3['category'];

echo "<td width='50%' valign='top'><h4 class='heading'>" . $row3['category'];
echo "</h4><br><table class='' cellspacing='0' width='100%'>";
$sql3a = mysqli_query($con, "SELECT kb_ID, kb_subject FROM knowledgebase_items WHERE kb_visible = 'Yes' and kb_category = '$kbc_ID' ORDER BY kb_subject LIMIT 5");
while ($row3a = $sql3a->fetch_assoc()){

echo "<tr><td width='50%'><blockquote class='message'><a class='hover' href='" . $row3a['kb_ID'] . "'>" . $row3a['kb_subject'] . "</a></blockquote></td></tr>";
}

echo "<tr><td width='50%'><span class='glyphicon glyphicon-forward' aria-hidden='true'></span> <a class='hover' href='category-" . $kbc_ID . "'>View All Items</a><br><br></td></tr>";
echo "</table>";

echo "</td>";
}
echo "</tr></table>";







echo "<table class='' cellspacing='0' width='100%'><tr>";
$sql3 = mysqli_query($con, "SELECT ID, category FROM knowledgebase_category ORDER BY category LIMIT 4,2");
while ($row3 = $sql3->fetch_assoc()){

$kbc_ID = $row3['ID'];
$kbc_category = $row3['category'];

echo "<td width='50%' valign='top'><h4 class='heading'>" . $row3['category'];
echo "</h4><br><table class='' cellspacing='0' width='100%'>";
$sql3a = mysqli_query($con, "SELECT kb_ID, kb_subject FROM knowledgebase_items WHERE kb_visible = 'Yes' and kb_category = '$kbc_ID' ORDER BY kb_subject LIMIT 5");
while ($row3a = $sql3a->fetch_assoc()){

echo "<tr><td width='50%'><blockquote class='message'><a class='hover' href='" . $row3a['kb_ID'] . "'>" . $row3a['kb_subject'] . "</a></blockquote></td></tr>";
}

echo "<tr><td width='50%'><span class='glyphicon glyphicon-forward' aria-hidden='true'></span> <a class='hover' href='category-" . $kbc_ID . "'>View All Items</a><br><br></td></tr>";
echo "</table>";

echo "</td>";
}
echo "</tr></table>";

下面是上面的代码在我的网站上的样子。

This is what the above code looks like on my website

谢谢

最佳答案

尝试这个,根据您的设计使用 2 个 50% 宽度的 div,如果您将来需要 3 或 4 列,您可以调整。甚至没有必要有类别?!!

//显示所有类别并将两个类别分成每一行

  echo "<div class='' cellspacing='0' width='100%'>";
$sql3 = mysqli_query($con, "SELECT ID, category FROM knowledgebase_category ORDER BY category");
while ($row3 = $sql3->fetch_assoc()){

$kbc_ID = $row3['ID'];
$kbc_category = $row3['category'];

echo "<div width='50%' valign='top' style='float:left;'><h4 class='heading'>" . $row3['category'] . "</h4>";

echo "<table class='' cellspacing='0' width='100%'>";
$sql3a = mysqli_query($con, "SELECT kb_ID, kb_subject FROM knowledgebase_items WHERE kb_visible = 'Yes' and kb_category = '$kbc_ID' ORDER BY kb_subject LIMIT 5");
while ($row3a = $sql3a->fetch_assoc()){
echo "<tr><td width='50%'><blockquote class='message'><a class='hover' href='" . $row3a['kb_ID'] . "'>" . $row3a['kb_subject'] . "</a></blockquote></td></tr>";
}
echo "<tr><td width='50%'><span class='glyphicon glyphicon-forward' aria-hidden='true'></span> <a class='hover' href='category-" . $kbc_ID . "'>View All Items</a><br><br></td></tr>";
echo "</table>";

echo "</div>";
}
echo "</div>";

关于php - 每两行将返回的 sql 行拆分为多个新表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41600364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com