Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP order into columns

Status
Not open for further replies.

internetguy

Technical User
Nov 22, 2003
57
US
I have a photo gallery with pictures from me and 2 friends. I have it select which photographer based on Get variables, and all the picture information is stored in a mysql database. I got the photos to sort each picture by row, but I want to sort two pictures per row.

This is what is happening...

col1 col2
| pic1 | pic1 |
| pic2 | pic2 |
| pic3 | pic3 |

What I want is...
col1 col2
| pic1 | pic2 |
| pic3 | pic4 |

here is my code

if (!isset($_GET['artist'])){
//Content introducing brent, joe and I
echo "<a href='photos.php?artist=brent'>Brent Darting</a><br>";
echo "<a href='photos.php?artist=james'>James Devan</a><br>";
echo "<a href='photos.php?artist=joe'>Joseph Hinton</a>";
} else {
$artist = $_GET['artist'];
mysql_connect('localhost','root');
mysql_select_db('justjd');
$query = "SELECT * FROM photos WHERE artist='".$artist."' ORDER by `name` LIMIT 0,10";
$result = mysql_query($query);
$num = mysql_num_rows($result);
for($num; $num > 0; $num--){
$row = mysql_fetch_array($result);
echo "<table width='420' border='0' cellspacing='0'>";
echo "<tr>\n<td align='center' width='210'>\n<a href='pics/photos/".$artist."/".$row['img']."'>\n<img src='pics/photos/".$artist."/thumbs/".$row['img']."' border='0'></a>\n<br>\n".$row['name']." - ".sizes($artist,$row['img'])."</td>";
echo "<td align='center'>\n<a href='pics/photos/".$artist."/".$row['img']."'>\n<img src='pics/photos/".$artist."/thumbs/".$row['img']."' border='0'></a>\n<br>\n".$row['name']." - ".sizes($artist,$row['img'])."</td>\n</tr>";
echo "</table>";
}
}
 
the easiest thing to do would probably be to add another two variables that count which column you're on. Also put your <table> tags outside of the loop so you can have as many rows/cols as you want.
Code:
$totalcols=2; //or more
$currentcol=0;

echo "<table width='420' border='0' cellspacing='0'>";
echo "<tr>"; //note that this first <tr> is outside also

for ($num; $num>0;$num--) {
   $pic=mysql_fetch_array($result);  //next line in array
   $currentcol++;   //increase column number (that's why the definition was zero
   echo "<td>put your picture info here</td>"; //show each column's picture
   if ($currentcol==$totalcols) { //check if it's the end of the column and if so, reset to a new line
       $currentcol=0;
       echo "</tr><tr>";  //end the row
   }
}
echo "</table>";

The code above still doesn't handle the ending of the array, but you get the idea.
 
Oh man, it seems so easy once it is explained. I can write the rest, thanks a million. This has been driving me nuts.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top