Enjoy an ad free experience by logging in. Not a member yet?
Register .
04-02-2004, 02:42 AM
PM User |
#1
Regular Coder
Join Date: Oct 2002
Location: USA
Posts: 255
Thanks: 1
Thanked 0 Times in 0 Posts
PHP Function - Image Selection
I have a stock indices page.
There I have three images: up arrow, down arrow, dash.
When the variables is positive, I want php to show the up arrow.
When the variables is negative, I want php to show the down arrow.
When the variables is 0, I want php to show the up dash.
The name of the variable is: $quote->change;
---
I used to use .js to handle this - which was as follows:
function upDown(which) {
if (which.indexOf("+") == -1) {
if (which.indexOf("-") == -1) {
return '<img src="dash.gif">';
} else {
return '<img src="down.gif">';
}
} else {
return '<img src="up.gif">';
}
}
Thanks!
04-02-2004, 06:40 AM
PM User |
#2
Senior Coder
Join Date: Feb 2004
Posts: 1,206
Thanks: 0
Thanked 0 Times in 0 Posts
Ok so what's the problem exactly? You want to know how to do this in PHP? Show the PHP code where you would be displaying the stock quote, and where the image should go. Then I can help you with it.
Sadiq.
04-02-2004, 09:52 AM
PM User |
#3
Regular Coder
Join Date: Oct 2002
Location: USA
Posts: 255
Thanks: 1
Thanked 0 Times in 0 Posts
Yes, I want to know how to do this in php.
Here's the function script:
<?php
Class yahoo
{
function get_stock_quote($symbol)
{
$url = sprintf("http://finance.yahoo.com/d/quotes.csv?s=%s&f=sl1d1t1c1ohgv" ,$symbol);
$fp = fopen($url, "r");
if(!fp)
{
echo "error : cannot recieve stock quote information";
}
else
{
$array = fgetcsv($fp , 4096 , ', ');
fclose($fp);
$this->symbol = $array[0];
$this->last = $array[1];
$this->date = $array[2];
$this->time = $array[3];
$this->change = $array[4];
$this->open = $array[5];
$this->high = $array[6];
$this->low = $array[7];
$this->volume = $array[8];
}
}
}
?>
Here's two examples of how I echo the variables (in a table cell):
<?php $findquote="^DJI"; $quote = new yahoo; $quote->get_stock_quote("$findquote"); echo $quote->change; ?>
<?php $findquote="^IXIC"; $quote = new yahoo; $quote->get_stock_quote("$findquote"); echo $quote->change; ?>
I want PHP to insert the right image in the table cell right next to it.
Thanks!
04-02-2004, 03:55 PM
PM User |
#4
Regular Coder
Join Date: Nov 2003
Location: mostly in Ann Arbor
Posts: 458
Thanks: 0
Thanked 0 Times in 0 Posts
assuming the varible $quote->change is a number, i'd just use greater than and less than comparisons.
PHP Code:
if ( $quote -> change > 0 ) {
echo '<img src=up.gif>' ;
} elseif ( $quote -> change < 0 ) {
echo '<img src=down.gif>' ;
} else {
echo '<img src=dash.gif>' ;
}
04-02-2004, 03:58 PM
PM User |
#5
Regular Coder
Join Date: Nov 2003
Location: mostly in Ann Arbor
Posts: 458
Thanks: 0
Thanked 0 Times in 0 Posts
if it isn't a number, i suppose you could use RegEx much the same way you used the .indexOf method
PHP Code:
if ( preg_match ( "/-/" , $quote -> change )) {
# neg
} elseif ( preg_match ( "/+/" , $quote -> change )) {
# pos
} else {
# zero
}
i think that'll work. you might get a syntax error. "-" and "+" maybe resereved, i dunno. post an error if you get one.
04-02-2004, 05:47 PM
PM User |
#6
Regular Coder
Join Date: Oct 2002
Location: USA
Posts: 255
Thanks: 1
Thanked 0 Times in 0 Posts
Yes, the variable is a NUMBER, and so the first script worked great.
Thanks!
I am trying to transform my last few scripts from .js to php.
So, how would you recommend me, for the same variable, to make the color red for negatives and black for positives and zero.
This is how I echo it:
<php $findquote="^AORD"; $quote = new yahoo; $quote->get_stock_quote("$findquote"); echo $quote->change; ?>
04-02-2004, 05:53 PM
PM User |
#7
Regular Coder
Join Date: Nov 2003
Location: mostly in Ann Arbor
Posts: 458
Thanks: 0
Thanked 0 Times in 0 Posts
maybe modify the previous script like this:
PHP Code:
if ( $quote -> change > 0 ) {
$color = "green" ;
$img = '<img src=up.gif>' ;
} elseif ( $quote -> change < 0 ) {
$color = "red" ;
$img = '<img src=down.gif>' ;
} else {
$color = "black" ;
$img = '<img src=dash.gif>' ;
}
echo "<FONT color='$color'>$quote->change $img</FONT>" ;
i realize font is deprecated, i'm assuming you can handle it anyway you please...style sheets included.
04-02-2004, 06:07 PM
PM User |
#8
Regular Coder
Join Date: Oct 2002
Location: USA
Posts: 255
Thanks: 1
Thanked 0 Times in 0 Posts
I understand the logic behind your script. but what I wanted was to make it as a separate script - which means:
the image selector does its job separately. (just echoes an image)
the "font color" script is just for the variable ($quote->change).
04-02-2004, 06:15 PM
PM User |
#9
Regular Coder
Join Date: Nov 2003
Location: mostly in Ann Arbor
Posts: 458
Thanks: 0
Thanked 0 Times in 0 Posts
okay then how about this:
PHP Code:
if ( $quote -> change > 0 ) {
$quote -> change = "<FONT color='green>$quote->change</FONT>" ;
} elseif ( $quote -> change < 0 ) {
$quote -> change = "<FONT color='red'>$quote->change</FONT>" ;
} else {
$quote -> change = "<FONT color='black'>$quote->change</FONT>" ;
}
echo $quote->change;
04-02-2004, 06:15 PM
PM User |
#10
Regular Coder
Join Date: Oct 2002
Location: USA
Posts: 255
Thanks: 1
Thanked 0 Times in 0 Posts
OK I got it!
if ($quote->change > 0) {
$color="#008000";
} elseif ($quote->change < 0) {
$color="#FF0000";
} else {
$color="#000000";
}
echo "<font color='$color'>$quote->change</font>";
Last edited by ssskaya; 04-02-2004 at 06:24 PM ..
04-02-2004, 06:23 PM
PM User |
#11
Regular Coder
Join Date: Oct 2002
Location: USA
Posts: 255
Thanks: 1
Thanked 0 Times in 0 Posts
One last request:
How can I make all the numbers separated by commas for each thousand digits?
And how can I make them have just two decimals (at all cases)?
php tutorial has number_format function for this, but I can't say I could figure it out.
thanks.
04-02-2004, 06:38 PM
PM User |
#12
Regular Coder
Join Date: Mar 2004
Location: Jackson, Georgia
Posts: 102
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
And how can I make them have just two decimals (at all cases)?
I guess you mean
90.89 instead of 90.897654 ect..
One way you can do this(im sure theres an easier way) is find what position the decimal point is located from the start of the string, add two and then allow that many chars to be printed to the page. Im very busy, i'll come up with the code in a min.
04-02-2004, 06:39 PM
PM User |
#13
Regular Coder
Join Date: Nov 2003
Location: mostly in Ann Arbor
Posts: 458
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:
if ( $quote -> change > 0 ) {
$color = "#008000" ;
} elseif ( $quote -> change < 0 ) {
$color = "#FF0000" ;
} else {
$color = "#000000" ;
}
$quote -> change = number_format ( $quote -> change , 2 );
echo "<font color='$color'>$quote->change</font>" ;
04-02-2004, 06:45 PM
PM User |
#14
Regular Coder
Join Date: Mar 2004
Location: Jackson, Georgia
Posts: 102
Thanks: 0
Thanked 0 Times in 0 Posts
heres what i threw togethor right quick. replace string with the value you want. in other words, if the value of the number is in a variable named $myvalue then replace $string = "24764.47464"; with $string = $myvalue;
Code:
<?php
$string = "24764.47464";
$pos = strpos($string, ".");
$allowedlen = $pos+3;
$value = substr($string,0, $allowedlen);
print $value;
?>
04-02-2004, 06:52 PM
PM User |
#15
Regular Coder
Join Date: Oct 2002
Location: USA
Posts: 255
Thanks: 1
Thanked 0 Times in 0 Posts
dswimboy:
how do you frame the scripts like you just did?
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 02:26 PM .
Advertisement
Log in to turn off these ads.