|
Posted by Hendri Kurniawan on 01/04/07 22:48
<?php
// initialize array
$vals = array();
// Fetch data from database, put them into associative array
//
// I am assuming the SQL will be something like:
// SELECT comparableValue, otherInformation1, otherInformation2 FROM
sometable
//
// Thus. when fetching result:
// $temp[0] = comparableValue
// $temp[1] = otherInformation1
// $temp[2] = otherInformation2
//
// With associative array, the newer values will always
// re-write the older value. See example
//
// Say you fecth the row, it returned array('value1', 'someinfo',
'someinfo')
// I will put them into assoc array, where 'value1' will be the key.
// The next fetch, it returns array('value1', 'someinfoagain',
'someinfotoo')
// If I put them into the same associative array, with the same key,
// it will over-write the previous value.
// and so on.
//
// Try first. If still don't understand. please ask again in ng
//
// PS. for Craig. 2 pass loop is not an elegant solution. But it's just
an advice
//
while ($temp = fetch_row_from_db()) $vals[$temp[0]] = $temp;
// This just dump the whole array to screen
var_dump($vals);
?>
Hendri Kurniawan
bill wrote:
> Hendri Kurniawan wrote:
>> Another way to do it VIA array
>>
>> <?php
>> $vals = array();
>> while ($temp = fetch_row_from_db()) $vals[$temp[0]] = $temp;
>> var_dump($vals);
>> ?>
>>
>> This way, you are sure to get the last row of every value.
>>
>> Hendri Kurniawan
>>
>
> Hendri,
> Would you please take pity on a PHP newbie and explain your code.
> It is elegant and dense I can't quite make sense of it.
> Shouldn't there be a bracket after the while statement ?\
>
> bill
[Back to original message]
|