Вопрос

Please, I need help on this one. I want to build a multilevel menu, so I want to iterate through an associative array with foreach loop inside smarty template. First, I have this mysql output: enter image description here

Now I try to get an associative array from it, so I tried fetchAll(PDO::FETCH_ASSOC), but because the column names are the same, it gives me values from the right column:

Array ( [0] => Array ( [id] => 7 [name] => Beta 1-3 glucan ) [1] => Array ( [id] => 8 [name] => Okinawa Fucoidan ) 

Please if you have any ideas how to process this table to get a multidimentional menu, let me know.

Thank you.

Это было полезно?

Решение

Either use FETCH_NUM or create aliases in the query.

Другие советы

I assume you JOIN tables and in both of them you have id and name (or you JOIN the same table). What you need to do, is to use AS in your query.

For example:

Instead of: SELECT * FROM table JOIN table ON [...]

Write: SELECT t1.id AS level1_id, t1.name AS level1_name, t2.id AS level2_id, t2.name AS level2_name FROM table t1 JOIN table t2 ON [...] .

This solution will give you 4 different field names in each row (level1_id, level1_name, level2_id, level2_name).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top