Ask script to echo n images per row

Discussion in 'PHP' started by junandya, Jan 1, 2008.

  1. #1
    Hi all,

    i have some problem with "show images". This is the field from table 'gallery',

    id int(25)
    imagePath varchar(250)
    imageTitle varchar(250)

    which is the image path taken from field 'imagePath', so i could be like this:
    <img src="imgGallery/<?PHP $row["imagePath"]; ?>">
    PHP:
    The result that i want is like this, all images displayed in each row is 4 images:
    <TABLE>
    	<TR>
    		<TD>
    			<TABLE>
    				<TR>
    					<TD><img src="imgGallery/1.jpg"></TD>
    				</TR>
    				<TR>
    					<TD>tile: image number 1</TD>
    				</TR>
    			</TABLE>
    		</TD>
    		<TD>
    			<TABLE>
    				<TR>
    					<TD><img src="imgGallery/2.jpg"></TD>
    				</TR>
    				<TR>
    					<TD>tile: image number 2</TD>
    				</TR>
    			</TABLE>
    		</TD>
    		<TD>
    			<TABLE>
    				<TR>
    					<TD><img src="imgGallery/3.jpg"></TD>
    				</TR>
    				<TR>
    					<TD>tile: image number 3</TD>
    				</TR>
    			</TABLE>
    		</TD>
    		<TD>
    			<TABLE>
    				<TR>
    					<TD><img src="imgGallery/4.jpg"></TD>
    				</TR>
    				<TR>
    					<TD>tile: image number 4</TD>
    				</TR>
    			</TABLE>
    		</TD>
    	</TR>
    	<TR>
    		<TD>
    			<TABLE>
    				<TR>
    					<TD><img src="imgGallery/5.jpg"></TD>
    				</TR>
    				<TR>
    					<TD>tile: image number 5</TD>
    				</TR>
    			</TABLE>
    		</TD>
    		<TD>
    			<TABLE>
    				<TR>
    					<TD><img src="imgGallery/6.jpg"></TD>
    				</TR>
    				<TR>
    					<TD>tile: image number 6</TD>
    				</TR>
    			</TABLE>
    		</TD>
    		<TD>
    			<TABLE>
    				<TR>
    					<TD><img src="imgGallery/7.jpg"></TD>
    				</TR>
    				<TR>
    					<TD>tile: image number 7</TD>
    				</TR>
    			</TABLE>
    		</TD>
    		<TD>
    			<TABLE>
    				<TR>
    					<TD><img src="imgGallery/8.jpg"></TD>
    				</TR>
    				<TR>
    					<TD>tile: image number 8</TD>
    				</TR>
    			</TABLE>
    		</TD>
    	</TR>
    </TABLE>
    PHP:
    My question is how is the complete php script (also the query), so i can display the images as 4 images in a row, and 4 images again in next row...

    Thank you
     
    junandya, Jan 1, 2008 IP
  2. phpl33t

    phpl33t Banned

    Messages:
    456
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I will code this for $25.
     
    phpl33t, Jan 1, 2008 IP
  3. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Personally, I think your nested tables will bloat your html. I would try something like this:

    <?php
    
    $result = mysql_query( "SELECT * FROM gallery" );
    
    $count = 0;
    
    while ( $Row = mysql_fetch_assoc( $result ) )
    {
    	$count++;
    	
    	echo '<TABLE style="float:left;">
    <TR>
    <TD><img src="imgGallery/' . $Row["imagePath"] . '"></TD>
    </TR>
    <TR>
    <TD>tile: ' . $Row["imageTitle"] . '</TD>
    </TR>
    </TABLE>';
    
    	if ( $count % 4 ) echo '<br style="clear:both;" />';
    
    }
    
    ?>
    PHP:
    The table in the loop could be replaced by a div if need be. Have a play and see what you think.

    Brew
     
    Brewster, Jan 1, 2008 IP
  4. junandya

    junandya Member

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #4
    @phpl33t : thanx for your offering, but i'm really sorry this i really dont have enough money for this code, thanx anyway.

    @Brewster : thanx it's working properly, but i have to change
    if ( $count % 4 )
    PHP:
    to be like this
    if ( $count % 4 == 0 )
    PHP:
    :) Best Regards
     
    junandya, Jan 1, 2008 IP
  5. phpl33t

    phpl33t Banned

    Messages:
    456
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    0
    #5
    A fix for that code:



    $result = mysql_query( "SELECT * FROM `gallery`") or die(mysql_error());
    if (mysql_num_rows($result) >= 1) {
    	echo '<table border="0">';
    	$column = 1;
    	while($items = mysql_fetch_array($result)) {
    		$imagepath = stripslashes($items['imagepath']);
    		$imageTitle = stripslashes($items['imageTitle']);
    		if ($column == 1) {
    			echo '<tr>';
    		}
          		echo '
          			<td valign="top" width="50%" align="center">
          				<div><img src="./imgGallery/'.$imagePath.'"></div>
                                                       <div><strong>Title: '.$imageTitle.'</div>
          			</td>
          		';
          		$column++;
          		if ($column == 3) { 
          			echo '</tr>';
          			$column = 1;
          		}
    	}
    	echo '</table>';
    } else {
    	echo '<div>No images found in the database!</div>';
    }
    PHP:
     
    phpl33t, Jan 2, 2008 IP