Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Formatting sql query results into 4 columns

    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>';

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Untested:

    Code:
    <?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>';
    
    ?>
    Last edited by MattF; 04-02-2010 at 01:16 PM.

  • #3
    New Coder
    Join Date
    Oct 2008
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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!

    Also all of my images show as broken and none of the alt texts work.
    Last edited by manders; 04-02-2010 at 02:25 PM.

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Use the method I suggested in this thread instead:

    http://codingforums.com/showthread.p...733#post934733

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

  • Users who have thanked MattF for this post:

    manders (04-04-2010)

  • #5
    New Coder
    Join Date
    Oct 2008
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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");

    ?>

  • #6
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    My psychic abilities seem to be on strike today . What's the error?

  • #7
    New Coder
    Join Date
    Oct 2008
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.

  • #8
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by manders View Post
    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.

  • #9
    New Coder
    Join Date
    Oct 2008
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •