$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?
$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.
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> </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..
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.
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
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!
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
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.