Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a simple table - help please!

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Apologies if the code below looks somewhat jumbled. Cut and paste problems ) Basically what it does is reads all the .htm files in a directory and displays them on a php page (with different names). At the moment it is only displaying them in one column (each file a different row), but I'd like to be allowed to set the number of columns, e.g. 6. I thought I had this set up but it isn't working. Any ideas? Where do I put the <td> and <tr> tags??

thanks,
Gizmo

Code:
<?
/* DECLARATIONS */
  $col = 1;
  $numcols = 6;
$p = &quot;results&quot;;
$d = opendir($p);

$f = readdir($d);

echo(&quot;<p align='center' class=gen><u><b>Results</b> (please click on a link below)</u><br> <br><table cellSpacing=2 cellPadding=1 bgColor=#e6e6e6 border=0>&quot;);

  while($f !== false)
    {
    if($f != &quot;.&quot; and $f != &quot;..&quot; and $f != &quot;index.htm&quot;) {
  if($col ==1) {
  echo(&quot;<tr>&quot;);

    $prettyname = $f;
  $prettyname = eregi_replace(&quot;jan&quot;, &quot;January &quot;, $prettyname); }
  $prettyname = eregi_replace(&quot;feb&quot;, &quot;February &quot;, $prettyname);
  $prettyname = eregi_replace(&quot;mar&quot;, &quot;March &quot;, $prettyname);
  $prettyname = eregi_replace(&quot;apr&quot;, &quot;April &quot;, $prettyname);
  $prettyname = eregi_replace(&quot;may&quot;, &quot;May &quot;, $prettyname);
  $prettyname = eregi_replace(&quot;jun&quot;, &quot;June &quot;, $prettyname);
  $prettyname = eregi_replace(&quot;jul&quot;, &quot;July &quot;, $prettyname);
  $prettyname = eregi_replace(&quot;aug&quot;, &quot;August &quot;, $prettyname);
  $prettyname = eregi_replace(&quot;sep&quot;, &quot;September &quot;, $prettyname);
  $prettyname = eregi_replace(&quot;oct&quot;, &quot;October &quot;, $prettyname);
  $prettyname = eregi_replace(&quot;nov&quot;, &quot;November &quot;, $prettyname);
  $prettyname = eregi_replace(&quot;dec&quot;, &quot;December &quot;, $prettyname);
  $prettyname = eregi_replace(&quot;.htm&quot;, &quot;&quot;, $prettyname);


  echo(&quot;<td bgColor=white valign='center' align='center'>&quot;); echo(&quot;  <a href=results/$f>$prettyname</a>  &quot;);};
  $f = readdir($d) ;  }
 echo(&quot;</td>&quot;);


        $col++;

        if($col-1 == $numcols) {
        echo(&quot;</tr>&quot;);
        $col=1;
        }
     // }
      echo(&quot;</tr></table></p>&quot;);
 closedir($d);
 ?>
 
This script should give you an idea where to go. It takes the elements of $dataArray and prints them in a table, column first.

Code:
<?php
print &quot;<html><body>&quot;;
$dataArray = array (&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;e&quot;,&quot;f&quot;,&quot;g&quot;,&quot;h&quot;,&quot;i&quot;,&quot;j&quot;,&quot;k&quot;,&quot;l&quot;,&quot;m&quot;,&quot;n&quot;,&quot;o&quot;,&quot;p&quot;,&quot;q&quot;);

$numCols   = 3;
$numRows = ceil (count($dataArray) / $numCols);

print &quot;<table border=1>\n&quot;;

for ($rowCount = 0; $rowCount < $numRows; $rowCount++)
{
	print &quot;<tr>\n&quot;;
	for ($colCount = 0; $colCount < $numCols; $colCount++)
	{
		print &quot;<td>&quot;;
		$arrayIndex = $numRows * $colCount + $rowCount;
		if (isset($dataArray[$arrayIndex]))
		{
			print $dataArray[$arrayIndex];
		}
		else
		{
			print &quot; &quot;;
		}
		print &quot;</td>&quot;;
	}
	print &quot;</tr>\n&quot;;
}

print &quot;</table></body></html>&quot;;
______________________________________________________________________
Perfection in engineering does not happen when there is nothing more to add.
Rather it happens when there is nothing more to take away.
 
hello!!

I gave it a go, but I'm a little confused with regards to $numrows in my case. What am I counting (as it is not an array like the example above - or is it?)? I used the code above with my variables and instead of one long column, I now have one long row...

any ideas?

thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top