SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict
    Join Date
    Jan 2002
    Location
    Norway
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Results across 2 columns

    I have been playing around with some code from a tutorial by Matt Wade at codewalkers.com. It works ok except that for some reason I get too many table rows:

    The code:

    $columns = 2;

    $sql = "SELECT a_pic, a_title, a_date
    FROM Activities";

    $result = mysql_query($sql) or die("Couldn't execute query.");

    $num_rows = mysql_num_rows($result);

    $rows = ceil($num_rows / $columns);

    while($row = mysql_fetch_array($result)) {
    $data[] = $row['a_pic'];
    $data1[] = $row['a_title'];
    $data2[] = $row['a_date'];

    }

    echo "<table border=\"0\">\n";

    for($i = 0; $i < $num_rows; $i++) {
    echo "<tr>\n";

    for($j = 0; $j < $columns; $j++) {
    if(isset($data[$i + ($j * $rows)])) {
    echo "<td><img src=\"" . $data[$i + ($j * $rows)] . "\"></td>\n";

    echo "<td valign=\"top\">" . $data1[$i + ($j * $rows)] . "<br>" . $data2[$i + ($j * $rows)] . "</td>\n";
    }

    }
    echo "</tr>\n";
    }
    echo "</table>\n";

    ?>

    I have 6 rows in the table. From this code I get 9 results like this:


    result1 result4 again
    result2 result5 again
    result3 result6 again
    result4
    result5
    result6

    So it appears that the script is echoing 6 table rows instead of 3. There is supposed to be 2 columns printed in each row with the amount of rows determined by the num_rows function...


    I could use some help, I have square eyes and a tired brain...

  2. #2
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know exactly how you want the data formatting, but I use something like this:

    Code:
    $columns = 2;
    
    $sql = "SELECT a_pic, a_title, a_date FROM Activities";
    
    $result = mysql_query($sql) or die("Couldn't execute query." );
    
    $num_rows = mysql_num_rows($result);
    
    $rows = ceil($num_rows / $columns);
    
    echo "<table border=\"0\">\n<tr>\n<td>";
    
    $index = 0;
    while($row = mysql_fetch_array($result)) {
        if($index == $rows) {
            echo "</td>\n<td>\n";
        }
        $index++;
        echo "<img src=\"" . $row['a_pic'] . "\"><br>\n";
        echo $row['a_title'] . "<br>" . $row['a_date'] . "\n";    
    }
    
    echo "</td>\n</tr>\n</table>\n";

  3. #3
    SitePoint Addict
    Join Date
    Jan 2002
    Location
    Norway
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's very nice! I adjusted the code slightly for formatting, but I am having trouble with making the photos and text line up correctly...

    I inserted <p> tags but it doesn't quite do it...

    Code:

    <?php

    $columns = 2;

    $sql = "SELECT a_pic, a_title, a_date, a_intro FROM Activities";

    $result = mysql_query($sql) or die("Couldn't execute query." );

    $num_rows = mysql_num_rows($result);

    $rows = ceil($num_rows / $columns);

    echo "<table border=\"0\">\n<tr>\n<td valign=\"top\">";

    $index = 0;
    while($row = mysql_fetch_array($result)) {
    if($index == $rows) {
    echo "</td>\n<td>\n";
    }
    $index++;
    echo "<p><img src=\"" . $row['a_pic'] . "\" align=\"left\">\n";
    echo $row['a_title'] . "<br>" . $row['a_date'] . "<br>" . $row['a_intro'] . "</p>\n";
    }

    echo "</td>\n</tr>\n</table>\n";

    ?>

  4. #4
    SitePoint Addict Knightime's Avatar
    Join Date
    Jan 2003
    Location
    USA
    Posts
    376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jstubbs
    That's very nice! I adjusted the code slightly for formatting, but I am having trouble with making the photos and text line up correctly...

    I inserted <p> tags but it doesn't quite do it...

    Code:

    <?php

    $columns = 2;

    $sql = "SELECT a_pic, a_title, a_date, a_intro FROM Activities";

    $result = mysql_query($sql) or die("Couldn't execute query." );

    $num_rows = mysql_num_rows($result);

    $rows = ceil($num_rows / $columns);

    echo "<table border=\"0\">\n<tr>\n<td valign=\"top\">";

    $index = 0;
    while($row = mysql_fetch_array($result)) {
    if($index == $rows) {
    echo "</td>\n<td>\n";
    }
    $index++;
    echo "<p><img src=""\"" . $row['a_pic'] . "\" align=\"left\">\n";
    echo $row['a_title'] . "<br>" . $row['a_date'] . "<br>" . $row['a_intro'] . "</p>\n";
    }

    echo "</td>\n</tr>\n</table>\n";

    ?>

    Try this out. (not sure why you have so many \n's though)

    echo "<table border=\"0\"><tr><td colspan=\"2\" valign=\"top\">";

    $index = 0;
    while($row = mysql_fetch_array($result)) {
    if($index == $rows) {
    echo "</td></tr>";
    }
    $index++;
    echo "<tr style=\"text-align:left; vertical-align:top;\"><td img src=""\"" . $row['a_pic'] . "\" ></td><td " . $row['a_title'] . "<br />" . $row['a_date'] . "<br />" . $row['a_intro'] . "</td></tr>";
    }

    echo "</table>\n";


    Not sure what look you are trying to achieve, but this should give you an idea of how to possibly control the elements better.

    Good luck.

    Troy
    Last edited by Knightime; Mar 17, 2003 at 11:50.

  5. #5
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That would cause his code to be split into two rows, rather than the two columns he asked for.

  6. #6
    SitePoint Addict Knightime's Avatar
    Join Date
    Jan 2003
    Location
    USA
    Posts
    376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It does both actually. Two rows, bottom row = two columns. I am throwing out ideas cause the previously fix already gave him two columns, but it wasn't as neat as he wanted.
    Got a better way to do it?

  7. #7
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've no idea how he wants the data formatting so no.

  8. #8
    SitePoint Addict
    Join Date
    Jan 2002
    Location
    Norway
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the replies. What I want is the following:

    image1 title image4 title
    image1 date image4 date
    image1 intro image4 intro

    image2 title image5 title
    image2 date image5 date
    image2 intro image5 intro

    image3 title image6 title
    image3 date image6 date
    image3 intro image6 intro

    If the images go in a separate <td> then better because I can make sure that if the one line intro becomes two lines it won't break.

    I have the following currently and it seems to work ok, but I would welcome any other suggestions!

    $columns = 2;

    $sql = "SELECT a_pic, a_title, a_date
    FROM Activities
    ORDER BY a_date";

    $result = mysql_query($sql) or die("Couldn't execute query.");

    $num_rows = mysql_num_rows($result);

    $rows = ceil($num_rows / $columns);

    while($row = mysql_fetch_array($result)) {
    $data[] = $row['a_pic'];
    $data1[] = $row['a_title'];
    $data2[] = $row['a_date'];

    }

    echo "<table border=\"0\">\n";

    for($i = 0; $i < $rows; $i++) {
    echo "<tr>\n";

    for($j = 0; $j < $columns; $j++) {
    if(isset($data[$i + ($j * $rows)])) {
    echo "<td><img src=\"" . $data[$i + ($j * $rows)] . "\"></td>\n";

    echo "<td valign=\"top\">" . $data1[$i + ($j * $rows)] . "<br>" . $data2[$i + ($j * $rows)] . "</td>\n";
    }

    }
    echo "</tr>\n";
    }
    echo "</table>\n";

    ?>
    Last edited by jstubbs; Mar 17, 2003 at 15:43.

  9. #9
    SitePoint Addict
    Join Date
    Jan 2002
    Location
    Norway
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I should have mentioned that the difference between the first code I posted and the last is this line:

    for($i = 0; $i < $num_rows; $i++) {
    echo "<tr>\n

    The corrected code is:

    for($i = 0; $i < $rows; $i++) {
    echo "<tr>\n

  10. #10
    SitePoint Addict
    Join Date
    Jan 2002
    Location
    Norway
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Formatting this way would also be good:

    image1 title image2 title
    image1 date image2 date
    image1 intro image2 intro

    image3 title image4 title
    image3 date image4 date
    image3 intro image4 intro

    image5 title image6 title
    image5 date image6 date
    image5 intro image6 intro

    Where is says "image1" etc, that is one image i.e "a_pic". The a_title, a_date and a_intro are in a separate <td> and with line breaks <br> to separate them.

    Hope this is clear...!


Bookmarks

Posting Permissions

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