Re: populating multi-dimensional array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Matt Anderton wrote:
> long-time reader, first-time poster.
> 
> I am trying to use PEAR's 'hierselect' in HTML_Quickform.
> 

A few things:

1. SQL style: avoid "SELECT *" and list your field names in the specific
order you want them. What you have works, but you are assuming the field
order in your statement. It isn't self documenting either. In a week,
month, year -- will you or the next programmer know what fields *
represents?

2. This may be a typo in your post, but line 25 uses "$while" instead of
"while". Also in line 17, your use $result1, but just $result in the
next line.

3. For clearer code, use the same field name for foreign keys in your
tables. So in subcategory, use "cat_id" instead of "cat". It is also a
good idea to keep the field types identical when matching on keys.

3. Your code would be cleaner with a join:


$query1 = "SELECT cat.cat_id,
                  cat.name AS cat_name,
                  subcat.sc_id,
                  subcat.name AS subcat_name
             FROM category cat,
                  subcategory subcat
            WHERE subcat.cat = cat.cat_id
         ORDER BY cat.name, subcat.name";

$result = $db->query($query);
while ( $result->fetchInto($row) ) {
  $main[$row[0]] = $row[1];
  $secondary[$row[0]][$row[2]] = $row[3];
}


Hope that helps.
Roberto

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux