Re: Re: help w/ multidementional array in mysql

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 .= '<tr><td class="results_blue">'.$value['lastname'].', '.
$value['firstname'].'</td></tr><br>';
 }
 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 ("<tr><td class='results_blue'>%s, %s</td></tr><br>",
> > $value['lastname'], $value['firstname']); next($returned_rows);
> >        printf ("<tr><td class='results_white'>%s, %s</td></tr><br>",
> > $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 '<tr><td class="results_blue">'.$value['lastname'].
>     ', '.$value['firstname'].'</td></tr><br>';
> }
> ?>
> </table>
>
> --
> paperCrane <Justin Patrin>

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


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux