Re: amount of elements in an array ? - building dynamic html tables for sets of MySQL results
| From: | Nick | Date: | Thu, 13 Jul 2000 09:53:27 +0000 |
| Subject: | Re: amount of elements in an array ? - building dynamic html tables for sets of MySQL results | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-6401@lists.php.net to get a copy of this message | ||
RE: amount of elements in an array ? - building dynamic html tables for sets of MySQL resultshi,
just a short note to say thanks for your email.....
however, i have only been programming for 4 weeks :( and there are a couple of elements i havent
understood in your script...
*what is a 2D array and how to I read my results into one ? What is the difference between a 2D
array and the array returned by mysql_fetch_array ?
* I don't understand what some of the arrays represent: $data (how do i use/make this ?); why
do you "reset ($ary)" so many times ? Isn't that the results array ?
Are $col, $key, $row predefined mysql result variables ? As in, do i need to set any of these
variables elsewhere or will the script know what i mean when i use these variables?
i think that's about it.....
thanks a lot for taking the time to answer my original query, i appreciate it and have already
learnt quite a lot from your script (like using $table / return $table / echo to print the table
out...). thanks for bearing with my beginners' questions :p
cheers,
nick
----- Original Message -----
From: Nold, Mark
To: 'Nick' ; php general mailin list
Sent: Wednesday, July 12, 2000 2:06 AM
Subject: RE: amount of elements in an array ? - building dynamic html tables for sets of MySQL
results
---------------------------------------------------------------------------------------------
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