0

我正在尝试打印类似这样的列表....

Catagory 01
   subjects1   subjects4
   subjects2   subjects5
   subjects3   subjects6

Catagory 02
   subjects1   subjects4
   subjects2   subjects5
   subjects3   subjects6

Catagory 03
   subjects1   subjects4
   subjects2   subjects5
   subjects3   subjects6

一类有自己的科目。目前我有类别 ID 并创建了一个查询来获取其类别名称和拥有的主题。这是我创建的查询..

$categoryIds = implode(',', $_SESSION['category']);

$q = "SELECT c. category_id AS ci, c.category_name AS cn, s.subject_name AS sn, s.subject_id AS si
FROM category AS c
INNER JOIN category_subjects AS cs ON cs.category_id = c.category_id
INNER JOIN subjects AS s ON s.subject_id = cs.subject_id
WHERE c.category_id IN ($categoryIds)";

$r = mysqli_query( $dbc, $q) ;

上面的查询给出了每个类别和它自己的主题。它就像一个多维数组。我的问题是我试图打印这个日期类似于上面的列表......但无法让它工作..

$catID = 假;

而 ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

echo '<div>';

if ( $catID != $row['ci']) {

    echo '<h3>Category 01: <span>' . $row['cn'] . '</span><span></span></h3>';
}

echo '<div class="container">';

    echo '<p>' . $row['sn']. '</p>'; // This subjects I need to display in a table with 2 columns..

$catID = $row['ci'];

    echo '</div>';

echo '</div>';

}

有人可以帮助我吗?非常感谢您的想法和意见。

谢谢你。

4

3 回答 3

0

由于我没有工作环境,因此无法发布代码片段,但这里有一些提示。

您需要在 order by 子句中添加类别 ID 及其名称,以确保记录不会混乱。

  1. 在循环中迭代结果。将一个临时变量(我们称之为“temp”)初始化为空白值。

  2. 要打印类别名称和 ID,您可以使用临时变量 ('temp'),该变量用于将当前类别与当前正在打印的类别进行比较。一旦类别发生变化,临时变量值就会发生变化。

  3. 主题可以存储在一个数组($subject_arr)中,并在循环内类别发生变化时在最后打印。稍后在循环之外检查主题数组是否有任何内容,如果是则打印以确保没有留下任何内容。

  4. 要以列格式打印主题(如果您的行是固定的并且列是动态的),您可以将主题数除以您想要的固定行数,这将为您提供列数。(如果有余数,只需 +1 列数)

于 2013-01-12T10:54:35.117 回答
0

如何首先使用定义如下的键创建一个数组

array("Category 1" => (
   array ( 0 => "subject1", "subject2", ...)),
      "Category 2" => (
   array ( 0 => "subject1", "subject2", ...)),
...);

可以通过

<?php
    $r = array("0" => array("ci" => "1", "cn" => "Category1", "si" => "1", "sn" =>     "subject1"),
        array("ci" => "1", "cn" => "Category1", "si" => "2", "sn" => "subject2"),
        array("ci" => "1", "cn" => "Category1", "si" => "3", "sn" => "subject3"),
        array("ci" => "1", "cn" => "Category1", "si" => "4", "sn" => "subject4"),
        array("ci" => "2", "cn" => "Category2", "si" => "5", "sn" => "subject1"),
        array("ci" => "2", "cn" => "Category2", "si" => "6", "sn" => "subject2"),
        array("ci" => "2", "cn" => "Category2", "si" => "7", "sn" => "subject3"),
        array("ci" => "2", "cn" => "Category2", "si" => "8", "sn" => "subject4"));
        //[...]

    $arr = array();

    foreach($r as $entry) {
        $cat = $entry["cn"];
        if (!array_key_exists($cat, $arr)) {
            $arr[$cat] = array();
        }
        array_push($arr[$cat], $entry);
    }

    foreach($arr as $key => $value) {
        echo $key."<br>";
        foreach($value as $infos) {
            echo "- ".$infos["sn"]."<br>";  
        }
        echo "<br>...";
    }
?>
于 2013-01-12T10:58:34.163 回答
0

一种简单的方法是创建一个主题数组,按类别分组。使用您的 category_id 作为分组的键:

$subjects = array();
foreach($r as $subject) {
    if(!isset($subjects[$subject['ci']]) $subjects[$subject['ci']] = array();
    $subjects[$subject['ci']] []= $subject;
}

foreach($subjects as $category => $values) {
    echo $category."\n";
    foreach($values as $subject)
        echo '  '.$subject."\n";
}

如果您真的需要这 2 列演示文稿,您可以拆分数组(例如使用 array_slice)并在每次迭代时输出每个值之一。

于 2013-01-12T10:58:58.267 回答