Jump to content

Table with Columns


Jas0n

Recommended Posts

Hi Guys,

 

I'm currently trying to populate a table from reading images from a folder.  Heres my code which works perfectly, however I would like to display the images like this:

 

a b c

e f g

h etc...

 

rather in this format of table..

 

a

b

c

d

e

f

g

etc...

 

I hope this makes sense!

 

<?php

$websiteurl = 'http://www.xxxxx.co.uk/beta/';
//$ProductCatFolder   = ;
//$ProductImageFolder = $product_info['products_model'];

$dir = "images/fascinators/mandy/";

// *********************************** Functions
function RemoveExtension($strName) 
{ 
     $ext = strrchr($strName, '.'); 

     if($ext !== false) 
     { 
         $strName = substr($strName, 0, -strlen($ext)); 
     } 
     return $strName; 
}  
// *********************************************

$dh = opendir($dir);

echo '<table width="450" border="0">';
while (($file = readdir($dh)) !== false) {

	$blah = $websiteurl.$dir.$file;
	$ProductColourName = RemoveExtension($file);
	$ProductColourName = ucwords($ProductColourName);

	if(($file != ".") and ($file != "..")) {

        	echo '<tr><td><img src="'. $blah .'" alt="'. $blah .'" name="'. $blah .'" width="147" height="146" /><br></td></tr>';
        	echo '<tr><td><a href="'. $blah .'">'. $ProductColourName .'</a><br></td></tr>';


	}

}
echo '</table>';
closedir($dh);

?>

 

Any help would be much appreciated!

Thanks again,

Jase

Link to comment
Share on other sites

You can use modulus for this:

 

<?php
$string = "";
$cols = "";
if (!empty($_POST['words'])) {
$string = $_POST['words'];			//declare the $string variable passed from the "words" textarea
}
if (!empty($_POST['cols'])) {
$cols = $_POST['cols'];				//declare the $cols string passed from the "cols" textbox - defines the number of table columns to draw
}
$string = str_replace("'", '&#38;#39;', $string); 	//replace apostrophes with the equivalent SGML decimal character reference code (Standard Generalized Markup Language)
$string = stripslashes($string);		//remove the annoying automatic magic_quotes_gpc slashes set in php.ini 
?>

<html>
<head>
<title> </title>
<LINK REL="stylesheet" TYPE="text/css" HREF="../style.css" />

</head>
<body onLoad="document.maketable.cols.focus();">
Finishes the table when the array isn't perfectly divisible by the number of columns. 
<form action="" method="post" name="maketable">
Enter an array: 
<?php
if($string == '')
{
print "<textarea cols='60' rows='5' name='words'>For who would bear the whips and scorns of time, th' oppressor's wrong, the proud man's contumely, the pangs of despised love, the law's delay, the insolence of office, and the spurns that patient merit of th' unworthy takes, when he himself might his quietus make with a bare bodkin? Who would fardels bear, to grunt and sweat under a weary life, but that the dread of something after death, the undiscover'd country, from whose bourn no traveller returns, puzzles the will and makes us rather bear those ills we have than fly to others that we know not of?</textarea>";
print "<br />Number of columns: <input type='text' size='1' maxlength='1' name='cols' value='6' onMouseDown=\"window.document.maketable.cols.value='';\">";
}
else
{
print "<textarea cols='60' rows='5' name='words'>$string</textarea>";
print "<br />Number of columns: <input type='text' size='1' maxlength='1' name='cols' value='$cols' onMouseDown=\"window.document.maketable.cols.value='';\">";
}
?>
<br />

<input type="submit" value="Write Table">  <input type="button" value="Clear" onclick="window.document.maketable.words.value='';document.maketable.words.focus();">  <input type="button" value="Reload Hamlet" onclick="window.document.maketable.words.value='For who would bear the whips and scorns of time, th\' oppressor\'s wrong, the proud man\'s contumely, the pangs of despised love, the law\'s delay, the insolence of office, and the spurns that patient merit of th\' unworthy takes, when he himself might his quietus make with a bare bodkin? Who would fardels bear, to grunt and sweat under a weary life, but that the dread of something after death, the undiscover\'d country, from whose bourn no traveller returns, puzzles the will and makes us rather bear those ills we have than fly to others that we know not of?';document.maketable.cols.focus();">
</form>

