[PHP-DB] Re: help w/ multidementional array in mysql
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'] ) ); } 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 table class=results_inner ?php foreach ($returned_rows as $value) { printf (trtd class='results_blue'%s, %s/td/trbr, $value['lastname'], $value['firstname']); next($returned_rows); printf (trtd class='results_white'%s, %s/td/trbr, $value['lastname'], $value['firstname']); } } ? /table 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. table class=results_inner ?php foreach ($returned_rows as $value) { echo 'trtd class=results_blue'.$value['lastname']. ', '.$value['firstname'].'/td/trbr'; } ? /table -- paperCrane Justin Patrin -- 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
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: ? function table (){ $rows=''; --- Put here your db connect --- while ($fields = mysql_fetch_array($query_result)){ $rows .= 'trtd class=results_blue'.$value['lastname'].', '. $value['firstname'].'/td/trbr'; } return $rows; } if ($rows == ''){ echo There is no rows; }else{ ? table class=results_inner ? echo $rows ? /table ?}? 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 table class=results_inner ?php foreach ($returned_rows as $value) { printf (trtd class='results_blue'%s, %s/td/trbr, $value['lastname'], $value['firstname']); next($returned_rows); printf (trtd class='results_white'%s, %s/td/trbr, $value['lastname'], $value['firstname']); } } ? /table 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. table class=results_inner ?php foreach ($returned_rows as $value) { echo 'trtd class=results_blue'.$value['lastname']. ', '.$value['firstname'].'/td/trbr'; } ? /table -- paperCrane Justin Patrin -- 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
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 ?php foreach ($returned_rows as $key = $value) { if ($key 1) { echo trtd class='results_blue' } else { echo trtd class='result } } ? -- Scott Phelps --- Linux: $ su - root -- Windows: (reboot) -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Fw: [PHP-DB] Re: help w/ multidementional array in mysql
Sorry, I didn't cut that right: Here it is: table class=results_inner ?php foreach ($returned_rows as $key = $value) { if ($key 1) { echo trtd class='results_blue'.$value['lastname']., .$value[firstname]./td/trbr; } else { echo trtd class='results_white'.$value['lastname']., .$value[firstname]./td/trbr; } } ? /table -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: Fw: [PHP-DB] Re: help w/ multidementional array in mysql
;-) table class=results_inner ?php foreach ($returned_rows as $key = $value) { echo trtd class='. ($key 1 ? 'results_blue' : 'results_white').'. $value['lastname']., .$value[firstname]./td/trbr; } ? /table -- paperCrane Justin Patrin -- 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
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: ;-) table class=results_inner ?php foreach ($returned_rows as $key = $value) { echo trtd class='. ($key 1 ? 'results_blue' : 'results_white').'. $value['lastname']., .$value[firstname]./td/trbr; } ? /table -- paperCrane Justin Patrin 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