RE: amount of elements in an array ? - building dynamic html tabl es for sets of MySQL results

From: Date: Wed, 12 Jul 2000 01:06:49 +0000
Subject: RE: amount of elements in an array ? - building dynamic html tabl es for sets of MySQL results
Groups: php.general 
Request: Send a blank email to php-general+get-6043@lists.php.net to get a copy of this message
---------------------------------------------------------------------------- ----------------- Disclaimer: The information contained in this email is intended only for the use of the person(s) to whom it is addressed and may be confidential or contain legally privileged information. If you are not the intended recipient you are hereby notified that any perusal, use, distribution, copying or disclosure is strictly prohibited. If you have received this email in error please immediately advise us by return email at postmaster@normandy.com.au and delete the email document without making a copy. ---------------------------------------------------------------------------- ----------------- What i would suggest is that you first throw your entire results into a 2 Dimensional array, then write a function for taking a 2D array into a table. Try the following. <? function maketable($ary){ $table = "<table border=1 cellspacing=0>\n"; reset($ary); $firstrow = current($ary); $table .= "<tr>"; while(list($col,$val) = each($firstrow)){ $columns[] = $col; $name = ucwords($col); // You could do lots of nice transformations here.. $table .= "<th>$name</th>"; } $table .= "</tr>\n"; reset($ary); while(list($key,$row) = each($ary)){ $table .= "<tr>"; reset($columns); while(list($key,$col) = each($columns)){ $val = $row["$col"]; $table .= "<td>$val</td>"; } $table .= "</tr>\n"; } $table .= "</table>\n"; return $table; } // Now this comes from your DB. $data[1]["field1"] = 1; $data[1]["field2"] = 2; $data[1]["field3"] = 3; $data[2]["field1"] = 4; $data[2]["field2"] = 5; $data[2]["field3"] = 6; echo maketable($data); ?> $data should be from your DB though. This does one odd thing in that it looks at the first row to set $columns, this is so the right data from the right field goes into the correct column. I would suggest that you put something like this function into an include file that you can then call from anywhere. Once you develop it more you could make it into a class where you could set anything from background colours to css classes. NB: This method is slower (slightly.. depending on the size of your tables) than building the table as you return the data, but it is generic and reusable. (Imaging never coding a table just call maketable() and its done :) In my opinion this is more important than slight speed differences. (Our extremely complex table classes take about 5 seconds to build a 3000x20 table so hey, most are _much_ smaller) Good luck, mn -----Original Message----- From: Nick [mailto:nick@carrierworld.com] Sent: Tuesday, July 11, 2000 9:13 PM To: php general mailin list Subject: amount of elements in an array ? - building dynamic html tables for sets of MySQL results i'm trying to write a script that dynamically generates HTML tables depending on the results from an SQL query. So far, I have managed to set the HTML table headers to be the same as those in the MySQL table. [thanks to rod k -cheers !] Now i'm trying to put the results in the table. Basically, I need a MySQL function to retrieve data and put it into the table until there is no data left.... my problem basically is that I do not have a way of putting the data into the table unless i know how many elements to put in before creating a new <tr>[html table row]. Is there a way of finding out the amount of elements in an array ? Then I could do something like: while($array=mysql_fetch_row ($this->Query_ID)): $i = 0; $j = [amount of elements in $array]; echo "<tr>"; while ($i < $j): echo "<tr><td>$db->Rowdata[$i]</td> <td>"; $i++; endwhile; echo "</tr>"; any ideas ? -nick

« previous php.general (#6043) next »