Jump to content

PHP Code Question


f500designs

Recommended Posts

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.)";
    
?> 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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]));?><

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.