Need help with a dynamic table *new to PHP*

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Herzlos
Forum Newbie
Posts: 7
Joined: Wed May 27, 2009 3:17 pm

Need help with a dynamic table *new to PHP*

Post 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. :banghead:

Can anyone point me in the right direction to figure this out quicker. Any help would be appreciated.
keopz
Forum Newbie
Posts: 2
Joined: Wed May 27, 2009 3:35 pm

Re: Need help with a dynamic table *new to PHP*

Post 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>
Last edited by Benjamin on Fri May 29, 2009 10:38 am, edited 1 time in total.
Reason: Added [code=php] tags.
Herzlos
Forum Newbie
Posts: 7
Joined: Wed May 27, 2009 3:17 pm

Re: Need help with a dynamic table *new to PHP*

Post 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 />";
 
 
?>
 
Last edited by Benjamin on Fri May 29, 2009 10:39 am, edited 1 time in total.
Reason: Changed code type from text to php.
mischievous
Forum Commoner
Posts: 71
Joined: Sun Apr 19, 2009 8:59 pm

Re: Need help with a dynamic table *new to PHP*

Post 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 :dubious:

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>";
}
 
Herzlos
Forum Newbie
Posts: 7
Joined: Wed May 27, 2009 3:17 pm

Re: Need help with a dynamic table *new to PHP*

Post 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
Last edited by Benjamin on Fri May 29, 2009 10:39 am, edited 1 time in total.
Reason: Changed code type from text to php.
mischievous
Forum Commoner
Posts: 71
Joined: Sun Apr 19, 2009 8:59 pm

Re: Need help with a dynamic table *new to PHP*

Post 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
Herzlos
Forum Newbie
Posts: 7
Joined: Wed May 27, 2009 3:17 pm

Re: Need help with a dynamic table *new to PHP*

Post by Herzlos »

Still getting " Fatal error: Call to a member function result() on a non-object in" for that same line. :banghead:
Herzlos
Forum Newbie
Posts: 7
Joined: Wed May 27, 2009 3:17 pm

Re: Need help with a dynamic table *new to PHP*

Post by Herzlos »

Thanks for all the help mischievous.:drunk:

I ended up figuring out some code that works and does what I need. Just need to clean it up now.
Post Reply