Page 1 of 1

Dynamic table structure

Posted: Tue Nov 26, 2002 4:07 am
by brody
I am new to php and I have developed a database with about 8 records. I want to display the data in a table that is only 3 columns wide. As the database grows - the rows of the table would increase but not the columns. As it is written below, the columns increase depending on the number of records I have. Is there an "if" statement I can implement within my code?

<?php
if (!$retid) { echo( mysql_error()); }
else {
while ($row = mysql_fetch_array($retid)) {
$logid = $row["logid"];
$actress = $row["actress"];
$resume = $row["resume"];
echo ("<td bgcolor=#2D2D2D><a href=$logid.html>$logid</a><br>$actress<br><a href=$logid-comments.html>Comments</a></td>\n");
echo ("<td width=6></td><img src=\"../images/spacer.gif\" width=6 height=1></td>\n");

}
}
echo ("</tr><tr><td colspan=5><img src=\"../images/spacer.gif\" width=1 height=10></td></tr>\n");

?>
Any help would be greatly appreciated

Posted: Tue Nov 26, 2002 6:28 am
by Johnm
brody,
Here is an example of how I handled a similar situation:

Code: Select all

&lt;?php
for($x=0;$row=ifx_fetch_row($qry,"NEXT");$x++)
{
    echo "&lt;form method=post action=single_unit_sel_sub.php&gt;\n";
    ////////////////////////////////////////
    // Echo break after every third unit. //
    ////////////////////////////////////////
    if (($x != 0) &amp;&amp; (($x % 3) == 0))
        echo "&lt;tr valign="top"&gt;\n";
        
            echo "&lt;td valign="top"&gt;\n";
            echo "&lt;table border="0" valign="top" align="center"&gt;\n";
                echo "&lt;tr valign="top"&gt;\n";
                    echo "&lt;td width="33%" align="center"&gt;\n";
                    echo "&lt;h2&gt;".$web_desc."&lt;/h2&gt;\n";
                     echo "&lt;img src="".$image."" height="190" width="190"&gt;&lt;br&gt;&lt;br&gt;\n";
                     echo "&lt;b&gt;&lt;input type=button Value=Configure    
                         onClick=this.form.submit()&gt;&lt;/b&gt;&lt;br&gt;\n";
                      include($desc_file);
                      echo "&lt;/td&gt;\n";
                  echo "&lt;/tr&gt;\n";
              echo "&lt;/table&gt;\n";
          echo "&lt;/form&gt;\n";
          
     ////////////////////////////////////////////
     // Echo break after every third unit. //
     ////////////////////////////////////////
     if (($x != 0) &amp;&amp; (($x % 3) == 0))
          echo "&lt;/tr&gt;\n";
}
   
?&gt;
Hope that helps.

John M

Don't mean to pest, but ...

Posted: Tue Nov 26, 2002 2:28 pm
by brody
Hey Johnm, thanks for the fast reply. I am new to PHP - so bare with me. I tried to implement some of your principles and now my code looks like this:

Code: Select all

&lt;?php

for ($x = 0; $row = mysql_fetch_array($retid); $x++) 
	{ 
		if (($x != 0) &amp;&amp; (($x % 2) == 0)) 
			   	$actress = $row&#1111;"actress"];
		    	$logid = $row&#1111;"logid"]; 


        echo ("&lt;td bgcolor=#2D2D2D&gt;\n");
		echo ("&lt;a href="$logid.html"&gt;$logid&lt;/a&gt;\n");
		echo ("&lt;br&gt;$actress&lt;br&gt;\n");
		echo ("&lt;a href="$logid-comments.html"&gt;Comments&lt;/a&gt;&lt;/td&gt;\n");
        echo ("&lt;td width=6&gt;&lt;img src="../images/spacer.gif" width=6 height=1&gt;&lt;/td&gt;\n");

	if (($x != 0) &amp;&amp; (($x % 2) == 0)) 
echo ("&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=5&gt;&lt;img src="../images/spacer.gif" width=1 height=10&gt;&lt;/td&gt;&lt;/tr&gt;\n");          
				 
		
}

?&gt;
I am running into two problems:
1 - After the first row has 3 columns (that is what I want) the following rows only have 2 columns.

2 - The logid variables are working (they are increasing), however, the actress variables are displaying funny.
http://www.ovadrive.com/php/cadence_table1.php

Again, any additional help is greatly appreciated!

Posted: Wed Nov 27, 2002 6:03 am
by Johnm
Try changing this:

Code: Select all

&lt;?php
if (($x != 0) &amp;&amp; (($x % 2) == 0))  

?&gt;
to this:

Code: Select all

&lt;?php
if (($x != 0) &amp;&amp; (($x % 3) == 0)) 
 
?&gt;
in both places. Also, looking at the source in the browser can also clue you in to the problem but this should work.


John M