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
    Dec 2012
    Posts
    54
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Split results into 2 columns

    I have the following code:

    Code:
    $result = mysql_query("SELECT * from patterns WHERE cattxt = 'Wreaths' ORDER BY dsptxt"); 
    while($row = mysql_fetch_array($result)) 
    { 
    echo "<a href=\"".$row['lnktxt']."\">".$row['dsptxt']."</a>"."<br />"."<br />"; 
    } 
    ?>
    It displays the results just fine, however I would like to break the results into two columns. I'm not quite sure how I go about that...do I have to specify after how many rows to break or does it do a 1 for 1 type deal in each column?

  • #2
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    PHP Code:
    $result = mysql_query("SELECT * from patterns WHERE cattxt = 'Wreaths' ORDER BY dsptxt"); 
    ?><table><?
    while($row mysql_fetch_array($result)) 

       
    ?><tr><td><?= $row['lnktxt'?></td><td><?= $row['dsptxt'?></td></tr><? 

    ?></table>

  • #3
    New Coder
    Join Date
    Dec 2012
    Posts
    54
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by poyzn View Post
    PHP Code:
    $result = mysql_query("SELECT * from patterns WHERE cattxt = 'Wreaths' ORDER BY dsptxt"); 
    ?><table><?
    while($row mysql_fetch_array($result)) 

       
    ?><tr><td><?= $row['lnktxt'?></td><td><?= $row['dsptxt'?></td></tr><? 

    ?></table>
    While this does break up data into two columns, it also breaks my links so I have the URL on one side and the display text on the other, so not really what I am looking for. But thank you for helping.

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Something like the following although I'm unable to test it currently.

    PHP Code:
    <?php
    $result 
    mysql_query("SELECT * from patterns WHERE cattxt = 'Wreaths' ORDER BY dsptxt"); 
    $split 0;
    echo 
    "<table><tr>";
    while(
    $row mysql_fetch_array($result)) 

        echo 
    "<td><a href=\"".$row['lnktxt']."\">".$row['dsptxt']."</a></td>";
        if (
    $split++ % 2) {    // or (($split++ % 2) == 1)
            
    echo "</tr><tr>";
        } 
    }
    if (
    $split 2) {
        echo 
    "</tr></table>";
    } else {
        echo 
    "<td></td></tr></table>";    // or "<td>&nbsp;</td></tr></table>"
    }
    ?>
    In particular, I may have the last two echoes the wrong way round(?).

    You might end up with a blank row, so it needs a little more work. Added: to get around this you could, instead, create a string and check what occurs at the end of this string - and amend it accordingly.
    Last edited by AndrewGSW; 12-13-2012 at 02:35 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • Users who have thanked AndrewGSW for this post:

    mharrison (12-13-2012)

  • #5
    New Coder
    Join Date
    Dec 2012
    Posts
    54
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by AndrewGSW View Post
    Something like the following although I'm unable to test it currently.

    PHP Code:
    <?php
    $result 
    mysql_query("SELECT * from patterns WHERE cattxt = 'Wreaths' ORDER BY dsptxt"); 
    $split 0;
    echo 
    "<table><tr>";
    while(
    $row mysql_fetch_array($result)) 

        echo 
    "<td><a href=\"".$row['lnktxt']."\">".$row['dsptxt']."</a></td>";
        if (
    $split++ % 2) {    // or (($split++ % 2) == 1)
            
    echo "</tr><tr>";
        } 
    }
    if (
    $split 2) {
        echo 
    "</tr></table>";
    } else {
        echo 
    "<td></td></tr></table>";
    }
    ?>
    In particular, I may have the last two echoes the wrong way round(?).

    You might end up with a blank row, so it needs a little more work.


    This does exactly what I want it to do....add the table width of 100% in there so it spaces the table out through the whole screen and it is perfect! The only oddity is I have 2 links at the very top showing out of alphabetical order, but it may be a problem with how they were entered in the database.

  • #6
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    If you prefer the data to go down the first column, before continuing in a second column, you would need to:

    Count how many records there are;
    Half this value;
    Store the td's in two separate arrays (for odd and even indexes);
    Use, perhaps, array_map, to construct a single string from the two arrays;
    Echo the resultant string.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #7
    New Coder
    Join Date
    Dec 2012
    Posts
    54
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by AndrewGSW View Post
    If you prefer the data to go down the first column, before continuing in a second column, you would need to:

    Count how many records there are;
    Half this value;
    Store the td's in two separate arrays (for odd and even indexes);
    Use, perhaps, array_map, to construct a single string from the two arrays;
    Echo the resultant string.
    I'm not going to worry about it that much. In order to do what you said, I would have to constantly be updating the code to reflect any increase in the number of records in the table. 2 links out of order is perfectly fine by me. Thanks again for the help!

  • #8
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    No worries

    I would check the resultant table (View Source) to make sure that the HTML is not malformed; but I suppose having an additional empty row is not too worrying - it will collapse anyway. Andy.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #9
    New Coder
    Join Date
    Dec 2012
    Posts
    54
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by AndrewGSW View Post
    No worries

    I would check the resultant table (View Source) to make sure that the HTML is not malformed; but I suppose having an additional empty row is not too worrying - it will collapse anyway. Andy.
    Final update...the 2 links that were out of order had an empty space before the 1st letter, so your code is perfect all the way around. I also checked the HTML and there is no empty row....table codes end properly after the last link on the 2nd column.


  •  

    Posting Permissions

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