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

时间:2017-01-11 20:44:15

标签: php mysql

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

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

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

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

        $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

由于

1 个答案:

答案 0 :(得分:1)

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

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

  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>";