Jas0n Posted September 21, 2008 Share Posted September 21, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/125175-table-with-columns/ Share on other sites More sharing options...
.josh Posted September 21, 2008 Share Posted September 21, 2008 http://www.phpfreaks.com/forums/index.php/topic,95426.0.html Quote Link to comment https://forums.phpfreaks.com/topic/125175-table-with-columns/#findComment-646997 Share on other sites More sharing options...
benphp Posted September 21, 2008 Share Posted September 21, 2008 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("'", '&#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> Quote Link to comment https://forums.phpfreaks.com/topic/125175-table-with-columns/#findComment-647084 Share on other sites More sharing options...
Jas0n Posted September 21, 2008 Author Share Posted September 21, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/125175-table-with-columns/#findComment-647229 Share on other sites More sharing options...
Minase Posted September 21, 2008 Share Posted September 21, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/125175-table-with-columns/#findComment-647273 Share on other sites More sharing options...
Jas0n Posted September 21, 2008 Author Share Posted September 21, 2008 Just given your code a bash and it seems like its not displaying all of the images from the directory. Any Ideas? Thanks for your reply, Jase Quote Link to comment https://forums.phpfreaks.com/topic/125175-table-with-columns/#findComment-647294 Share on other sites More sharing options...
Jas0n Posted September 21, 2008 Author Share Posted September 21, 2008 Looks like only half of them Quote Link to comment https://forums.phpfreaks.com/topic/125175-table-with-columns/#findComment-647296 Share on other sites More sharing options...
Minase Posted September 21, 2008 Share Posted September 21, 2008 just remove " !== false " from while loop Quote Link to comment https://forums.phpfreaks.com/topic/125175-table-with-columns/#findComment-647316 Share on other sites More sharing options...
Jas0n Posted September 22, 2008 Author Share Posted September 22, 2008 Yup that fixed it! just one more simple thing... at the moment its doing this a b c d e f g h i j etc... for some reason its not filling the first row. Any ideas? Thanks again, Jase Quote Link to comment https://forums.phpfreaks.com/topic/125175-table-with-columns/#findComment-647410 Share on other sites More sharing options...
sasa Posted September 22, 2008 Share Posted September 22, 2008 set $i = 1 before while loop Quote Link to comment https://forums.phpfreaks.com/topic/125175-table-with-columns/#findComment-647429 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.