Formatting SQL Results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
indyboy
Forum Newbie
Posts: 6
Joined: Mon May 10, 2010 4:36 am

Formatting SQL Results

Post by indyboy »

Hey all

ive been trying to figure this out for a while now and my brains gone into meltdown. :banghead:

I want to format the results of an sql query so that it alternates the order each time it loops through the array

for example displays

image price description
description price image
then back to the first order

Anybody got any ideas :D

Cheers
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Formatting SQL Results

Post by mikosiko »

post the code that you have so far and will move from there
indyboy
Forum Newbie
Posts: 6
Joined: Mon May 10, 2010 4:36 am

Re: Formatting SQL Results

Post by indyboy »

im not great at explaining so heres a picture of the layout im looking for

Image

this is the code ill be working on

Code: Select all

while($row = mysql_fetch_array($query))
{
echo "<table width='100%' border='1'>";
echo "<tr><td>";
echo $row['make']; 
echo "</td></tr>";
echo "<tr><td>";
echo $row['model']; 
echo "</td></tr>";
echo "<tr><td>";
echo " £"; 
echo $row['price']; 
echo "</td></tr>";


if ($row['image_name'])
{

$imageDir = "images/";

$img = $imageDir . $row['image_name'];
echo "<tr><td>";

echo "<centre><img src='$img'>";
echo "</td></tr>";
}
echo"</table>";
echo"<br/>";

cheers
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Formatting SQL Results

Post by mikosiko »

indyboy
Forum Newbie
Posts: 6
Joined: Mon May 10, 2010 4:36 am

Re: Formatting SQL Results

Post by indyboy »

cheers for that

would i not still need to set up some kind of variable to alternate between the styles like this i found

Code: Select all

$i = 0; 
while( /* retrieve data from SQL */ ) { 
  if($i++ % 2 == 0) 
    echo "Odd row\n"; 
  else 
    echo "Even Row\n"; 
} 
i just dunno where or how to link the style to the odd and even rows
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Formatting SQL Results

Post by mikosiko »

indyboy wrote: would i not still need to set up some kind of variable to alternate between the styles like this i found

Code: Select all

$i = 0; 
while( /* retrieve data from SQL */ ) { 
  if($i++ % 2 == 0) 
    echo "Odd row\n"; 
  else 
    echo "Even Row\n"; 
} 
this is another alternative
indyboy wrote: just dunno where or how to link the style to the odd and even rows
read the examples in the links provided
Post Reply