CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Split results into 2 columns (http://www.codingforums.com/showthread.php?t=284063)

mharrison 12-13-2012 02:08 AM

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?

poyzn 12-13-2012 08:33 AM

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>


mharrison 12-13-2012 12:52 PM

Quote:

Originally Posted by poyzn (Post 1299573)
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.

AndrewGSW 12-13-2012 01:23 PM

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.

mharrison 12-13-2012 01:33 PM

Quote:

Originally Posted by AndrewGSW (Post 1299612)
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.

AndrewGSW 12-13-2012 01:40 PM

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.

mharrison 12-13-2012 01:44 PM

Quote:

Originally Posted by AndrewGSW (Post 1299614)
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!

AndrewGSW 12-13-2012 01:47 PM

No worries :thumbsup:

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.

mharrison 12-13-2012 01:59 PM

Quote:

Originally Posted by AndrewGSW (Post 1299617)
No worries :thumbsup:

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.


All times are GMT +1. The time now is 04:42 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.