This is a discussion on increment an array element to display jpg's ... within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I'm reading a directory of pictures , and trying to display them nicely formated in a table 4 accross... a ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I'm reading a directory of pictures , and trying to display them nicely
formated in a table 4 accross... a few issues .. My play with array's don't work ...I'm able to feed pictures into array's ,with $thefiles[$c] = $file; but when I try to call the pictures echo "<img src=\"\\images\\$thefiles[$c]\" width='142'height='142'>"; works but thefiles[$c+1] bombs ! If I can't manually increment the array I don't see how to do this .. the whole thing at the moment is: <html> <head> <title>PHP Test</title> </head> <body> <?php $mydir = dir('images'); ?> <?php $thefiles=array(); ?> <?php echo '<table border="3">'; $c=0 ?> <?php while(($file = $mydir->read()) !== false) { $c=$c+1 ; // echo $c; $d=($c+=2); $thefiles[$c] = $file; echo '<tr>'; echo '<td>'; // echo "<img src=\"\\images\\$file\" width='142'height='142'>"; // echo "<img src=\"\\images\\$file\" width='142'height='142'>"; echo "<img src=\"\\images\\$thefiles[$c]\" width='142'height='142'>"; echo "<img src=\"\\images\\$thefiles[$d]\" width='142'height='142'>"; //echo "<img src=\"\\images\\$thefiles[$c(+2)]\" width='142'height='142'>"; // $c=$c+1 ; echo $c; echo $c; echo '</td>'; echo '</tr>'; } echo '</table>'; $mydir->close(); ?> <p> test</p> </body> </html> |
|
|||
|
I played around for a few minutes and made an example for you, I think this
captures what you want to do.... http://ghost.3drealm.net/imagetable.php <?php // just setting a path variable to simplify writing $path = "puppies/images/puppies/"; // how many columns do you want? can be set to whatever $cols = 4; $mydir = dir($path); $lines = "<table border=3>\n"; while ($file = $mydir->read()) { $fullpath = $path.$file; if (GetImageSize($fullpath)) { // Increment $c up 1 each cycle thru $c++; // If this is the first entry on the row start the table row. if ($c == 1) { $lines .= "<tr>\n"; } $lines .= "<td><img src='$fullpath' width=150></td>\n"; // If this is the last column close the row and set $c to 0 if ($c == $cols) { $lines .= "</tr>\n"; $c = 0; } } } // now finish out the table columns just to be complete if ($c < $cols) { $colstogo = $cols - $c; $lines .= "<td colspan=$colstogo> </td>\n"; } // and add in the closing tag to make netscape happy. $lines .= "</table>"; echo $lines; ?> This is the un*x version, freebsd actually, so the file pathing is different but the code should work just the same. Doug B. =============================================== "J Bard" <support@41north.net> wrote in message news:21dTb.6520$GO6.4547@newsread3.news.atl.earthl ink.net... > I'm reading a directory of pictures , and trying to display them nicely > formated in a table 4 accross... > a few issues .. > > My play with array's don't work ...I'm able to feed pictures into array's > ,with > $thefiles[$c] = $file; > > but when I try to call the pictures > echo "<img src=\"\\images\\$thefiles[$c]\" width='142'height='142'>"; > works but > > thefiles[$c+1] > > bombs ! If I can't manually increment the array I don't see how to do this > . > > > the whole thing at the moment is: > > <html> > <head> > <title>PHP Test</title> > </head> > <body> > > <?php > > $mydir = dir('images'); > ?> > <?php $thefiles=array(); ?> > <?php echo '<table border="3">'; $c=0 ?> > > <?php > > while(($file = $mydir->read()) !== false) { > > $c=$c+1 ; // echo $c; > $d=($c+=2); > $thefiles[$c] = $file; > > > echo '<tr>'; > echo '<td>'; > // echo "<img src=\"\\images\\$file\" > width='142'height='142'>"; > // echo "<img src=\"\\images\\$file\" > width='142'height='142'>"; > echo "<img src=\"\\images\\$thefiles[$c]\" > width='142'height='142'>"; > echo "<img src=\"\\images\\$thefiles[$d]\" > width='142'height='142'>"; > //echo "<img > src=\"\\images\\$thefiles[$c(+2)]\" width='142'height='142'>"; > // $c=$c+1 ; echo $c; > echo $c; > > echo '</td>'; > echo '</tr>'; > > > } > echo '</table>'; > $mydir->close(); > > > ?> > <p> test</p> > > > > > </body> > </html> > > > |
|
|||
|
I would first read the dir, put all the images into the array:
$mydir = dir('images'); $thefiles = array(); while(($file = $mydir->read()) !== false) { if ( is_file ( $file )) { // might want to check if it's really images // and not text, or something else $thefiles[] = $file; } } $mydir->close(); Then format and print the table. foreach ( $thefiles as $value ) { // table stuff echo "<img src='$value' width='142'height='142'>\n"; // table stuff } HTH. "J Bard" <support@41north.net> wrote in message news:21dTb.6520$GO6.4547@newsread3.news.atl.earthl ink.net... > I'm reading a directory of pictures , and trying to display them nicely > formated in a table 4 accross... > a few issues .. > > My play with array's don't work ...I'm able to feed pictures into array's > ,with > $thefiles[$c] = $file; > > but when I try to call the pictures > echo "<img src=\"\\images\\$thefiles[$c]\" width='142'height='142'>"; > works but > > thefiles[$c+1] > > bombs ! If I can't manually increment the array I don't see how to do this > . > > > the whole thing at the moment is: > > <html> > <head> > <title>PHP Test</title> > </head> > <body> > > <?php > > $mydir = dir('images'); > ?> > <?php $thefiles=array(); ?> > <?php echo '<table border="3">'; $c=0 ?> > > <?php > > while(($file = $mydir->read()) !== false) { > > $c=$c+1 ; // echo $c; > $d=($c+=2); > $thefiles[$c] = $file; > > > echo '<tr>'; > echo '<td>'; > // echo "<img src=\"\\images\\$file\" > width='142'height='142'>"; > // echo "<img src=\"\\images\\$file\" > width='142'height='142'>"; > echo "<img src=\"\\images\\$thefiles[$c]\" > width='142'height='142'>"; > echo "<img src=\"\\images\\$thefiles[$d]\" > width='142'height='142'>"; > //echo "<img > src=\"\\images\\$thefiles[$c(+2)]\" width='142'height='142'>"; > // $c=$c+1 ; echo $c; > echo $c; > > echo '</td>'; > echo '</tr>'; > > > } > echo '</table>'; > $mydir->close(); > > > ?> > <p> test</p> > > > > > </body> > </html> > > > |