I have a table with images ('imgurl' , 'orientation') where orientation is vertical or horizontal.
I want to display images :
first horizontal first vertical second vertical second horizontal
I would usually do a
while($img2 = mysql_fetch_assoc($img))
{ ... }
But how can I change that to control first horizontal, then first vertical, then second vertical, then second horizontal,
thanks
CODE:
$img = mysql_query("select * from blogimage where blog_id = '".$blog2['blog_id']."' ");
while($img2 = mysql_fetch_assoc($img))
{ ?>
<tr><td colspan="2"><img src="blogimages/<? echo $img2['img_url']; ?>" /></td></tr>
<? } ?>
the mysql table blogimage has 'img_url' and 'orientation' (vertical or horizontal)
There's a couple of solutions to this:
Add a third column to your table called position: store the position of the element in there.
If you insist doing it your way, be careful.
$img = mysql_query("select * from blogimage where blog_id = '".$blog2['blog_id']."' ");
Just a note don't pass unescaped strings into your SQL queries use parameter binding like PDO or framework like codeigniters active record. Also mysql is depreciated use mysqli
if you want to alternate between vertical and horizontal store the results in an array:
$vertical,$horizontal = array();
while($img2 = mysql_fetch_assoc($img))
{
$orientation = mysql_fetch_assoc($orientation);
if($orientation == 'vertical') {
array_push($vertical,$img2);
}
else
{
array_push($horizontal,$img2 );
}
//now you have two arrays filled with index=>picture based on orientation
for($i=0; $i<count($horizontal)-1; i++)
{
echo "<tr><td colspan='2'><img src='blogimages/'.$horizontal[$i].'/></td></tr>";
echo "<tr><td colspan='2'><img src='blogimages/'.$vertical[$i].'/></td></tr>";
}
}