简体   繁体   中英

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

SOLVED
Sloan Trasher's answer worked great. I added another set of if-statements that checks the subcat_id and prints the right header before the according .

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).

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 . All the rows where the column subcat_id == 2 need to go in a list below that one with the header Tablets . Both lists need to be sorted by the primary key in ascending order.

Using PDO::FETCH_GROUP and PDO::FETCH_COLUMN like this:

 $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:

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:

 $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 . 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).
To me it looks like the for loop is iterated correctly but the internal array pointer isn't advanced by one? 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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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