简体   繁体   中英

Creating and using associative array from MySQL Query Results - PHP

I have a sql query which selects meal options by number key. I want to associate each number key with the meal name. The below query shows just the meal numbers (which is correct) I can't match the ID's as I would get incorrect results.

Array
(
    [meal_1] => 1
    [meal_2] => 2
    [meal_3] => 1
)
Array
(
    [meal_1] => 2
    [meal_2] => 1
    [meal_3] => 2
)
Array
(
    [meal_1] => 0
    [meal_2] => 3
    [meal_3] => 3
)
Array
(
    [meal_1] => 2
    [meal_2] => 4
    [meal_3] => 0
)

But I would like to output like this

Array
(
    [meal_1] => Soup
    [meal_2] => Salmon
    [meal_3] => Torte
)
Array
(
    [meal_1] => Salad
    [meal_2] => Chicken
    [meal_3] => Tart
)
Array
(
    [meal_1] => No
    [meal_2] => Pasta
    [meal_3] => Brulee
)
Array
(
    [meal_1] => Salad
    [meal_2] => Burger
    [meal_3] => Pate
)

$result = mysql_query("SELECT Meal_table_1.meal_1, Meal_table_1.meal_2, Meal_table_1.meal_3 FROM Meal_table_1 ");
echo "<pre>";
while ($row = mysql_fetch_assoc($result)) {
print_r($row);
}
echo "</pre>";
mysql_free_result($result);

//Meal_table_1

+----+--------+-------+-------+
| ID | meal_1 | meal_2| meal_3|
+----+--------+-------+-------+
|  1 | 1      | 2     | 1     |
|  2 | 2      | 1     | 2     |
|  3 | 0      | 3     | 3     |
|  4 | 2      | 4     | 0     |
+----+--------+-------+-------+

$meal_1 = array('0' => 'No','1' => 'Soup','2' => 'Salad','3' => 'Pate');

$meal_2 = array('0' => 'No','1' => 'Chicken','2' => 'Salmon','3' => 'Pasta','4' => 'Burger');

$meal_3 = array('0' => 'No','1' => 'Torte','2' => 'Tart','3' => 'Brulee',);

// Meal_table_2 (should this be in three separate tables?)

+----+--------+--------+--------+
| ID | starter| main   | dessert|
+----+--------+--------+--------+
|  1 | Soup   | Chicken| Torte  |
|  2 | Salad  | Salmon | Tart   |
|  3 | Pate   | Pasta  | Brulee |
|  4 |        | Burger |        |
+----+--------+--------+--------+

Your query now is:

SELECT Meal_table_1.meal_1, Meal_table_1.meal_2, Meal_table_1.meal_3 FROM Meal_table_1

Your query could be:

SELECT tstarter.starter, tmain.main, tdessert.dessert
FROM Meal_table_1 meal
LEFT OUTER JOIN Meal_table_2  tstarter
ON meal.ID = tstarter.id
LEFT OUTER JOIN Meal_table_2  tmain
ON meal.ID = tmain.id
LEFT OUTER JOIN Meal_table_2  tdessert
ON meal.ID = tdessert.id

But it would be better to change the structure of meal_table_2 (and give your tables a better name too!)

+----+---------+---------+
| ID | type    | main    |
+----+---------+---------+
|  1 | starter | Soup    |
|  1 | main    | Chicken | 
|  1 | dessert | Torte   |
|  2 | starter | Salad   |
|  2 | main    | Salmon  |
|  2 | dessert | Tart    |
|  3 | starter | Pate    |
|  3 | main    | Pasta   |
|  3 | dessert | Brulee  |
|  4 | main    | Burger  |
+----+---------+---------+

Unless the ID's need to be the same, it's better to give each row its own ID. Maybe you even don't need the type anymore?

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