Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-13-2012, 02:08 AM   PM User | #1
mharrison
New Coder

 
Join Date: Dec 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
mharrison is an unknown quantity at this point
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?
mharrison is offline   Reply With Quote
Old 12-13-2012, 08:33 AM   PM User | #2
poyzn
Regular Coder

 
poyzn's Avatar
 
Join Date: Nov 2010
Posts: 265
Thanks: 2
Thanked 61 Times in 61 Posts
poyzn is on a distinguished road
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>
__________________
Ushousebuilders.com
poyzn is offline   Reply With Quote
Old 12-13-2012, 12:52 PM   PM User | #3
mharrison
New Coder

 
Join Date: Dec 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
mharrison is an unknown quantity at this point
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.
mharrison is offline   Reply With Quote
Old 12-13-2012, 01:23 PM   PM User | #4
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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.
__________________
"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

Last edited by AndrewGSW; 12-13-2012 at 01:35 PM..
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
mharrison (12-13-2012)
Old 12-13-2012, 01:33 PM   PM User | #5
mharrison
New Coder

 
Join Date: Dec 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
mharrison is an unknown quantity at this point
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.
mharrison is offline   Reply With Quote
Old 12-13-2012, 01:40 PM   PM User | #6
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Old 12-13-2012, 01:44 PM   PM User | #7
mharrison
New Coder

 
Join Date: Dec 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
mharrison is an unknown quantity at this point
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!
mharrison is offline   Reply With Quote
Old 12-13-2012, 01:47 PM   PM User | #8
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Old 12-13-2012, 01:59 PM   PM User | #9
mharrison
New Coder

 
Join Date: Dec 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
mharrison is an unknown quantity at this point
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.
mharrison is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:40 PM.


Advertisement
Log in to turn off these ads.