I was having trouble with returning PDO query results in arrays, due to the structure of the results of those queries (query array with arrays for results inside). Now when executing a fetchAll() on a query you get the following:
Array ( [0] = Array ( [key1] => "value1", [key2] => "value2") [1] = Array( [key1] => "value1", [key2] => "value2") )
This is a smaller array, but return results in a HUGE database this way and it quickly becomes IMPPOSIBRU to read out the array given._
This is where the glorious foreach comes in! We all know the basic writing of a foreach (foreach ($array as $key => $value) {...}), but with query results there's a twist, as both the $key AND $value variables are arrays, where $key is the array of the query containing the result arrays and $value is/are the result array(s) themselves.
So in this foreach we have to talk directly to $value to get our results! This would look something like this:_
foreach ($query as $key => $value) {
return $value["key2"];
}
?>_
This already does the job, but to return them in arrays we have to change our return code a little bit, like so:_
foreach ($query as $key => $value) {
return array (
"key1" => $value["key1"],
"key2" => $value["key2"]
);
}
?>_
Array ( [0] = Array ( [key1] => "value1", [key2] => "value2") [1] = Array( [key1] => "value1", [key2] => "value2") )0
Array ( [0] = Array ( [key1] => "value1", [key2] => "value2") [1] = Array( [key1] => "value1", [key2] => "value2") )1
Array ( [0] = Array ( [key1] => "value1", [key2] => "value2") [1] = Array( [key1] => "value1", [key2] => "value2") )2
to print an array, simply use print_r(array name)
like this:
$myrow = mysql_fetch_row($result);
echo "
print_r($myrow);
echo "";
this will output the array in a readable form, with the index, too. Don't forget the 'pre' tags or the output will be on a single line.
One of the most common mistakes that people make with this function, when using it multiple times in one script, is that they forget to use the mysql_data_seek() function to reset the internal data pointer.
When iterating through an array of MySQL results, e.g.
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
foreach ($line as $col_value) {
echo $col_value . '
';
}
}
?>
the internal data pointer for the array is advanced, incrementally, until there are no more elements left in the array. So, basically, if you copy/pasted the above code into a script TWICE, the second copy would not create any output. The reason is because the data pointer has been advanced to the end of the $line array and returned FALSE upon doing so.
If, for some reason, you wanted to interate through the array a second time, perhaps grabbing a different piece of data from the same result set, you would have to make sure you call
mysql_data_seek($result, 0);
?>
This function resets the pointer and you can re-iterate through the $line array, again!
Loop foreach hanya berfungsi pada array, dan digunakan untuk mengulang setiap pasangan kunci/nilai dalam array
Sintaksis
foreach ($array sebagai $nilai) {
kode yang akan dieksekusi;
}
Untuk setiap iterasi loop, nilai elemen array saat ini ditetapkan ke $value dan penunjuk array dipindahkan satu per satu, hingga mencapai elemen array terakhir
The for_ loop - Loops melalui blok kode beberapa kali tertentu
PHP untuk Loop
Loop for digunakan saat Anda mengetahui sebelumnya berapa kali skrip harus dijalankan
Sintaksis
for (penghitung init; penghitung tes; penghitung kenaikan) {
kode yang akan dieksekusi untuk setiap iterasi;
}
Parameter
- penghitung init. Inisialisasi nilai penghitung loop
- penghitung tes. Dievaluasi untuk setiap iterasi loop. Jika hasilnya BENAR, pengulangan berlanjut. Jika bernilai FALSE, loop berakhir
- penghitung kenaikan. Meningkatkan nilai penghitung loop
Contoh
Contoh di bawah menampilkan angka dari 0 sampai 10
Contoh
untuk ($x = 0; $x
Cobalah sendiri "Contoh Dijelaskan
- $x = 0;
- $x Cobalah sendiri "
Contoh Dijelaskan
- $x = 0;
- $x query($query);