1

I am coding a plugin for our company's stock price. I am determining growth as such:

public function __construct(){
   $this->EE =& get_instance();
   $fp = fopen ("http://finance.yahoo.com/d/quotes.csv?s=EDR&f=sl1c1pohgv&e=.csv","r");
   $retData = fgetcsv ($fp, 1000, ",");
   $this->return_data = round($retData[2]/$retData[3]*100,2);
}

where $retData[2] is "change" and $retData[3] is "previous close."

My returned data is something like -0.19% which I can output in a template using {exp:edrstock}%.

Now the question: I need to create a conditional set of if/else tags so that if the number is negative I output it in a div with a class of "negStock" or if it is even, I output it in a div with a class of "evenStock" and if it is positive, I output it in a div with a class of "posStock."

By the way, the class will determine what color the text is and whether or not it appears in the header (positive) or the footer (even or negative).

I would appreciate any guidance as I am teaching myself EE from a book and would like to learn this the right way.

Thanks in advance, Jon

1

You should create a tag pair:

$variables = array(
  'change' => $retData[2],
  'previous_close' => $retData[3],
  'percentage' => round($retData[2]/$retData[3]*100,2),
);
$this->return_data = $this->EE->TMPL->parse_variables_row($this->EE->TMPL->tagdata, $variables);

And then you could do this (AND you'd have access to these other variables, should you need them):

{exp:edrstock}
{if percentage >= 0}
  <span class="posStock">{percentage}%</span> (+${change})
{if:else}
  <span class="negStock">{percentage}%</span> (-${change})
{/if}
{/exp:edrstock}
| improve this answer | |
  • Works perfectly. – jgravois Apr 21 '13 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.