populating multi-dimensional array
| From: | Matt Anderton | Date: | Wed, 11 Apr 2007 11:09:35 +0000 |
| Subject: | populating multi-dimensional array | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-43432@lists.php.net to get a copy of this message | ||
long-time reader, first-time poster.
I am trying to use PEAR's 'hierselect' in HTML_Quickform.
I have category and subcategory tables. I want to populate a
drop-down based on category, then populate the subcategory based on
the user's category selection.
mysql> desc category;
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |+--------+-------------+------+-----+---------+----------------+
| cat_id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(30) | YES | | NULL | |+--------+-------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec) mysql> desc subcategory; +-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+----------------+
| sc_id | tinyint(4) | NO | PRI | NULL | auto_increment | | name | varchar(30) | YES | | NULL | | | cat | tinyint(4) | YES | | NULL | |+-------+-------------+------+-----+---------+----------------+ 3 rows in set (0.01 sec) here's what I *THINK* the code should look like: **************************************************************************************************** 9 $form = new HTML_QuickForm('catForm','POST'); 10 11 $form->addElement('header', null, 'Add a New Category'); 12 13 $main = array(); 14 $secondary = array(); 15 16 $query1 = "SELECT * FROM category"; 17 $result1 = $db->query($query); 18 while ($result->fetchInto($row1)) {
19 $main[($row1[0])] = $row1[1];20 } 21 22 for ($i = 0; $i < count($main); $i++) {
23 $query2 = "SELECT * FROM subcategory WHERE cat = " . $i;
24 $result2 = $db->query($query2);
25 $while ($result2->fetchInto($row2)) {
26 $secondary[$i][] = $row2[1];
27 }
28 }
29
30 $sel =& $form->addElement('hierselect', 'cats', 'Categories: ');
31 $sel->setMainOptions($main);
32 $sel->setSecOptions($secondary);
33 $form->addElement('submit', 'btnSubmit', 'Submit');
34
35 $form->display();
********************************************************************************************************
but I keep getting "unexpected '[' on line 25"
is there a problem with populating the $secondary array that way?
thanks,
matt