Page 1 of 1

Same key in array

Posted: Tue Sep 04, 2007 1:59 pm
by psurrena
I am working on returning my results into a three row table. I'll get the number of rows, divide it by 3 and limit the column by $key <= 1/3 of the total rows. Simple enough but when I use the method below, $key is always equal to 0 which kills my whole idea. Is there a better mysql_fetch_* for this use?

Code: Select all

<?php
	mysql_connect('localhost','','');
	mysql_select_db('test');

	echo "<h3>Multiple Row Test</h3>\n";
	$query="SELECT people_lname FROM people ORDER BY people_lname";
	$result=mysql_query($query) or die (mysql_error());

	while($row=mysql_fetch_array($result)){
		foreach($row as $key=>$value){
			echo $key."<br />\n";
		}
	}
	
	mysql_close();
?>

Posted: Tue Sep 04, 2007 2:18 pm
by Christopher
When formatting a three-up style layout like this I have a counter determine when the end of row is reached.

Posted: Tue Sep 04, 2007 2:23 pm
by psurrena
How do you count the results as they flow in?

Posted: Tue Sep 04, 2007 2:35 pm
by RobertGonzalez

Code: Select all

<?php
// The counter
$c = 0;
while (somethingGoesOnHere()) {
  // Handle it
  $c++; // Increment the counter
}
?>

Posted: Tue Sep 04, 2007 3:41 pm
by Christopher

Code: Select all

<?php
// The counter
$max = 3;
$c = 0;
while (somethingGoesOnHere()) {
  // Handle it
  $c++; // Increment the counter
  if ($c >= $max) {
    // new row
    $c = 0;
  }
}
?>