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