Page 1 of 1

Two columns

Posted: Fri Mar 26, 2004 6:03 am
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

Posted: Fri Mar 26, 2004 6:32 am
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.

Posted: Fri Mar 26, 2004 6:35 am
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.

Posted: Fri Mar 26, 2004 6:43 am
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

Posted: Fri Mar 26, 2004 7:32 am
by cto1mac
That code almost worked it still displayed everything on the same row.

Posted: Fri Mar 26, 2004 7:35 am
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>";

Posted: Fri Mar 26, 2004 7:43 am
by cto1mac
Dude, seriously you rock!! Thanks alot for the help.

Posted: Fri Mar 26, 2004 8:03 am
by magicrobotmonkey
Thanks, now, do you understand how that works?

Posted: Fri Mar 26, 2004 9:30 am
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.