faqts : Computers : Programming : Languages : PHP : Common Problems : Arrays

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 8 people (88%) answered Yes
Recently 6 of 7 people (86%) answered Yes

Entry

I want to display the results of one column in MySQL into a table with 3 columns and 2 rows. How ?

Jul 29th, 2004 21:56
Pravin Nirmal, raiden coolyo, Pravin


Actually it will form 3 columns and 2 rows only if you are having 6 
records.
Hence if you want 3 cloumns and no. of rows are more, then don't
restrict no. of rows.This code will form these 3 columns

<?
		$query1="select  FIELD_NAME from TABLE_NAME ";
		$result1=mysql_query($query1);
		$total1=mysql_num_rows($result1);
		if($total1)
		{
			echo"<table width=800 cellpadding=2 
cellspacing=2 border=1 bordercolor=1>";
			$i=0;
			while($row1=mysql_fetch_array($result1))
			{
				if($i%3==0)
				echo"<tr>";
					echo"<td>".$row1
["FIELD_NAME"]."</td>";
				$i++;
					if($i%3==0)
						echo"</tr>";	
			}
			echo"</table>";
		}
?>
Or use this to show results in 2 rows.In thsi case columns will 
increase as per no. of records.

<?
		$query1="select  FIELD_NAME from TABLE_NAME ";
		$result1=mysql_query($query1);
		$total1=mysql_num_rows($result1);
		if($total1)
		{
			$columns=$total1/2;
			echo"<table width=800 cellpadding=2 
cellspacing=2 border=1 bordercolor=1>";
			$i=0;
			while($row1=mysql_fetch_array($result1))
			{
				if($i%$columns==0)
				echo"<tr>";
					echo"<td>".$row1
["FIELD_NAME"]."</td>";
				$i++;
					if($i%$columns==0)
						echo"</tr>";	
			}
			echo"</table>";
		}
?>

		...................... Pravin Nirmal