<table border="1">
<tr>
<?php
if($string !="") {				//if $string is not empty, then write the table - so the table isn't written when first drawing the page

$myarray = split(' ', $string);			//split the string on [space]  -isn't there a StrToArray function?
array_unshift($myarray, ' ');			//adds a blank element to the beginning of the array - necessary, due to modulus use - if zero is used, then it would bollix the first quotient
$num = count($myarray) -1;			//use minus 1 to offset the empty value at the beginning of the array 

for($i = 1; $i<= $num; $i++)			//loop while the array is less than the array count	
{
if($i%$cols != 0) 			//if the array count divided by cols has a remainder then write ONLY a table cell - 'cause if it doesn't have a remainder, then it's reached the end of the row
{
	if($i != $num)			//if this is not the last cell then print a cell
	{ 
		print "\n\t<td>$myarray[$i]</td>";  		//each individual cell
	}  //end not last cell
	else				//this IS the last cell
	{
		print "\n\t<td>$myarray[$i]</td>";  		//print the last cell
		$myremain = ($i%$cols);		  		//get the remaining number of items in the array to print
		$emptycels = $cols - $myremain;	  		//find the remaining table cells to print
			while($emptycels != 0)	  		//loop through the empty table cells until then number of cells in the row = $cols 
			{
				print "\n\t<td> </td>";  	//print remaining empty table cells
				$emptycels -= 1;		//negatively increment the empty cell count
			}  
	}  //end else last cell  		
}  //end if modulus not zero
else					//if the array can be divided by cols with no remainder, then write end of row 
{
	if ($i < $num)							//if the counter hasn't yet reached the total number of records
	{
		print "\n\t<td>$myarray[$i]</td></tr>\n<tr>"; 		//print the last cell in the row with a new row 
	}
	else								//else if the counter is equal to the total number of records
	{
		print "\n\t<td>$myarray[$i] </td>\n"; 			//print the last cell in the row without a new row 
	}
}  //end else modulus IS zero
}  //end main loop



} //end if $string is not empty
?>

</tr>    
</table>

</body>
</html>

Link to comment
Share on other sites

Hi again,

 

Thanks for the quick replys!

 

Heres what I've tried, with using code from the "Multi-column Results" snippet.

 

<?php

$websiteurl = 'http://www.xxxxx.co.uk/beta/';
$dir        = "images/fascinators/mandy/";

// *********************************** Functions
function RemoveExtension($strName) 
{ 
     $ext = strrchr($strName, '.'); 

     if($ext !== false) 
     { 
         $strName = substr($strName, 0, -strlen($ext)); 
     } 
     return $strName; 
}  
// *********************************************

$dh = opendir($dir);

$i = 0;
    $max_columns = 3;

echo '<table cellspacing="3" cellpadding="3">';

while (($file = readdir($dh)) !== false) {

	$blah = $websiteurl.$dir.$file;
	$ProductColourName = RemoveExtension($file);
	$ProductColourName = ucwords($ProductColourName);

	if(($file != ".") and ($file != "..")) {

	   // open row if counter is zero
	   if($i == 0)
	   {
		  echo '<tr><td><img src="'. $blah .'" alt="'. $blah .'" name="'. $blah .'" width="147" height="146" /></td>';

	   // increment counter - if counter = max columns, reset counter and close row
	   if(++$i == $max_columns) 
	   {
		echo '</tr>';
		   $i=0;
	   }  // end if 
	   } // end while

	   // clean up table - makes your code valid!
	   if($i < $max_columns)
	   {
			for($j=$i; $j<$max_columns;$j++)
			echo '<td> </td>';
	   }
	   
        	//echo '<tr><td><img src="'. $blah .'" alt="'. $blah .'" name="'. $blah .'" width="147" height="146" /><br></td></tr>';
        	//echo '<tr><td><a href="'. $blah .'">'. $ProductColourName .'</a><br></td></tr>';
	}	
}
echo '</tr>';
echo '</table>';
closedir($dh);

?>

 

I'm not having much luck as only one image is appearing.  Anyone have any ideas?

 

Thanks,

Jase

Link to comment
Share on other sites

lool its soo easy to do:P

 


<?php

$websiteurl = 'http://****/';
//$ProductCatFolder   = ;
//$ProductImageFolder = $product_info['products_model'];

$dir = "img/";

// *********************************** Functions
function RemoveExtension($strName) 
{ 
     $ext = strrchr($strName, '.'); 

     if($ext !== false) 
     { 
         $strName = substr($strName, 0, -strlen($ext)); 
     } 
     return $strName; 
}  
// *********************************************

$dh = opendir($dir);
$i = 0;
echo '<table width="450" border="0"><tr>';
while (($file = readdir($dh)) && readdir($dh) !== false) {
if ($i % 3 == 0) {
$end = "</tr><tr>";
} else {
$end = "";
}
	$blah = $websiteurl.$dir.$file;
	$ProductColourName = RemoveExtension($file);
	$ProductColourName = ucwords($ProductColourName);

	if(($file != ".") and ($file != "..")) {

        	echo '<td><img src="'. $blah .'" alt="'. $blah .'" name="'. $blah .'" width="147" height="146" /><br></td>';
        	echo '<td><a href="'. $blah .'">'. $ProductColourName .'</a><br></td>';
	echo $end;


	}
$i++;				
}
echo '</table>';
closedir($dh);

?>

 

 

hope it helps :P  ;D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.