...

View Full Version : Dynamic Table with Columns



rock_14
08-01-2007, 10:35 PM
I am generating a dynamic table and everything works fine except I have the columns hard coded. I need to somehow determine if a column exists in the sense that there is a value in it other than NULL and then change the code below to generate the columns dynamically.

Here is my current code:


<table border=0 align=center cellpadding=5>
<tr>
<td><b>Land</b></td>
<td><b>Acres</b></td>
<td><b>$/Acre</b></td>
<td><b>$/Mile</b></td>
<td><b>Survey</b></td>
<td><b>Township</b></td>
<td><b>Range</b></td>
<td><b>Section</b></td>
<td><b>Block</b></td>
</tr>
<tr>
<td colspan=11>
<hr>
</td>
</tr>
<tr>
<td>
<?
foreach($land_category as $item)

{
if ($item != NULL)
{
print $item.'<br>';
}
}
?>
</td>
<td>
<?
foreach($acres as $item)
{
if ($item != NULL)
{
print $item.'<br>';
}
}
?>
</td>
<td>
<?
foreach($price_per_acre as $item)
{
if ($item != NULL)
{
print $item.'<br>';
}
}
?>
</td>
<td>
<?
foreach($price_per_linear_mile as $item)
{
if ($item != NULL)
{
print $item.'<br>';
}
}
?>
</td>
<td>
<?
foreach($survey as $item)
{
if ($item != NULL)
{
print $item.'<br>';
}
}
?>
</td>
<td>
<?
foreach($township as $item)
{
if ($item != NULL)
{
print $item.'<br>';
}
}
?>
</td>
<td>
<?
foreach($range as $item)
{
if ($item != NULL)
{
print $item.'<br>';
}
}
?>
</td>
<td>
<?
foreach($section as $item)
{
if ($item != NULL)
{
print $item.'<br>';
}
}
?>
</td>
<td>
<?
foreach($block as $item)
{
if ($item != NULL)
{
print $item.'<br>';
}
}
?>
</td>
</tr>

The problem again, is that I don't want to display the column title (e.g., Acres) if no value is in the database. Can anyone offer some advice?

westmatrix99
08-01-2007, 10:51 PM
Not mine got it from here::D
http://www.codewalkers.com/c/a/Database-Code/Nested-repeat-region/
Hope this helps you.:thumbsup:

<?php
// this is an example query from the link page on my website, use your own data and don't forget to change the names of the results inside the table below
$query = "SELECT title, descr, link FROM linksite ORDER BY vote DESC";
$result = mysql_query($query);
$total_records = mysql_num_rows($result); // the number of records in your result set

$num_cols = 3; // the number of columns
$num_rows = ceil($total_records / $num_cols); // the number of rows
$num = 0; // don't change this value, this is the first number of each record inside a record set

echo "<table>\n";
// next the loop for the table rows
for ($rows = 0; $rows < $num_rows; $rows++) {
echo "<tr>\n";
// this is the loop for the table columns
for ($cols = 0; $cols < $num_cols; $cols++) {
if ($num < $total_records) { // show records if available (reduce by one because the first record is no. "0" (zero)
// first create variables with the values of the current record
$titel = mysql_result($result, $num, "title"); // you have to chenge the names here to fit your own sql statement
$description = mysql_result($result, $num, "descr");
$url = mysql_result($result, $num, "link");
echo "<td><b>".$titel."</b><br>".$description."<i><a href=\"".$url."\">Visit here!</a></i><td>\n";
} else { // show an empty cell
echo "<td>&nbsp;</td>\n";
}
$num++; // raise the number by one for the next record
}
echo "</tr>\n"; // there are no more cols in this row, close the table row tag
}
echo "</table>\n"; // end of the region = closing tag for the table element
?>

rock_14
08-01-2007, 11:00 PM
Thank you very much.

I am studying how this works, and I am curious about the



$num_cols = 3; // the number of columns


This code may be exactly what I need, but I don't think I quite understand. The table I am working with has maybe 15 columns. If I SELECT * FROM tbl
but then am only using a maximum of 8 of those columns, how can I somehow figure out to use the column. I suppose the test is if a value exists in one of the rows of the column.

Please comment if you can on this.

westmatrix99
08-01-2007, 11:11 PM
Maybe it's the amount of columns, I tried it and it did not work for my horizontal loop that I needed.
Change the number to 15 and see what happens.

Cheers
West

rock_14
08-01-2007, 11:17 PM
Thanks for replying. I will try that.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum