dreampho Posted January 23, 2013 Share Posted January 23, 2013 Hi. I am new to PHP, and am testing a few things in a Expression Engine module. I have a database called 'currency', and I want to retrieve some data from it and place the data into variables. How can I do this? Here is what I currently have. public function convert() { $price = $this->EE->TMPL->fetch_param('price'); if( $price === FALSE ) { return ""; } $tagdata = $this->EE->TMPL->tagdata; $this->EE->db->select( "usd, eur, gbp" ); $query = $this->EE->db->get( "currency" ); $usd = ''; $eur = ''; $gbp = ''; $usd_convert = $price * $usd; $eur_convert = $price * $eur; $gbp_convert = $price * $gbp; return $usd; } Link to comment https://forums.phpfreaks.com/topic/273540-get-data-from-database-into-variable/ Share on other sites More sharing options...
Jessica Posted January 23, 2013 Share Posted January 23, 2013 This sounds like a third-party question, so I'm moving it there. Check "Expression Engine"'s website and documentation as well. Link to comment https://forums.phpfreaks.com/topic/273540-get-data-from-database-into-variable/#findComment-1407717 Share on other sites More sharing options...
albertdiones Posted February 4, 2013 Share Posted February 4, 2013 If you got a currency table, with columns usd, eur, gbp: public function convert() { $price = $this->EE->TMPL->fetch_param('price'); if( $price === FALSE ) { return array(); } $query = $this->EE->db->select( "usd, eur, gbp" )->get( "currency" ); $currency_values = $query->num_rows() ? array_shift($query->result()) : false; // Only if there are results if (!$currency_values) { throw new Exception("No currency row found in the database"); } foreach ($currencies as $currency => $currency_values) { if (!$currency_values) { throw new Exception("$currency currency has blank value"); } } $return = array( 'usd' => $price * $currency_values['usd'], 'eur' => $price * $currency_values['eur'], 'gbp' => $price * $currency_values['gbp'] ); return $return; } I got no idea actually about "EE" but I made that based on the documentation hoping it'll help or give you idea at least. Btw. having a currency table with only one row doesn't seem to make sense to me, I could have understood it wrong though, but if I didn't, I suggest having a currency row with ( currency varchar, usd_value decimal ) table instead Link to comment https://forums.phpfreaks.com/topic/273540-get-data-from-database-into-variable/#findComment-1409972 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.