SkyRanger Posted February 3, 2008 Share Posted February 3, 2008 Here is the situation. I am trying to use a php multi column to display some images that are uploaded to a directory without using a mysql backend. I have figured out how to do it with mysql: <?php //set the number of columns $columns = x; mysql_connect('localhost','',''); mysql_select_db('test'); $query = "SELECT * FROM table ORDER BY id"; $result = mysql_query($query); //we add this line because we need to know the number of rows $num_rows = mysql_num_rows($result); echo "<TABLE BORDER=\"0\">\n"; //changed this to a for loop so we can use the number of rows for($i = 0; $i < $num_rows; $i++) { $row = mysql_fetch_array($result); if($i % $columns == 0) { //if there is no remainder, we want to start a new row echo "<TR>\n"; } echo "<TD>" . $row['stuff'] . "</TD>\n"; if(($i % $columns) == ($columns - 1) || ($i + 1) == $num_rows) { //if there is a remainder of 1, end the row //or if there is nothing left in our result set, end the row echo "</TR>\n"; } } echo "</TABLE>\n"; ?> But instead of doing it that way, I am trying to figure out how to get php to count the images in the directory and display them in columns. Is there a way to do this and if so can somebody please help. I can get them out of the directory and display on the page already using this: <?php function strip_ext($name) { $ext = strrchr($name, '.'); if($ext !== false) { $name = substr($name, 0, -strlen($ext)); } return $name; } function removeHyphen($filename) { $target = $filename; $patterns[0] = '/-/'; $patterns[1] = '/_/'; $replacements[0] = ' '; $replacements[1] = ' '; $filename = preg_replace($patterns, $replacements, $target); return $filename; } function capFirstWord($word) { $cap = $word; $cap = explode(' ', $cap); foreach($cap as $key => $value) { $cap[$key] = ucfirst($cap[$key]); } $word = implode(' ', $cap); return $word; } function formatFile($name) { $name = strip_ext($name); $name = removeHyphen($name); $name = capFirstWord($name); return $name; } $mydir = dir('./displayimages'); while(($file = $mydir->read()) !== false) { // Security - remove "." and ".." files (directories) if ($file != "." && $file != "..") { // Output. This is what is actually outputed by the script and that shows // up on the screen (browser). echo "<a href='./displayimages/$file'><img border='0' src='./displayimages/$file' width=\"77\" height=\"102\"></a>".formatFile($file)."</a><br>"; } } $mydir->close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/89145-solved-question/ Share on other sites More sharing options...
thepresidentis Posted February 3, 2008 Share Posted February 3, 2008 are you trying to have table rows and columns for your images? is that what you are looking to do? <? //the rest of your script ?> // end php code to create a table <table width="100%" border="1"> <tr> <? //restart php code echo "<TD><a href='./displayimages/$file'><img border='0' src='./displayimages/$file' width=\"77\" height=\"102\"></a>".formatFile($file)."</a></TD> //the rest of your code ?> if that is what ou are trying to do you need to create a table first then echo your td columns. you will problably have to play with it to get it to work for your needs, but that is the basic way to do that. Quote Link to comment https://forums.phpfreaks.com/topic/89145-solved-question/#findComment-456549 Share on other sites More sharing options...
SkyRanger Posted February 3, 2008 Author Share Posted February 3, 2008 What I need it to do, is have php count the number of images in the directory, and then place them in the table for example: 10 Images, would display 3 columns wide and 4 rows If it was say 6 images then it would show 3 columns and 2 rows. Quote Link to comment https://forums.phpfreaks.com/topic/89145-solved-question/#findComment-456776 Share on other sites More sharing options...
SkyRanger Posted February 3, 2008 Author Share Posted February 3, 2008 ~bump~ Quote Link to comment https://forums.phpfreaks.com/topic/89145-solved-question/#findComment-456971 Share on other sites More sharing options...
Barand Posted February 3, 2008 Share Posted February 3, 2008 use glob() to pull the filenames into an array. Then it's just a loop through the array. Quote Link to comment https://forums.phpfreaks.com/topic/89145-solved-question/#findComment-457099 Share on other sites More sharing options...
SkyRanger Posted February 3, 2008 Author Share Posted February 3, 2008 use glob() to pull the filenames into an array. Then it's just a loop through the array. Never even thought of that Barand, thanks Quote Link to comment https://forums.phpfreaks.com/topic/89145-solved-question/#findComment-457126 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.