I have a table (items) in my DB and it looks like this:
--------------------------------------
| id | name | price | buyer | status |
--------------------------------------
| 1 item1 10 Bob requested |
| 2 item2 20 Bob requested |
| 3 item3 10 Tom requested |
--------------------------------------
I'm trying to query the data and then sort it, lump it together, and display it by name. If 2 names are the same it'll group it into one div in my HTML, and if they are different it'll group it into a different div. Basically each group of names is its own div whether there is 0 or 20 names that are the same. So far I queried my data like so
$request_data_query = $db->prepare("SELECT * FROM items WHERE status = 'requested' GROUP BY buyer");
$request_data_query->execute();
while($fetch = $request_data_query->fetch(PDO::FETCH_ASSOC)) {
then I was trying to compare results to see if they match
if ($fetch['buyer'] === $fetch['buyer']) {
//Same name HTML code here
} else {
// Single person HTML
}
}
That code isn't sorting the way I thought it would. That prints 2 separate divs for the duplicate name and doesn't print the single person. I'm not quite sure what else to do.
You can use something like this. One that this isn't an exact match for your use, but is based on your examples.
// this goes before your loop
$previousBuyer = '';
// then inside your loop
if ($previousBuyer === $fetch['buyer']) {
//Same name HTML code here
} else {
// Single person HTML
}
$previousBuyer = $fetch['buyer'];
You'll need to account for the first buyer you check.