f500designs Posted December 2, 2008 Share Posted December 2, 2008 This code takes stock information off of Yahoo! Finance's website and outputs it into text as you see below in the echo() statement. It works perfectly. Now what I am trying to do is take the "Today's change" result and determine rather it is positive or negative. The "Today's change" outputs as $contents[4] How do I do this? Thanks! Here is my code below: <?php $ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, 'http://download.finance.yahoo.com/d/quotes.csv?s=USO&f=sl1d1t1c1ohgv&e=.csv' ); curl_setopt( $ch, CURLOPT_HEADER, false ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); $output = curl_exec( $ch ); curl_close( $ch ); $contents = explode( ',', str_replace( '"', '', $output ) ); echo "US Oil Fund</p><p>Most recent trade: <b>\$$contents[1]</b></p><p>Today's Change: $contents[4]</p><p>(Prices delayed up to 20 minutes.)"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/135087-php-code-question/ Share on other sites More sharing options...
xtopolis Posted December 2, 2008 Share Posted December 2, 2008 Is this something like <?php if($content[4] >= 0){ //positive }else{ //negative } ?> Or am I missing something here? Quote Link to comment https://forums.phpfreaks.com/topic/135087-php-code-question/#findComment-703622 Share on other sites More sharing options...
f500designs Posted December 2, 2008 Author Share Posted December 2, 2008 i've been trying that... tell me where i am going wrong... if($content[4] >= 0){ $status=='high'; }else{ $status=='low'; } Quote Link to comment https://forums.phpfreaks.com/topic/135087-php-code-question/#findComment-703623 Share on other sites More sharing options...
xtopolis Posted December 2, 2008 Share Posted December 2, 2008 You're using == instead of = == -> compare = -> assign $status = ($content[4] >= 0) ? 'high' : 'low'; (I think) Quote Link to comment https://forums.phpfreaks.com/topic/135087-php-code-question/#findComment-703624 Share on other sites More sharing options...
f500designs Posted December 2, 2008 Author Share Posted December 2, 2008 you are genius!!! Thanks so much!! Quote Link to comment https://forums.phpfreaks.com/topic/135087-php-code-question/#findComment-703625 Share on other sites More sharing options...
f500designs Posted December 2, 2008 Author Share Posted December 2, 2008 quick question to wrap things up... $status will either output 'red' or 'green'... i need it to output in the middle of calling out a image file name (i.e. redarrow.jpg or greenarrow.jpg) what do i place in the area where there are ?????'s $status = ($content[4] >= 0) ? 'green' : 'red'; echo "US Oil Fund (USO)<p>$$contents[1] <img src='images/??????arrow.jpg'> $contents[4]; Quote Link to comment https://forums.phpfreaks.com/topic/135087-php-code-question/#findComment-703631 Share on other sites More sharing options...
xtopolis Posted December 2, 2008 Share Posted December 2, 2008 $status = ($content[4] >= 0) ? 'green' : 'red'; echo "US Oil Fund (USO)<p>$$contents[1] <img src='images/".$status."arrow.jpg'> $contents[4]"; You may also want to change the last part, $contents[4], to ".abs($contents[4]); since you are going to be adding your own positive/negative sign. Quote Link to comment https://forums.phpfreaks.com/topic/135087-php-code-question/#findComment-703636 Share on other sites More sharing options...
DeanWhitehouse Posted December 2, 2008 Share Posted December 2, 2008 <?php $status = ($content[4] >= 0) ? 'green' : 'red'; echo "US Oil Fund (USO)<p>$$contents[1] <img src='images/".$status."arrow.jpg'> $contents[4]; ?> Edit: beat me to it Quote Link to comment https://forums.phpfreaks.com/topic/135087-php-code-question/#findComment-703638 Share on other sites More sharing options...
xtopolis Posted December 2, 2008 Share Posted December 2, 2008 We're both wrong, we both left of the s in contents for the status line, my bad. $status = ($contents[4] >= 0) ? 'green' : 'red'; Quote Link to comment https://forums.phpfreaks.com/topic/135087-php-code-question/#findComment-703640 Share on other sites More sharing options...
f500designs Posted December 2, 2008 Author Share Posted December 2, 2008 that abs($content[4]) didn't work... but i fandangled it a bit... BUT i need it to stay two decimal places... when the number is 2.20 it changes it to 2.2 here's the code updated $status = ($contents[4] >= 0) ? 'green' : 'red'; $abscontent = abs($contents[4]); echo "US Oil Fund (USO)<p>$$contents[1] <img src='images/".$status."arrow.jpg'> $abscontent"; Quote Link to comment https://forums.phpfreaks.com/topic/135087-php-code-question/#findComment-703644 Share on other sites More sharing options...
xtopolis Posted December 2, 2008 Share Posted December 2, 2008 Ya, I was working on that. I'm not sure how to carry the precision, so I cheated: $contents = explode( ',', str_replace( '"', '', $output ) ); $status = ($contents[4] >= 0) ? 'green' : 'red'; echo "US Oil Fund (USO)<p>$$contents[1] <img src='images/".$status."arrow.jpg'> ".str_replace('-','',$contents[4]); str_replace the "-" sign. Quote Link to comment https://forums.phpfreaks.com/topic/135087-php-code-question/#findComment-703647 Share on other sites More sharing options...
xtopolis Posted December 2, 2008 Share Posted December 2, 2008 Err, found it: sprintf echo "US Oil Fund (USO)<p>$$contents[1] <img src='images/".$status."arrow.jpg'> ".sprintf("%01.2f", abs($contents[4])); Quote Link to comment https://forums.phpfreaks.com/topic/135087-php-code-question/#findComment-703648 Share on other sites More sharing options...
f500designs Posted December 2, 2008 Author Share Posted December 2, 2008 Err, found it: sprintf echo "US Oil Fund (USO)<p>$$contents[1] <img src='images/".$status."arrow.jpg'> ".sprintf("%01.2f", abs($contents[4])); WORKS!!! Quote Link to comment https://forums.phpfreaks.com/topic/135087-php-code-question/#findComment-703650 Share on other sites More sharing options...
f500designs Posted December 2, 2008 Author Share Posted December 2, 2008 Makeshift Stock Ticker without all of the stupid advertisements that widgets provide... Thank you for all of your help!! <?php $ch = curl_init(); $symbol = 'USO'; curl_setopt( $ch, CURLOPT_URL, "http://download.finance.yahoo.com/d/quotes.csv?s=".$symbol."&f=sl1d1t1c1ohgv&e=.csv" ); curl_setopt( $ch, CURLOPT_HEADER, false ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); $output = curl_exec( $ch ); curl_close( $ch ); $contents = explode( ',', str_replace( '"', '', $output ) ); $status = ($contents[4] >= 0) ? 'green' : 'red'; $abscontent = abs($contents[4]); echo "US Oil Fund (USO)<p>$$contents[1] <img src='images/".$status."arrow.jpg'> ".sprintf("%01.2f", abs($contents[4]));?>< Quote Link to comment https://forums.phpfreaks.com/topic/135087-php-code-question/#findComment-703653 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.