Page 1 of 1

[SOLVED] how to generate records in a horizontal fashion

Posted: Thu Jul 01, 2004 12:37 am
by aj2000
hi,

im trying to generate info from a database and it should list the items in a horizontal fashion with 3 records per row ...so it would look like this:


field1 field1 field1
field2 field2 field2


field1 field1
field2 field2

.... and so on..


so far i have it generating 1 row...but it does return to the second row ..the site can be found at http://www.financialshopper.com/categor ... y_test.php

and here is my code so far:


Code: Select all

$query_cat_d = "SELECT c_name, c_image from category"; 
$result_cat_d = mysql_query($query_cat_d);

 

	echo ("<TR>");
	
	for ($j = 0; $j < mysql_num_rows($result_cat_d); $j++) { 
	
	
	
		$rowarray = mysql_fetch_row($result_cat_d);
		$field_num = mysql_num_fields($result_cat_d);
	
	//for ($j = 0; $j < $field_num; $j++) {
		$remainder = mysql_num_rows($result_cat_d) % 3 
		
		echo ("<TD>");
    	echo ("<TABLE border=1>");
		echo ("<TR><TD>") . $rowarray[1] . ("</TD></TR>");
		echo ("<TR><TD>") . $rowarray[0] . ("</TD></TR>");
		echo ("</TABLE>");
		echo ("</TD>");
	//}
	 
	 
	 
	}  	
	
	echo ("</TR>");
any help would be appreciated

Posted: Thu Jul 01, 2004 9:57 am
by feyd
in order to do this you'll need to store off all the data, and then stripe it back out.
(example)

Code: Select all

<?php

$query = mysql_query("SELECT col1, col2 FROM table") or die(mysql_error());

if(!mysql_num_rows($query)) die("No data found.");

$data = array();
while($data[] = mysql_fetch_assoc($query));

$keys = array_keys($data[0]);
$num = count($data);
echo '<table>';
foreach($keys as $key)
{
  echo '<tr><td>'.$key.'</td>';
  for($x = 0; $x < $num; $x++)
  {
    echo '<td>'.($data[$x][$key]).'</td>';
  }
  echo '</tr>'."\n";
}
echo '</table>';

?>

displaying db results in a horizontal fashion

Posted: Fri Jul 02, 2004 7:31 pm
by aj2000
Hi, i tried your script..i still ended up with more than 3 entries on the row..im trying to get it so that 3 records are shown on the first row and then the next 3 on the second row...and so on.

Posted: Fri Jul 02, 2004 8:28 pm
by feyd
for that.. you'll need a counter or two, to limit the and control the number displayed per row..

Posted: Sat Jul 03, 2004 1:54 pm
by aj2000
Hi! i finally got this to work. for anyone else with this same issue, heres what i have:

Code: Select all

$tmp_cnt = 0;   // temporary variable
$per_row = 3; 
	
	for ($j = 0; $j < mysql_num_rows($result_cat_d); $j++) { 
	
	//echo ("<TR>");
	
	
	if ($tmp_cnt % $per_row == 0)
	echo ('<tr>'); 
	
		$rowarray = mysql_fetch_row($result_cat_d);
		$field_num = mysql_num_fields($result_cat_d);
	
	 	
		echo ("<TD>");
    	echo ("<TABLE border=1>");
		echo ("<TR><TD>") . $rowarray[1] . ("</TD></TR>");
		echo ("<TR><TD>") . $rowarray[0] . ("</TD></TR>");
		echo ("</TABLE>");
		echo ("</TD>");
	//}
	 $tmp_cnt = $tmp_cnt + 1;
	 if ($tmp_cnt % $per_row == 0)
	echo ('</tr>'); 
	
	
	 
	}