Re: [PHP-DB] Re: help w/ multidementional array in mysql

2004-02-25 Thread Scott
X-Mailer: Sylpheed version 0.9.8claws (GTK+ 1.2.10; i686-pc-linux-gnu)
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit

On Wed, 25 Feb 2004 15:39:17 -0800
Justin Patrin <[EMAIL PROTECTED]> wrote:

> ;-)
> 
> 
> foreach ($returned_rows as $key => $value)
> {
>echo "".
>  $value['lastname'].", 
> ".$value[firstname]."";
>   }
> 
>  ?>
> 
> 
> -- 
> paperCrane 

Now how elegant is that?
You da man!

-- 
Scott 

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



Re: [PHP-DB] Re: help w/ multidementional array in mysql

2004-02-25 Thread PHELPS, SCOTT
On Wed, 25 Feb 2004 14:56:53 -0400
Luis M Morales C <[EMAIL PROTECTED]> wrote:

Thank you so much Justin and Luis. You guys are awesome.  
You both helped me tremendously!

BTW, I figured out a way to make the tables show the $value results in alternating 
colors.  
I just use a bitwise AND to  test if the $key is odd.  If so make the cell blue!
Heres: the code:
<<< snip-snip >>>
 $value)
{
if ($key & 1)
{
echo "
}
else
{
echo "

Re: [PHP-DB] Re: help w/ multidementional array in mysql

2004-02-25 Thread Luis M Morales C
This is an great aproach to solve your problem but i suggest some things to 
obtain an better performance on your apps.

See my comments bellow

On Wednesday 25 February 2004 14:24, Justin Patrin wrote:
> Scott Phelps wrote:
> > Thanks in advance for reading this:
> >
> > I am trying to take a query based on user input and selectively output
> > fields to a table.
> >
> > Heres the query code:
> > <<< snip - snip>>>
> > $query_result = mysql_query($query);
> > while ($field = mysql_fetch_array($query_result))
> > {
> > $returned_rows =
> > array(
> > array(
> > "id"=>$field['id'],
> > "lastname"=>$field['lastname'],
> > "firstname"=>$field['firstname'],
> > "yearhired"=>$field['yearhired'],
> > "yeardepart"=>$field['yeardepart']
> > )
> > );
> > }
Here you can do this:
while ($fields = mysql_fetch_array($query_result))
{
  $array_rows[] = $fields 
}

So it's better if you build the html over same loop and store the string into 
accumulative function for example:

'.$value['lastname'].', '.
$value['firstname'].'';
 }
 return  $rows;
}

if ($rows == ''){
  echo "There is no rows";
}else{
?>

   



> > <<< snip - snip>>>
> >
> > Now what I want to do is put only the lastname and firstname in a
> > small table with alternating colors:
> >
> > Like this:
> >
> > -
> >
> > |   $lastname, $firstname  |   ---> color=blue
> >
> > -
> >
> > |   $lastname, $firstname  |   ---> color=white
> >
> > -
> >
> > |   $lastname, $firstname  |   ---> color=blue
> >
> > -
> >
> > Heres the code:
> > <<< snip - snip>>>
> > 
> >  > foreach ($returned_rows as $value)
> >  {
> >printf ("%s, %s",
> > $value['lastname'], $value['firstname']); next($returned_rows);
> >printf ("%s, %s",
> > $value['lastname'], $value['firstname']);
> >   } }
> > ?>
> > 
> > <<< snip - snip>>>
> >
> > The problem is that it only prints one name.  Also next() doesn't seem to
> > be advancing the table so I am getting duplicates of the one name it does
> > print to the table
> >
> > Thanks.
>
> First of all,
>
> $returned_rows = array(array(...));
>
> Will just give you an array with one thing in it, however may rows are
> returned.
>
> Second, you don't have to recreate the returned row. This should work
> much better:
>
> $query_result = mysql_query($query);
> $returned_rows = array();
> while ($field = mysql_fetch_array($query_result)) {
>$returned_rows[] = $field;
> }
>
> Third, foreach() is already looping through your array, so you don't
> have to use next(). In addition, foreach() loops through a *copy* of the
> array, so using next() doesn't actually do anything to the values you're
> using. Basically what you're doing in that loop is looping through the
> original array *and* a copy of it. You need to just loop once and output
> one record in the loop.
>
> Fourth, printf() is pretty slow compared ot print and echo. It is really
> only useful if you need extended formatting. If all you're doing is
> putting strings in another string, it's much cleaner and easier to read
> using print or echo.
>
> 
>  foreach ($returned_rows as $value) {
>echo ''.$value['lastname'].
> ', '.$value['firstname'].'';
> }
> ?>
> 
>
> --
> paperCrane 

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