...

View Full Version : Align images only 4 in a row



shoaib
07-30-2010, 09:27 AM
Hi,

I have take the images form the database using the while loop.
I want to display the images only 4 in a row.

I have pasted the codings below.




<?php

$v_name = JRequest::getString('v_name', '', 'get');
if ( $v_name )
{
$db =& JFactory::getDBO();
$result = mysql_query( "SELECT Image_Path FROM Project_Design WHERE Project_ID = $db->quote($v_name)")
or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);


echo "<table width=600 border=0>\n";
$i=0;
while ($get_info = mysql_fetch_row($result))
{

//print "<tr>\n";

if(($i%2)==0)
{

print "<td> <img src=$get_info[0]><br /></img></td>";

}
else
{


echo "<td> <img src=$get_info[0]></img> </td>";

}

//print "</tr>\n";

$i++;

}
echo "</table>\n";
}


?>


Regards,
Shoaib.
:)

djh101
07-30-2010, 10:18 AM
You'd probably get more responses if you posted in the PHP forum. Anyway, what you could do is add a counter. You could either have the counter rest at 4 or use a script to check if the number is divisible by 4 evenly and then add a new row accordingly. Or, a new row, since you're using a table.
EDIT: Actually, since you're already using i as a counter, you could just implement that right into an if statement.


while ($get_info = mysql_fetch_row($result)){
//print "<tr>\n";

if(($i%2)==0){
print "<td> <img src=$get_info[0]><br /></img></td>";
}
else{
echo "<td> <img src=$get_info[0]></img> </td>";
}
if ($i%4 == 0 && $i != 0){ //NEW CODE, CHECKS IF $i DIVIDED BY 4 HAS A REMAINDER AND IF NOT IT ENDS THE ROW AND ADDS A NEW ROW
echo "</tr><tr>";
}
$i++;
}
echo "</tr>"; //THIS CREATES A ROW END FOR THE LAST ROW AFTER THE WHILE STATEMENT IS COMPLETE

shoaib
07-31-2010, 12:50 PM
Hi,


Thanks For ur Help. It Works fine.

Regards,
Shoaib.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum