Salut si multumesc pentru raspunsul rapid..
am sa incerc sa dau mai multe detalii
In baza de date am tabelul asa
+----+--------+------+
| id | parinte | name |
+----+--------+------+
| 1 | 0 | val1 |
| 2 | 0 | val2 |
| 3 | 0 | val3|
| 4 | 1 | val1_1 |
| 5 | 1 | val2_1|
| 6 | 1 | val3_1 |
| 7 | 2 | val1_2|
| 8 | 2 | val2_2 |
| 9 | 2 | val3_2 |
| 10 | 3 | val1_3 |
| 11 | 3 | val2_3 |
| 12 | 3 | val3_3 |
| 13 | 0 | val4 |
+----+--------+------+
Din cate se vede val1 spre ex. este o categorie iar val3_1 este o subcategorie a lui val1
interogarea este asa
Cod: Selectaţi tot
$sql = "SELECT c.name, c.parinte, c.id FROM categorii c ORDER BY id ASC";
$result = mysqli_query($conn, $sql) or trigger_error("Query: $sql\n<br />MySQL Error: " . mysqli_error($conn));
Ca sa se vada array-ul rezultat am sa dau un var_dump()
Cod: Selectaţi tot
while ($row = mysqli_fetch_assoc($result)) {
$categorii[] = $row;
var_dump($categorii);
}
rezultatul arata ceva de genul
Cod: Selectaţi tot
array(1) { [0]=> array(3) { ["name"]=> string(4) "val1" ["parinte"]=> string(1) "0" ["id"]=> string(1) "1" } }
array(2) { [0]=> array(3) { ["name"]=> string(4) "val1" ["parinte"]=> string(1) "0" ["id"]=> string(1) "1" }
[1]=> array(3) { ["name"]=> string(4) "val2" ["parinte"]=> string(1) "0" ["id"]=> string(1) "2" } }
array(3) { [0]=> array(3) { ["name"]=> string(4) "val1" ["parinte"]=> string(1) "0" ["id"]=> string(1) "1" }
[1]=> array(3) { ["name"]=> string(4) "val2" ["parinte"]=> string(1) "0" ["id"]=> string(1) "2" }
[2]=> array(3) { ["name"]=> string(4) "val3" ["parinte"]=> string(1) "0" ["id"]=> string(1) "3" } }
................
array(81) { [0]=> array(3) { ["name"]=> string(4) "val1" ["parinte"]=> string(1) "0" ["id"]=> string(1) "1" }
[1]=> array(3) { ["name"]=> string(4) "val2" ["parinte"]=> string(1) "0" ["id"]=> string(1) "2" }
[2]=> array(3) { ["name"]=> string(4) "val3" ["parinte"]=> string(1) "0" ["id"]=> string(1) "3" }
[3]=> array(3) { ["name"]=> string(4) "val4" ["parinte"]=> string(1) "0" ["id"]=> string(1) "4" }
[4]=> array(3) { ["name"]=> string(4) "val5" ["parinte"]=> string(1) "0" ["id"]=> string(1) "5" }
[5]=> array(3) { ["name"]=> string(4) "val6" ["parinte"]=> string(1) "0" ["id"]=> string(1) "6" }
[6]=> array(3) { ["name"]=> string(6) "val1_1" ["parinte"]=> string(1) "1" ["id"]=> string(2) "21" }
[7]=> array(3) { ["name"]=> string(6) "val2_1" ["parinte"]=> string(1) "1" ["id"]=> string(2) "22" }
[8]=> array(3) { ["name"]=> string(6) "val3_1" ["parinte"]=> string(1) "1" ["id"]=> string(2) "23" }
[9]=> array(3) { ["name"]=> string(6) "val1_2" ["parinte"]=> string(1) "2" ["id"]=> string(2) "30" }
[10]=> array(3) { ["name"]=> string(6) "val2_2" ["parinte"]=> string(1) "2" ["id"]=> string(2) "31" }
[11]=> array(3) { ["name"]=> string(6) "val1_3" ["parinte"]=> string(1) "3" ["id"]=> string(2) "40" }
[12]=> array(3) { ["name"]=> string(6) "val2_3" ["parinte"]=> string(1) "3" ["id"]=> string(2) "41" }
[13]=> array(3) { ["name"]=> string(6) "val3_3" ["parinte"]=> string(1) "3" ["id"]=> string(2) "42" }
[14]=> array(3) { ["name"]=> string(6) "val4_4" ["parinte"]=> string(1) "3" ["id"]=> string(2) "43" }
....
Daca as face var_dump($row);
Cod: Selectaţi tot
array(3) { ["name"]=> string(4) "val1" ["parinte"]=> string(1) "0" ["id"]=> string(1) "1" }
array(3) { ["name"]=> string(4) "val2" ["parinte"]=> string(1) "0" ["id"]=> string(1) "2" }
array(3) { ["name"]=> string(4) "val3" ["parinte"]=> string(1) "0" ["id"]=> string(1) "3" }
array(3) { ["name"]=> string(4) "val4" ["parinte"]=> string(1) "0" ["id"]=> string(1) "4" }
array(3) { ["name"]=> string(4) "val5" ["parinte"]=> string(1) "0" ["id"]=> string(1) "5" }
array(3) { ["name"]=> string(4) "val6" ["parinte"]=> string(1) "0" ["id"]=> string(1) "6" }
array(3) { ["name"]=> string(6) "val1_1" ["parinte"]=> string(1) "1" ["id"]=> string(2) "21" }
array(3) { ["name"]=> string(6) "val2_1" ["parinte"]=> string(1) "1" ["id"]=> string(2) "22" }
array(3) { ["name"]=> string(6) "val3_1" ["parinte"]=> string(1) "1" ["id"]=> string(2) "23" }
array(3) { ["name"]=> string(6) "val1_2" ["parinte"]=> string(1) "2" ["id"]=> string(2) "30" }
array(3) { ["name"]=> string(6) "val2_2" ["parinte"]=> string(1) "2" ["id"]=> string(2) "31" }
array(3) { ["name"]=> string(6) "val1_3" ["parinte"]=> string(1) "3" ["id"]=> string(2) "40" }
array(3) { ["name"]=> string(6) "val2_3" ["parinte"]=> string(1) "3" ["id"]=> string(2) "41" }
array(3) { ["name"]=> string(6) "val3_3" ["parinte"]=> string(1) "3" ["id"]=> string(2) "42" }
array(3) { ["name"]=> string(6) "val4_3" ["parinte"]=> string(1) "3" ["id"]=> string(2) "43" }
ok din datele astea vroiam sa am un tabel su douo coloane ceva de genul
<table>
<tr>
<td>val1
val1_1
val2_1
val3_1</td> <td>val2
val1_2
val2_2</td>
</tr>
<tr>
<td>val3
val1_3
val2_3
val3_3</td> <td>val4
val1_4
val2_4
val3_4</td>
</tr>
</table>
Sper ca m-am facut inteles..
Problema este ca mie imi construieste tabelul dar in unele locuri am cate 3 coloane in altele doar una poate nu este buna metoda mea si poate stia cineva alta
Multumesc