Jump to content

make picture display in a row


chanfuterboy

Recommended Posts

hi,

 

i try and try dont come with it, need to let picture display in a row of 3. help me plz

 

$cols = 3;

 

// creating table inorder to use it for columns.

echo "<table width=100% border ='1' bordercolor='#3399FF' cellpadding='10' cellspacing='6'>

      <tr>";

 

//open the dir where files were uploaded inside.

 

// create a loop and define a variable called ($file)

// to hold the value of file name located in the directory .

$i =1;

 

//define a variable to hold everthing that will be printed in table's columns.

$all_stuff =  "File name:<b> $location1</b> <hr size=1>

 

             

              <img src=\"images/$location\" border=\"0\" width=\"150\"></a>

              <br>article name: $location1

              <br>Article color: $location2";

             

//divide the loop ($i) and ($cols) if the result is integer

//then print column number 1.

if (is_int($i / $cols)){

echo "<td align='center' valign='top' bgcolor='#F2F2F2'>$all_stuff</td></tr><tr>";

}else{

//else then print column number 2 etc for the next loop.

echo "<td align='center' valign='top' bgcolor='#F2F2F2'>$all_stuff</td>";

}

//add only 1 for the loop ($i).

$i++;

 

 

}//end while loop

closedir($opendir);

clearstatcache();

// print the rest of table.

echo "</tr>

      </table>";

Link to comment
Share on other sites

let me explain in details. the page now print the latest 6 uploaded pictures . the pictures should display in 2 rows with 3 columns.

 

But i get now 6 picture in 1 column. i know somewhere in the scripts i need to edit but i dont know where. someone can help me

 

if (is_int($i / $cols)){

echo "<td align='center' valign='top' bgcolor='#F2F2F2'>$all_stuff</td></tr><tr>";

}else{

//else then print column number 2 etc for the next loop.

 

echo "  <td align='center' valign='top' bgcolor='#F2F2F2'>$all_stuff</td>";

}

//add only 1 for the loop ($i).

$i++;

Link to comment
Share on other sites

hi,

 

THe site help me put them in 1 row instead as previous in 1 column. Now help me with: when i have 6 picture to display ( limit 0,6) it display 2 row with 3 picture in each row. this is the new code.

 

$tblWidth = 4;

$col= 2;

// Make your query

$sql = mysql_query("SELECT * FROM members Limit 1,3");

$i = 1;

echo "<br><br><br><br><table width=100% border ='1' bordercolor='#3399FF' cellpadding='10' cellspacing='16'>

<tr>";

 

 

// Check to see if any results were returned

if(mysql_num_rows($sql) > 0){

 

echo '';

// Loop through the results

while($row = mysql_fetch_array($sql)){

$location = $row['picture'];

$location1 = $row['articlename'];

$location2 = $row['articlecolor'];

 

 

$all_stuff =  "<b>File name</b>:<b><font face='Tahoma' size='2'>$location1</font></b> <hr size=1>

 

              <img src='images/$location' width='100' height='100'></a>

              <br>article name: $location1

              <br>Article color: $location2";

if($i == $tblWidth && $i / $col){

echo "<td align='center' valign='top' bgcolor='#F2F2F2'>$all_stuff</td>";

}else{

//else then print column number 2 etc for the next loop.

                   

echo "<td align='center' valign='top' bgcolor='#F2F2F2'>$all_stuff</td>";

}

//add only 1 for the loop ($i).

$i++;

Link to comment
Share on other sites

hi,

 

THe site help me put them in 1 row instead as previous in 1 column. Now help me with: when i have 6 picture to display ( limit 0,6) it display 2 row with 3 picture in each row. this is the new code.

 

$tblWidth = 4;

$col= 2;

// Make your query

$sql = mysql_query("SELECT * FROM members Limit 0.6");

$i = 1;

echo "<br><br><br><br><table width=100% border ='1' bordercolor='#3399FF' cellpadding='10' cellspacing='16'>

<tr>";

 

 

// Check to see if any results were returned

if(mysql_num_rows($sql) > 0){

 

echo '';

// Loop through the results

while($row = mysql_fetch_array($sql)){

$location = $row['picture'];

$location1 = $row['articlename'];

$location2 = $row['articlecolor'];

 

 

$all_stuff =  "<b>File name</b>:<b><font face='Tahoma' size='2'>$location1</font></b> <hr size=1>

 

              <img src='images/$location' width='100' height='100'></a>

              <br>article name: $location1

              <br>Article color: $location2";

if($i == $tblWidth && $i / $col){

echo "<td align='center' valign='top' bgcolor='#F2F2F2'>$all_stuff</td>";

}else{

//else then print column number 2 etc for the next loop.

                   

echo "<td align='center' valign='top' bgcolor='#F2F2F2'>$all_stuff</td>";

}

//add only 1 for the loop ($i).

$i++;

Link to comment
Share on other sites

there's an easier way, but it means not using tables (Horrah!)

 

if you style a page based on CSS you can use the CSS to display them how you want.

 

example HTML:

<div id="gallery">
<h6>img</h6>
<h6>img</h6>
<h6>img</h6>
<h6>img</h6>
<h6>img</h6>
<h6>img</h6>
<h6>img</h6>
<h6>img</h6>
<h6>img</h6>
</div>

 

then the CSS

#gallery
{
padding : 0;
width : 600px;
height : auto;
display : block;
float : left;
}
#gallery h6
{
width : 200px;
height : 200px;
margin : 0;
display : block;
float : left;
}

 

that will display them 3 in a row, for two rows (obviously change your CSS width/height to suite your needs.

 

means you don't need to do anything special with your php code.

Link to comment
Share on other sites

Try this

 

echo '<table border="1"> <tr>';
for( $i = 0; $i < 6; $i++ ) {
if( ($i % 3) == 0 ) {
	echo '</tr><tr>';	
}
echo '<td><img width="100" height="100" src="http://bit.ly/ZSu4K" /></td>';

}
echo '</table>';[/code[

 

it gives

 

smi-1.jpg

Link to comment
Share on other sites

Ok, my code's now:

<?php
    $path = "uploads";

    // Open the folder
    $dir_handle = @opendir($path) or die("Unable to open $path");

    // Loop through the files
    while ($file = readdir($dir_handle)) {
echo '<table border="1"> <tr>';
for( $i = 0; $i < 3; $i++ ) {

   if( ($i % 3) == 0 ) {
      echo '</tr><tr>';   
   }
    if($file == "." || $file == ".." || $file == "jhey.js" || $file == "uploaded.php" || $file == "index.php" )
continue;
        echo "<td><center><img src =\"$file\" width=\"96\" height=\"96\"><br /><br></td>";
}
echo '</table>';   
    }
    // Close
    closedir($dir_handle);
?>
</center>

 

When I execute that code, it displays the same images in one row, then another image in the second row...and so on..

 

For a screenshot:

16887362.jpg

 

Link to comment
Share on other sites

Not supposed to use the while loop with the for loop.

 

here's a code that will work

 

<?php

$path = "./uploads";

// Open the folder
$dir_handle = @opendir($path) or die("Unable to open $path");

$i = 0;

// extensions allowed
$allowed = array('jpg', 'png', 'gif', 'bmp');


echo '<table border="1"> <tr>';

// Loop through the files
while ($file = readdir($dir_handle)) {
$ext = end(explode('.', $file));

// if $file is not a dir and has an allowed extension, show.
if( !is_dir($file) && in_array($ext, $allowed) ) {

	// needed to display 3 images pew row
	echo ( ($i % 3) == 0 ) ? '</tr><tr>' : null;

	// output image
	echo "<td><center><img src =\"$path/$file\" width=\"96\" height=\"96\"><br /><br></td>";

	$i++;
}
}

echo '</table>';

// Close handle
closedir($dir_handle);

?>

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.