Page 1 of 1

Looping through an assoc array - syntax prob?

Posted: Mon May 12, 2003 12:45 am
by shylock
My logic circuits are misbehaving.

I am attempting to navigate an assoc array called through mysql_fetch_assoc on a recordset. Then print out 3 records in an html table row for the entire set of X records.

If I am reading the man correctly, I can reference the key pointer as well as the actual field name, such that I can then use a for loop to reference 3 data 'chunks' at a time, then move the recordset pointer and ref another 3.

Here is my understaning of the assoc reference:
$result_array[$array_pointer]['field1']

and here is some hypothetical code (which doesn't work in context of my application).

Code: Select all

<?php
$query_result = mysql_query("SELECT field1, field2 FROM table WHERE field1 = $formvar");

$result_array = mysql_fetch_assoc($query_result);

$num_recs = mysql_num_rows($query_result);

$table_rows = round($num_recs/3);

			echo "<table width=95% border=0 cellpadding=3 cellspacing=2>";
			//set counter for 3 sets of data
			$counter = 3;
			// set result counter for first round of 3s
			$result_set = 0;
			//loop for each row
			for ($row = 1; $row< $table_rows; $row++){
					echo "<tr>";
					// loop for each set of 3 data chunks
					for ($array_pointer = $result_set;$array_pointer < $counter;$array_pointer++){
					if ($counter <= $num_recs){
						echo "<td align=center bgcolor=#FFFFFF>";

						echo $result_array[$array_pointer]['field1']\n;
						echo $result_array[$array_pointer]['field2']\n;

						echo "</td>";
					} //if
					} //2nd for
				echo "</tr>";
				//set the result set 3 more rows
				$result_set = $result_set+3;
				//set the counter 3 more steps if there are rows left and then move the result pointer 3
				$counter = $counter+3;
				if ($counter <= ($num_recs -3)){
				mysql_data_seek($query_result, $counter);
				$result_array = mysql_fetch_assoc($result_array);
				} else if ($counter = $num_recs){
				exit;
				} else {
				mysql_data_seek($query_result, $counter+1);
				$result_array = mysql_fetch_assoc($query_result);
				}
			}//1st for
			
			echo "</table>";
?>
I am not getting the desired results. Basically no data.
I tried different syntax: $result_set[$array_pointer => 'field1'] //no go

the above works if I take out the [$array_pointer] part but only prints one record (obviously).

could be simpler way of doing this I'm sure, but if someone can wag a finger and say "syntax is this:" or give me a man page ref to scrutinize, I would be grateful.

Thanks
Shylock

Posted: Mon May 12, 2003 5:29 am
by volka
$result_array = mysql_fetch_assoc($query_result);
returns a one-dimensional array. what is $array_pointer?

Posted: Mon May 12, 2003 8:36 am
by shylock
$array_pointer = key #

The array should be between 2 and 13 records in a table, so in my thinking I should be able to point to record 1 ['field name1'], record 1 ['field name 2'], then second loop: record 2 ['field name1'], record 2 ['field name 2'] and so on.
No?

I can do a while ($result_array = mysql_fetch_assoc($result)) and get through them fine. just need to do this via 3 record chunks. making sense?

-Dense

Posted: Mon May 12, 2003 9:21 am
by volka
mysql_fetch_... will give you the next record, but only one at a time.
the return value of mysql_fetch_assoc is an array [fieldname=>value] of that record, not an array of arrays for the complete recordset.
Today is not my most logical day :roll: can you please explain what you're trying to achieve (not your algorithm but the goal)?
I think I know but not for sure...

Posted: Mon May 12, 2003 10:40 am
by shylock
OK. Fairly simple.

1. Pull recordset
2. print each record's fields in a table cell and throw a TR tag on after 3

just writing this gives me a different idea on how to do it. maybe I'm trying to do too much.

Posted: Mon May 12, 2003 11:42 am
by volka
exactly what I thought ;)

Code: Select all

if ($cell++ == 3) { echo '</tr><tr>'; $cell=0; }
// or
if (++$cell % 3 == 0) { echo '</tr><tr>'; $cell=0; }
but I guess you already found that.

Posted: Tue May 13, 2003 1:38 pm
by shylock
absolutely what popped into my brain! thanks

works like a charm. except for when recordset does not come out just right for sets of 3. I'm working on a little test for that to throw in a colspan=X tag in the last cell, but I'm not having the most logical day.

If anyone has figured that one out before and would like to share that snippet, my remaining brain cells would be grateful.

-a

Posted: Tue May 13, 2003 3:33 pm
by volka
maybe

Code: Select all

...
<table border="1">
	<tr><th>1</th><th>2</th><th>3</th>
<?php
$c = -1;
while($row = mysql_fetch_row($result))
{
	if(++$c%3 == 0)
		echo '</tr><tr>';
	echo '<td>', 	$row[0], '</td>';
}
?>
	</tr>
</table>