Two columns
Moderator: General Moderators
Two columns
Ok, maybe it is just early but I cannot figure this one out.
I have a mysql table that I need to pull info and display it in two columns of a table. But I need record #1 in the first cell and record 2 in the second cell but the third in the next row first cell. i.e.
<table>
<tr><td>$record1</td><td>$record2</td></tr>
<tr><td>$record3</td><td>$record4</td></tr>
....
</table>
I can get one record per row all the records on one row but not this. Please help!
Thanks
I have a mysql table that I need to pull info and display it in two columns of a table. But I need record #1 in the first cell and record 2 in the second cell but the third in the next row first cell. i.e.
<table>
<tr><td>$record1</td><td>$record2</td></tr>
<tr><td>$record3</td><td>$record4</td></tr>
....
</table>
I can get one record per row all the records on one row but not this. Please help!
Thanks
-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
Ok, maybe I am not explaining my problem correctly. How do I write the php/mysql to output it that way. normally I do it this way:
This is probobly not the best way to output the info I need but it is the only way I know of. So please help.
Code: Select all
$get_info = "SELECT * FROM table1";
$get_info_res = mysql_query($get_info) or die(mysql_error());
if (mysql_num_rows($get_info_res) < 1) {
$display_block = "<p>No records returned.</p>";
} else {
while ($info = mysql_fetch_array($get_info_res)) {
$id = $infoї'id'];
$fname = $infoї'fname'];
$display_block .= "<tr>
<td>$id</td>
<td>$fname</td>
</tr>";
}
}
?>
<?php print $display_block; ?>-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
oh ok i see what you're doing. So each record has two display items and you want to display two records per row for four total items? Ok, try this:
Give that a try
Code: Select all
<?php
$get_info = "SELECT * FROM table1";
$looper=0; //use this to keep track of rows
$get_info_res = mysql_query($get_info) or die(mysql_error());
if (mysql_num_rows($get_info_res) < 1) {
$display_block = "<p>No records returned.</p>";
} else {
$display_block .= "<tr>" //to start a row
while ($info = mysql_fetch_array($get_info_res)) {
$index++; //increment index
$id = $info['id'];
$fname = $info['fname'];
$display_block .= "
<td>$id</td>
<td>$fname</td>";
if($index==2){
echo "</tr><tr>"; //end and start row every other time
$index = 0;
}
}
$display_block.="</tr>"; //end the last row
}
?>
<?php print $display_block;
?>-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA