Same key in array

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
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Same key in array

Post 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();
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

When formatting a three-up style layout like this I have a counter determine when the end of row is reached.
(#10850)
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

How do you count the results as they flow in?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
// The counter
$c = 0;
while (somethingGoesOnHere()) {
  // Handle it
  $c++; // Increment the counter
}
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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;
  }
}
?>
(#10850)
Post Reply