my god man, do you know what indenting is?

that confused the heck out of me.

Jim Lucas

----- Original Message ----- 
From: "David Otton" <[EMAIL PROTECTED]>
To: "Tom Wollaston" <[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>
Sent: Saturday, October 25, 2003 8:53 AM
Subject: Re: [PHP] Trying to organise an array


> On Sat, 25 Oct 2003 09:17:46 +0100, you wrote:
>
> >I have made this function which should be quite simple but doesn't seem
to
> >do what it is meant to.
> >What I want it to do is create an array where there is a numbered key
(the
> >rows of the table) and the colume headings as the second key.
>
> First, you can't have two keys on an index (AFAIK). What you're creating
is
> an array-of-arrays.
>
> Better to create $n when you /output/ the array.
>
> >It doesn't seem to work. If I set $pid=0 then I get no results (even
though
> >there are such values in the table), and when $pid=1 I get the results I
> >expect but they are not sorted in the way I expected.
>
> Use an ORDER BY clause in the SQL to sort the results.
>
> >function getinfo($pid)
> >{
> > $query = "SELECT name,id,parentid FROM ubsc_menu WHERE parentid='$pid'
";
> > $result = mysql_query ($query) or die ("Query failed");
>
> Your code as written assumes that a database connection exists. This is
bad,
> because your function should be a black-box, not make assumptions about
the
> environment it's running in.
>
> > $row=mysql_fetch_array($result,MYSQL_ASSOC);
>
> You're throwing away the first row.
>
> > $n = 1;
> > while ($row=mysql_fetch_array($result,MYSQL_ASSOC))
>
> 'cos it gets overritten first time round the loop
>
> >  foreach ($row as $colname => $value)
>
> The foreach in the while... I have no idea what that's going to do. I
> /think/ you're unwrapping the database row into multiple array lines.
>
> Try this rewrite (warning: completely untested)
>
> <?php
> function getinfo ($pid, $dbconn)
> {
> $result = array();
>
> if (!is_int ($pid)) // to stop SQL injection
> {
> return (NULL);
> }
>
> /* no need to get parentid - you already have it */
> $sql = "SELECT id, name FROM ubsc_menu WHERE parentid = $pid ORDER
> BY name";
>
> /* if there's an error while running the query, return NULL */
> if (($rs = mysql_query ($sql, $dbconn)) == FALSE)
> {
> return (NULL);
> }
>
> while ($row = mysql_fetch_array ($rs, MYSQL_ASSOC))
> {
> $result[$row['id']] = $row['name'];
> }
>
> /* at this point we have a dictionary where
> $result['id'] = name */
>
> /* no results found? return FALSE */
> if (sizeof ($result) == 0)
> {
> return (FALSE);
> }
>
> return ($result);
> }
>
> /* change these to the correct values */
> $dbname = 'ubsc';
> $pid = 1;
>
> /* attempt to connect to database */
> if (($dbconn = mysql_connect ()) == FALSE)
> {
> die ("<p>There was a problem</p>\r\n");
> }
>
> $result = getinfo ($pid, $dbconn);
>
> if ($result === NULL)
> {
> die ("<p>There was a problem</p>\r\n");
> }
>
> if ($result !== FALSE)
> {
> $n = 0;
> foreach ($result as $id => $name)
> {
> echo ("<p>" . $n+1 . ". $id: $name</p>\r\n");
> $n+=1;
> }
> } else {
> echo ("<p>No results found</p>\r\n");
> }
> ?>
>
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

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

Reply via email to