Skip to content
Advertisement

PHP – show category once per group

Categories, whilst only displaying the category title once. I have one table that holds the category title (the parent) and the second table which holds the sub-category information. Although when displaying the information, I dont know how to show the category information only once.

$result_array = mysqli_query($connect, "SELECT * 
                                        FROM tbl_customer, categories 
                                        WHERE tbl_customer.category_QA = categories.id 
                                        ORDER BY Category ASC");

while ($data = mysqli_fetch_array($result_array)) {
    echo $data["Category"];    //display once for every new category
    echo $data["productName"];
}

Advertisement

Answer

You could check for change

$result_array = mysqli_query($connect,"SELECT * FROM tbl_customer, categories WHERE tbl_customer.category_QA = categories.id ORDER BY Category ASC");

$checkCategory = '';
while($data = mysqli_fetch_array($result_array)){

   if  ($checkCategory != $data["Category"]){
      echo $data["Category"]; //display once for every new category
      $checkCategory = $data["Category"];
  }
  echo $data["productName"];
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement