I m new in php.i want some help in my code..plz..
i have 15 image in mysql table .and i want to retreive atleast 3 images in first row.den other 3 in second row..how can i make it possible..
the code is given below..!

<?php
include "dbconfig.php";
?>

<link href="main.css" rel="stylesheet" type="text/css" />
<table border="1" cellpadding="0" cellspacing="0" bordercolor="#BCC5C7">

<tr><?php
$results = "SELECT * FROM files WHERE menu_item_id='5' limit 5";
$Img_result = mysql_query ($results, $conn);
if (mysql_num_rows ($Img_result) >= 0){
$Img_menu = array();
while ($rows = mysql_fetch_array ($Img_result, MYSQL_ASSOC)){

$realname = $rows ['filename'];
$small_preview = 'download/wallpapers/'.$realname.'';
$large_preview = 'download/wallpapers/'.$realname.'';

?><td><?php echo "<a href='$large_preview' target='_blank'><img width='120' height='200' src='$small_preview' border='0'></a>";?>
<br /><div class="pic"><strong>Name :</strong> <?php echo $rows ['title']; ?></div></td><? } } ?></tr>

</table>

Recommended Answers

All 7 Replies

Are you saying you want 3 images per table row?
If this is what you want to do, you may also want to change your limit on the query results to 6.

Then you would put your table creation inside of a php loop:

<?php
$totalResults=mysql_num_rows($yourQuery);
$numberOfImagesPerRow=3;
$numberOfRows=$totalResults/$numberOfImagesPerRow;
echo "<table">;
for($x=0;$x<$numberOfRows;$x++)
{
echo "<tr>";
  for($i=0;$i<$numberOfImagesPerRow;$i++)
   {
      echo "<td>";
      //picture display code here
      echo "</td>;
   }
echo"</tr>";
}
echo "</table>";
?>

And if that isn't what you want to do, well now you know how to do that!

Thanx DisGSGurl...!i got it..thanx a billion

there is an other prob..it repeat 1 image various times..n i want to retri all images from database.this code showing 3 images per row..
plz chk ma array n loop..
==========

<?php
$results = "SELECT * FROM files WHERE menu_item_id='5' order by rand()";
$Img_result = mysql_query ($results, $conn);
$totalResults=mysql_num_rows($Img_result);
$numberOfImagesPerRow=3;
$numberOfRows=$totalResults/$numberOfImagesPerRow;

if (mysql_num_rows ($Img_result) >= 0){
$Img_menu = array();
while ($rows = mysql_fetch_array ($Img_result, MYSQL_ASSOC)){


echo "<table>";
for($x=0;$x<$numberOfRows;$x++)
{
echo "<tr>";
  for($i=0;$i<$numberOfImagesPerRow;$i++)
   {
      echo "<td>";
      //PREVIEW IMAGE IF EXISTS
$realname = $rows ['filename'];
$small_preview = 'download/wallpapers/'.$realname.'';

	echo "<div align='center'><img src='$small_preview' border='0'></div><BR>";

 
      echo "</td>"; 
     }
	 
	}  
echo "</tr>";
}
}
echo "</table>";

?>

===============

The reason you are only showing one file over and over is due to this line:

$realname = $rows ['filename'];

You need to tell it to iterate through the results array. You should keep a counter and append the number onto that line:

$i=0;(outside of your while loop)
$realname = $rows ['filename'][$i];
$i++;

WHERE menu_item_id='5'

Your where clause determines how many records are going to be returned. If you are having trouble after trying the first suggestion of moving through your array, then make sure the where is what you need it to be.

all recordes..
let me explain..!
if i have 12 images in my db..den
1 image repeat 12 time.den 2nd image repeat 12 times,..

Take the table out of the while loop.
Use the while loop to create your image array.

$i=0;
while ($rows = mysql_fetch_array ($Img_result, MYSQL_ASSOC))
{
$imageArray=$rows['filename'][$i};
$i++;
}

Then inside your table where you display your filename:

$c=0;(outside of the for loop)
$realname = $rows ['filename'][$c];
$c++;

DiGSGurl...would u like to eidt ur suggestion in ma code plz ..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.