Page 1 of 1
Need help with a dynamic table *new to PHP*
Posted: Wed May 27, 2009 3:30 pm
by Herzlos
I am trying to make a dynamic data table that is reusable for data being pulled from a MySQL DB. The number of columns and field names change per page I need the table on. I have figured out how to get the Table Header to fill in correctly from the DB, as well as getting the footer to span the correct number of cells. But I can't seem to get the Table Rows in the data area to work.
Can anyone point me in the right direction to figure this out quicker. Any help would be appreciated.
Re: Need help with a dynamic table *new to PHP*
Posted: Wed May 27, 2009 3:55 pm
by keopz
If you want to build a table with values from some SQL Database:
Code: Select all
<?
$query = "SELECT * FROM Table ";
$list = mysql_query($query);
?>
<table>
<?
while($row = mysql_fetch_array($list, MYSQL_ASSOC))
{
echo "
<tr>
<td>{$row['value1']}</td>
<td>{$row['value2']}</td>
<td>{$row['valueN']}</td>
</tr>
";}
?>
</table>
Re: Need help with a dynamic table *new to PHP*
Posted: Wed May 27, 2009 9:14 pm
by Herzlos
I tried running your code and I still couldn't get any Table data to show. I know there is data in the table and that I can pull data from it. I have used a few different tables with data in them.
Here is the code I am working with right now, Like I said the Header and Footer parts work fine, just not the table data section. Thanks for any help...
Code: Select all
<?php
$table= "10_XLP_Tray"; //change to table name for page
echo "<table class=\"wire\" align=\"center\">";
echo " <thead class=\"wire1\">";
echo "<tr>";
$result = mysql_query("SHOW COLUMNS FROM $table");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
print_r("<th>" . $row[Field] . "</th>");
}
}
echo "</tr>";
echo "</thead>";
echo "<tfoot class=\"wire3\">";
echo "<tr>";
$qColumnNames = mysql_query("SHOW COLUMNS FROM $table",$connection) or die("mysql error");
$numColumns = mysql_num_rows($qColumnNames);
echo "<td colspan=\"$numColumns\">The data shown is approximate and subject to standard industry tolerances.</td>";
echo "<tr>";
echo "</tfoot>";
echo "<tbody class=\"wire2\">";
echo "<tr>";
$data1 = mysql_query("SELECT * FROM $table", $connection);
$num_rows = mysql_num_rows($data1);
if (!$num_rows) {
die("database query failed: " . mysql_error());
while($row = mysql_fetch_row($data1))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
{
echo "<td>$cell</td>";
}
echo "</tr>";
}
}else{
echo "<tr><td>No Results found!</td></tr>";
}
echo "</tr>";
echo "</tbody>";
echo "</table>";
echo "<br /> <br />";
?>
Re: Need help with a dynamic table *new to PHP*
Posted: Thu May 28, 2009 12:37 am
by mischievous
Not exactly sure what your looking for... but possibly something like this? I haven't tested this code as it is almost 1am here... but this should technically run through a result set that as any amount of results and echo out all the columns for each row? not sure... test it and let me know...
Hope it works
Code: Select all
if (!$num_rows) {
die("database query failed: " . mysql_error());
} else if($num_rows > 0) {
foreach($data->result() as $cell)
{
echo "<tr>";
$cell_count = count($cell);
for($i = 0; $i <= $cell_count-1; $i++){
echo ("<td>".$cell[$i]."</td>");
}
echo "</tr>";
}
}else{
echo "<tr><td>No Results found!</td></tr>";
}
Re: Need help with a dynamic table *new to PHP*
Posted: Thu May 28, 2009 7:43 am
by Herzlos
I am getting "Fatal error: Call to a member function result() on a non-object on line 36", I am trying to figure it out. I figured I would post the code and while I am looking for the answer. If i can't figure it out hopefully someone here will know the answer. Thanks mischievous for the code and help.
From my count its this line that it seems to be having an issue with, foreach($data->result() as $cell).
Code: Select all
<?php
$table= "10_XLP_Tray"; //change to table name for page
echo "<table class=\"wire\" align=\"center\">";
echo " <thead class=\"wire1\">";
echo "<tr>";
$result = mysql_query("SHOW COLUMNS FROM $table");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
print_r("<th>" . $row[Field] . "</th>");
}
}
echo "</tr>";
echo "</thead>";
echo "<tfoot class=\"wire3\">";
echo "<tr>";
$qColumnNames = mysql_query("SHOW COLUMNS FROM $table",$connection) or die("mysql error");
$numColumns = mysql_num_rows($qColumnNames);
echo "<td colspan=\"$numColumns\">The data shown is approximate and subject to standard industry tolerances.</td>";
echo "<tr>";
echo "</tfoot>";
echo "<tbody class=\"wire2\">";
echo "<tr>";
$data1 = mysql_query("SELECT * FROM $table", $connection);
$num_rows = mysql_num_rows($data1);
if (!$num_rows) {
die("database query failed: " . mysql_error());
} else if($num_rows > 0) {
foreach($data->result() as $cell)
{
echo "<tr>";
$cell_count = count($cell);
for($i = 0; $i <= $cell_count-1; $i++){
echo ("<td>".$cell[$i]."</td>");
}
echo "</tr>";
}
}else{
echo "<tr><td>No Results found!</td></tr>";
}
echo "</tr>";
echo "</tbody>";
echo "</table>";
echo "<br /> <br />";
?>
Thanks
Re: Need help with a dynamic table *new to PHP*
Posted: Thu May 28, 2009 12:29 pm
by mischievous
Ummmm....
Code: Select all
<?php
$table= "10_XLP_Tray"; //change to table name for page
echo "<table class=\"wire\" align=\"center\">";
echo " <thead class=\"wire1\">";
echo "<tr>";
$result = mysql_query("SHOW COLUMNS FROM $table");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
print_r("<th>" . $row[Field] . "</th>");
}
}
echo "</tr>";
echo "</thead>";
echo "<tfoot class=\"wire3\">";
echo "<tr>";
$qColumnNames = mysql_query("SHOW COLUMNS FROM $table",$connection) or die("mysql error");
$numColumns = mysql_num_rows($qColumnNames);
echo "<td colspan=\"$numColumns\">The data shown is approximate and subject to standard industry tolerances.</td>";
echo "<tr>";
echo "</tfoot>";
echo "<tbody class=\"wire2\">";
echo "<tr>";
$data1 = mysql_query("SELECT * FROM $table", $connection);
$num_rows = mysql_num_rows($data1);
if (!$num_rows) {
die("database query failed: " . mysql_error());
} else if($num_rows > 0) {
foreach($data1->result() as $cell)
{
echo "<tr>";
$cell_count = count($cell);
for($i = 0; $i <= $cell_count-1; $i++){
echo ("<td>".$cell[$i]."</td>");
}
echo "</tr>";
}
}else{
echo "<tr><td>No Results found!</td></tr>";
}
echo "</tr>";
echo "</tbody>";
echo "</table>";
echo "<br /> <br />";
?>
looks like the $data variable you had the mysql query to had a 1 on the end? That variable inside the for each loop needs the 1 as well... is fixed in the code above... hope that works for ya
Re: Need help with a dynamic table *new to PHP*
Posted: Thu May 28, 2009 3:41 pm
by Herzlos
Still getting " Fatal error: Call to a member function result() on a non-object in" for that same line.

Re: Need help with a dynamic table *new to PHP*
Posted: Fri May 29, 2009 9:02 am
by Herzlos
Thanks for all the help mischievous.
I ended up figuring out some code that works and does what I need. Just need to clean it up now.