Re: [PHP-DB] Re: populating multi-dimensional array

2007-04-11 Thread Matt Anderton

thanks for the advice Roberto!  the typos in (2) WERE the cause of my
problems.

I appreciate the comments on my SQL queries and table structures too.
sloppy rookie mistakes.

~ matt


On 4/11/07, Roberto Mansfield <[EMAIL PROTECTED]> wrote:


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




[PHP-DB] Re: populating multi-dimensional array

2007-04-11 Thread Roberto Mansfield
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