views:

51

answers:

3

I have array data as shown below, I want to store 'sale' value into database if my currency are USD.

 Array ( [currency] =>  USD [sale] => 9120.00  [buy] => 8970.00  ) 
 Array ( [currency] =>  SGD [sale] => 6653.75  [buy] => 6520.75  ) 
 Array ( [currency] =>  HKD [sale] => 1173.40  [buy] => 1152.10  ) 
 Array ( [currency] =>  CHF [sale] => 8685.40  [buy] => 8517.40  ) 
 Array ( [currency] =>  GBP [sale] => 13958.80  [buy] => 13675.80  ) 
 Array ( [currency] =>  AUD [sale] => 8054.05  [buy] => 7885.05  ) 
 Array ( [currency] =>  JPY [sale] => 105.10  [buy] => 102.40  ) 
 Array ( [currency] =>  SEK [sale] => 1247.90  [buy] => 1216.80  ) 
 Array ( [currency] =>  DKK [sale] => 1590.95  [buy] => 1547.75  ) 
 Array ( [currency] =>  CAD [sale] => 8756.35  [buy] => 8568.35  ) 
 Array ( [currency] =>  EUR [sale] => 11776.75  [buy] => 11555.75  ) 
 Array ( [currency] =>  SAR [sale] => 2441.35  [buy] => 2382.35  ) 
 Array ( [currency] =>  NZD [sale] => 6550.75  [buy] => 6389.75  ) 
 Array ( [currency] =>  CNY [sale] => 1346.75  [buy] => 1322.35  )

Thanks.

Here my code:

foreach ($cur_array as $curs) {
    print_r ($curs)."<br>";
    if ($curs['currency'] = "USD") {
        $curs_sale = $curs['sale'];
            $curs_name = $curs['currency'];
            $db->query("UPDATE currency SET rate = '".
                $curs_sale."' WHERE currency_id ='".$curs_name."'");
    }
}
A: 

Assuming all the entries are in an array called $entries:

foreach ($entries as $entry) {
    if ($entry['currency'] == 'USD') {
        // do stuff with $entry['sale']
    }
}
Pradador
+2  A: 

You could just store all the currency arrays in an array, loop through it, check which of the currencies corresponds to USD and save that currency's sale value:

foreach ($currencies as $curr)
{
    if ($curr['currency'] == 'USD')
    {
        // Save $curr['sale'] into your database

        // Break as further iteration isn't needed
        break;
    }
}

In future, please don't mark questions as community wiki if you don't know what community wiki means.

BoltClock
I did the same but i get wrong value.
Chandra
What do you mean by wrong value?
BoltClock
I'm really sorry about Community Wiki .Stored value are the last value of $cur_sale. when i add break statement i can store the right value. But how if i want to store both currency_name and sale value. Thanks.
Chandra
`SET currency_name = '" . $curs_name . "', rate = '" . $curs_sale . "' WHERE currency_id = '" . $curs_name . "'` although I'm not sure which other variables you have, as you seem to be using `$curs_name` for `currency_id` as well. By the way, be sure to [escape your data](http://www.php.net/manual/en/function.mysql-real-escape-string.php).
BoltClock
This what i want:$db->query("UPDATE currency SET rate = '".$curs_sale."' WHERE currency_id ='".$curs_name."'");<br> Thanks for your suggestion.
Chandra
How is that storing both currency name and sale value?
BoltClock
You right I'm missed copy of old code.
Chandra
+1  A: 

What BoltClock proposed is correct. Furthermore, consider altering the structure of your array from:

Array ( [currency] =>  USD [sale] => 9120.00  [buy] => 8970.00  ) 
Array ( [currency] =>  SGD [sale] => 6653.75  [buy] => 6520.75  ) 
Array ( [currency] =>  HKD [sale] => 1173.40  [buy] => 1152.10  ) 
Array ( [currency] =>  CHF [sale] => 8685.40  [buy] => 8517.40  ) 

to:

Array (
    [USD] => Array( [sale] => 9120.00  [buy] => 8970.00 ),
    [SGD] => Array( [sale] => 6653.75  [buy] => 6520.75 ),
    [HKD] => Array( [sale] => 1173.40  [buy] => 1152.10 ),
    [CHF] => Array( [sale] => 8685.40  [buy] => 8517.40 )
)

This structure will allow you to write cleaner code, for example:

$usd_sale = $entries['USD']['sale'];
Anax
+1 using currency names as keys is more semantic.
BoltClock
But it might not adequately map to a key for the data - this looks like it might be a table of orders or products, in which case currency is not the right key for the entry. Can you imagine making Currency the primary key for a similar table of entities like this in a database?
Shabbyrobe
@Shabbyrobe: table Currencies(int id, varchar name, float sale, float buy), code: `while ($line = mysql_fetch_array($result)) $entries[$line['name']] = array('sale'=>$line['sale'], 'buy'=>$line['buy']);`
Anax