Jump to content


Photo

Get data from database into variable


  • Please log in to reply
2 replies to this topic

#1 dreampho

dreampho

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 23 January 2013 - 08:17 AM

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;
}


#2 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 23 January 2013 - 08:33 AM

This sounds like a third-party question, so I'm moving it there. Check "Expression Engine"'s website and documentation as well.
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#3 albertdiones

albertdiones

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 03 February 2013 - 10:23 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com