简体   繁体   English

PHP / MySQL:在列中显示与特定值匹配的所有行

[英]PHP / MySQL: Display all rows matching specific value in column

SOLVED 解决了
Sloan Trasher's answer worked great. Sloan Trasher的回答非常有效。 I added another set of if-statements that checks the subcat_id and prints the right header before the according . 我添加了另一组if语句,用于检查subcat_id并在依据之前打印正确的标题。

if($curr_sub_cat != $row['subcat_id']) {
  if($curr_sub_cat != '') {
    echo "</div>\n";
  }
  $curr_sub_cat = $row['subcat_id'];
  if($curr_sub_cat == '1') {
    echo "<h1 class='anchor' id='smartphones'>Smartphones</h1>";
    echo "<div class='flexed-boxes'>\n";
  }
  if($curr_sub_cat == '2') {
    echo "<h1 class='anchor' id='tablets'>Tablets</h1>";
    echo "<div class='flexed-boxes'>\n";
  }
}

Original question 原始问题
I have a database table which looks something like this: 我有一个数据库表,看起来像这样:

dev_id  dev_name         cat_id  subcat_id
1       Apple iPhone 6   1       1
2       Apple iPhone 6s  1       1
3       Apple iPad Air   1       2

dev_id is the primary key, cat_id is different for each brand (Apple = 1, Samsung = 2 etc.) and subcat_id indicates if the device is either a phone (1) or a tablet (2). dev_id是主键,每个品牌的cat_id都不相同(Apple = 1,Samsung = 2,等等), subcat_id指示设备是手机(1)还是平板电脑(2)。

I want to echo all the devices of one brand which have subcat_id == 1 assigned to them in one list with the header Smartphones . 我想在标题为Smartphones的一个列表中回显分配给subcat_id == 1的一个品牌的所有设备。 All the rows where the column subcat_id == 2 need to go in a list below that one with the header Tablets . subcat_id == 2的所有行都需要进入带有标题Tablets的列表的下方。 Both lists need to be sorted by the primary key in ascending order. 这两个列表都需要按主键升序排序。

Using PDO::FETCH_GROUP and PDO::FETCH_COLUMN like this: 像这样使用PDO::FETCH_GROUPPDO::FETCH_COLUMN

 $sql = "SELECT subcat_id,dev_id FROM devices WHERE cat_id='$cat_id' ORDER BY dev_id";
 $data = $db->query($sql)->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_COLUMN);

Prints the array with all the (in this case Apple) devices nicely: 用所有(在本例中为Apple)设备很好地打印阵列:

Array
(
[0] => Array
    (
        [0] => 1
        [1] => 2
        [2] => 3
        [3] => 4
        [4] => 5
        [5] => 6
        [6] => 7
        [7] => 8
        [8] => 9
        [9] => 10
        [10] => 11
        [11] => 12
    )

[1] => Array
    (
        [0] => 13
        [1] => 14
        [2] => 15
        [3] => 16
        [4] => 17
        [5] => 18
    )
)

If I implement a for-loop as follows: 如果我实现一个for循环,如下所示:

 $cat_name_lower=strtolower($cat_name);
 $dev_name_ul=str_replace(array("$cat_name "," "),array("","_"),$row_all_dev['dev_name']);

 foreach ($data as $dev => $dev_data)
 {
   echo "<div class='flexed-boxes'>
        ";
     foreach ($dev_data as $row)
     {
       echo "<a class='singlebox trpl' href='/devices/".$cat_name_lower."/smartphone_details.php?dev_name=".$dev_name_ul."'>
              <img class='singlebox-media' src='".$cat_name_lower."/img/".$row_all_dev['dev_img']."' alt='".$row_all_dev['dev_name']."'>
              <span class='singlebox-header'>".$row_all_dev['dev_name']."</span>
            </a>
            ";
     }
   echo "</div>";
 }

I get the first entry in the table (the iPhone 6, which in my actual database has a dev_id of 6) printed times 18 - picture for clarification . 我得到表中的第一个条目(iPhone 6,在我的实际数据库中,dev_id为6)打印了18 张图片以进行说明 However, I want the rows to show up like this (the second row in the picture contains the iPhone 6, 6 Plus and 6s, which happen to have the same picture). 但是,我希望这些行像这样显示 (图片的第二行包含iPhone 6、6 Plus和6s,它们恰好具有相同的图片)。
To me it looks like the for loop is iterated correctly but the internal array pointer isn't advanced by one? 对我来说,似乎正确地循环了for循环,但是内部数组指针未提前一个? Where am I going wrong? 我要去哪里错了?

This question is a follow-up to this one 这个问题是一个跟进到这一个

Try this: 尝试这个:

$sql = "";
$sql .= "SELECT\n";
$sql .= "   subcat_id,\n";
$sql .= "   dev_id,\n";
$sql .= "   dev_img,\n";    //  Not sure if that column exists in the table???
$sql .= "   dev_name\n";
$sql .= "FROM devices\n";
$sql .= "WHERE cat_id='$cat_id'\n";
$sql .= "ORDER BY subcat_id, dev_id";

$data = $db->query($sql)->fetchAll();

$cat_name_lower = strtolower($cat_name);

echo "<div class='flexed-boxes'>\n";
$curr_sub_cat = '';
foreach($data as $row_no => $row) {
    $dev_name_ul = str_replace(array("$cat_name "," "),array("","_"),$row['dev_name']);

    if($curr_sub_cat != $row['subcat_id']) {
        if($curr_sub_cat != '') {
            echo "</div>\n";
            echo "<div class='flexed-boxes'>\n";
        }
        $curr_sub_cat = $row['subcat_id'];
    }
    echo "<a class='singlebox trpl' href='/devices/".$cat_name_lower."/smartphone_details.php?dev_name=".$dev_name_ul."'>
              <img class='singlebox-media' src='".$cat_name_lower."/img/".$row["dev_img"]."' alt='".$row['dev_name']."'>
              <span class='singlebox-header'>".$row['dev_name']."</span>
            </a>\n";
}
echo "</div>\n";
//  echo "<p>\$data:<pre>".print_r($data,true)."</pre></p>\n";

Try this and show the result in your question. 试试这个,并在您的问题中显示结果。 It's not the answer yet, but will provide the info needed to give you an answer. 这还不是答案,但是会提供给您答案所需的信息。

$sql = "";
$sql .= "SELECT\n";
$sql .= "   subcat_id,\n";
$sql .= "   dev_id,\n";
$sql .= "   dev_name\n";
$sql .= "FROM devices\n";
$sql .= "WHERE cat_id='$cat_id'\n";
$sql .= "ORDER BY subcat_id, dev_id";

$data = $db->query($sql)->fetchAll();

echo "<p>\$data:<pre>".print_r($data,true)."</pre></p>\n";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM