...

View Full Version : Formatting sql query results into 4 columns



manders
04-02-2010, 01:40 PM
Hi Guys, wondering if you can help as Im a bit of a newbie!
Basically I want to be able to display one image per result into a column in a table.
For each row in the table I want 4 columns with an image in each one. Its probably quite simple and I've been searching around but cant seem to get it working.

Heres my code so far. Thanks in advance!

<?php
$query = "SELECT * FROM holidays WHERE type='tropical'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
?>

$rows_returned = mysql_num_rows($result);
$base = $rows_returned/4;
$first_column = $second_column = $third_column = $base;
if ($base % 4 > 0) {
$first_column++;
}
if ($base % 4 > 1) {
$second_column++;
}
$first_break = $first_column;
$second_break = $first_column + $second_column;
echo '<table class="catlist"><tr><td>';
for ($i = 1; $i <= $rows_returned; $i++) {
$row = mysql_fetch_row ($result);

echo '<a href="<?php echo $row['galurl']; ?>"><img height="179" width="117" src="<?php echo $row['hotimage']; ?>" border="0" alt="<?php echo $row['altname']; ?>">';

if ($i == $first_break $i == $second_break) {
echo '</td></tr>';
}
}
echo '</td></tr></table>';

MattF
04-02-2010, 01:55 PM
Untested:



<?php

$count = 0;
$query = "SELECT * FROM holidays WHERE type='tropical'";
$result = mysql_query($query) or die(mysql_error());

echo '<table class="catlist">';

while ($row = mysql_fetch_row($result))
{
++$count;
if (($count%4) == 0)
{
print('<tr>'."\n");
}
echo '<td><a href="'.$row['galurl'].'"><img height="179" width="117" src="'.$row['hotimage'].'" border="0" alt="'.$row['altname'].'"></td>';

if (($count%4) == 0)
{
print('</tr>'."\n");
}
}

if (($count%4) != 0)
{
print('</tr>'."\n");
}
echo '</table>';

?>

manders
04-02-2010, 03:16 PM
Hi Matt, thanks for your reply, appreciate it.
With that code I am getting 3 images on the first row and 1 on the 2nd row.

Any more help would be great thanks guys! :thumbsup:

Also all of my images show as broken and none of the alt texts work.

MattF
04-02-2010, 07:04 PM
Use the method I suggested in this thread instead:

http://codingforums.com/showthread.php?p=934733#post934733

It's easier to follow and debug, (in my opinion). :D

manders
04-03-2010, 05:17 PM
Hi Matt I've tried copying the code in the link you gave me but im still getting an error.

Here is my code below. Any more advice would be hugely appreciated!

<?php
$dbc = mysql_connect('mysql4.***************','a5314660_XXXXX','XXXXXX') or die(mysql_error());
mysql_select_db("a5314660_XXXXXX") or die(mysql_error());

?>
<?php
$query = "SELECT * FROM holidays WHERE type='tropical'";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)) {

$count = 0;

print('<table>'."\n");

while ($row = mysql_fetch_assoc($result))
{
$gal = '<a href="'.$row['galurl'].'"><img height="179" width="117" src="'.$row['hotimage'].'" border="0" alt="'.$row['altname'].'"></a>';

if ($count == 0)
{
print('<tr>');
}
++$count;
print('<td>'.$gal.'</td>');

if ($count == 4)
{
$count = 0;
print('</tr>'."\n");
}
}
print('</table>'."\n");

?>

MattF
04-03-2010, 06:49 PM
My psychic abilities seem to be on strike today :D. What's the error?

manders
04-03-2010, 09:06 PM
Ahhh the error would help lol.
It wont show up any of my results. It has the correct number of results but they are all broken images.

MattF
04-03-2010, 10:22 PM
Ahhh the error would help lol.
It wont show up any of my results. It has the correct number of results but they are all broken images.

View the page source and post one of the img links.

manders
04-04-2010, 04:10 PM
Hi Matt, I was messing about with the code just and I was missing an end curly bracket. All seems to be working perfectly now!

A big thanks to you!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum