Two columns

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cto1mac
Forum Commoner
Posts: 54
Joined: Tue Jan 27, 2004 6:11 am
Location: Virginia Beach, VA

Two columns

Post by cto1mac »

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
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

When looping through do something like

if($index%2==0)
echo "</tr><tr>";

$index%2 returns the remainder of $index/2 so if its 0 you know its an even number so you start a new row.
User avatar
cto1mac
Forum Commoner
Posts: 54
Joined: Tue Jan 27, 2004 6:11 am
Location: Virginia Beach, VA

Post by cto1mac »

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:

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) &#123;
	$display_block = "<p>No records returned.</p>";
&#125; else &#123;
	while ($info = mysql_fetch_array($get_info_res)) &#123;
	                $id = $info&#1111;'id'];
		$fname = $info&#1111;'fname'];
			
			
			$display_block .= "<tr>
				<td>$id</td>
				<td>$fname</td>
				</tr>";
			
				
		&#125;
			
&#125;
?>
<?php print $display_block; ?>
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.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

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:

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; 

?>
Give that a try
User avatar
cto1mac
Forum Commoner
Posts: 54
Joined: Tue Jan 27, 2004 6:11 am
Location: Virginia Beach, VA

Post by cto1mac »

That code almost worked it still displayed everything on the same row.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

hmmm... it didnt give any errors? just displayed on the same row huh? let me inspect my logic. and dont get fresh im trying to help you. oh shoot, i see what i did woops!

echo "</tr><tr>";

should be

$display_block.="</tr><tr>";
User avatar
cto1mac
Forum Commoner
Posts: 54
Joined: Tue Jan 27, 2004 6:11 am
Location: Virginia Beach, VA

Post by cto1mac »

Dude, seriously you rock!! Thanks alot for the help.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Thanks, now, do you understand how that works?
User avatar
cto1mac
Forum Commoner
Posts: 54
Joined: Tue Jan 27, 2004 6:11 am
Location: Virginia Beach, VA

Post by cto1mac »

Yes, actually I do. I had tried something like that about 2 days ago but couldn't get it to work. So after banging my head into the wall over it I went to the forums. Again thanks for the help.
Post Reply