Looping through an assoc array - syntax prob?
Posted: Mon May 12, 2003 12:45 am
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).
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
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 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