I'm trying to create a schedule. By selecting the date, I will have to display by company all the products that expire at that moment, let's take for example these two databases:
Company DB
:
+------------+-----------+
| id_company | name |
+------------+-----------+
| 1 | Microsoft |
+------------+-----------+
| 2 | Apple |
+------------+-----------+
| 3 | IBM |
+------------+-----------+
Products DB
:
+-------------+----------------+---------------+---------------+
| id_products | id_pro_company | name_products | control_dates |
+-------------+----------------+---------------+---------------+
| 1 | 1 | pc | 2020-10-02 |
+-------------+----------------+---------------+---------------+
| 2 | 2 | tablet | 2020-10-10 |
+-------------+----------------+---------------+---------------+
| 3 | 3 | phone | 2030-10-10 |
+-------------+----------------+---------------+---------------+
Query :
SELECT * FROM products as p JOIN company as c on c.id_company = p.id_pro_company and p.control_dates LIKE ?
Now i have all record i need, but how can i group the ouput in php. example:
$date = '%2020-10%';
$query = $mysqli -> prepare('SELECT * FROM products as p JOIN company as c on c.id_company = p.id_pro_company and p.control_dates LIKE ?');
$query -> bind_param('s',$date);
$query -> execute();
$result = $query -> get_result();
if($result -> num_rows > 0){
//how group for company then for products then output the record?
}else{
echo 'Nothing to do';
}
As you can see in the comment in the code I don't understand how to group companies with products inside.
Example output:
1. Microsoft
- PC
* 1 - pc - 2020-10-02
2. Apple
-tablet
* 2 - tablet - 2020-10-10