Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-02-2004, 02:42 AM   PM User | #1
ssskaya
Regular Coder

 
Join Date: Oct 2002
Location: USA
Posts: 255
Thanks: 1
Thanked 0 Times in 0 Posts
ssskaya is an unknown quantity at this point
Question 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!
ssskaya is offline   Reply With Quote
Old 04-02-2004, 06:40 AM   PM User | #2
sad69
Senior Coder

 
Join Date: Feb 2004
Posts: 1,206
Thanks: 0
Thanked 0 Times in 0 Posts
sad69 is an unknown quantity at this point
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.
sad69 is offline   Reply With Quote
Old 04-02-2004, 09:52 AM   PM User | #3
ssskaya
Regular Coder

 
Join Date: Oct 2002
Location: USA
Posts: 255
Thanks: 1
Thanked 0 Times in 0 Posts
ssskaya is an unknown quantity at this point
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!
ssskaya is offline   Reply With Quote
Old 04-02-2004, 03:55 PM   PM User | #4
dswimboy
Regular Coder

 
dswimboy's Avatar
 
Join Date: Nov 2003
Location: mostly in Ann Arbor
Posts: 458
Thanks: 0
Thanked 0 Times in 0 Posts
dswimboy is an unknown quantity at this point
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>';

dswimboy is offline   Reply With Quote
Old 04-02-2004, 03:58 PM   PM User | #5
dswimboy
Regular Coder

 
dswimboy's Avatar
 
Join Date: Nov 2003
Location: mostly in Ann Arbor
Posts: 458
Thanks: 0
Thanked 0 Times in 0 Posts
dswimboy is an unknown quantity at this point
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.
dswimboy is offline   Reply With Quote
Old 04-02-2004, 05:47 PM   PM User | #6
ssskaya
Regular Coder

 
Join Date: Oct 2002
Location: USA
Posts: 255
Thanks: 1
Thanked 0 Times in 0 Posts
ssskaya is an unknown quantity at this point
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; ?>
ssskaya is offline   Reply With Quote
Old 04-02-2004, 05:53 PM   PM User | #7
dswimboy
Regular Coder

 
dswimboy's Avatar
 
Join Date: Nov 2003
Location: mostly in Ann Arbor
Posts: 458
Thanks: 0
Thanked 0 Times in 0 Posts
dswimboy is an unknown quantity at this point
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&nbsp;$img</FONT>"
i realize font is deprecated, i'm assuming you can handle it anyway you please...style sheets included.
dswimboy is offline   Reply With Quote
Old 04-02-2004, 06:07 PM   PM User | #8
ssskaya
Regular Coder

 
Join Date: Oct 2002
Location: USA
Posts: 255
Thanks: 1
Thanked 0 Times in 0 Posts
ssskaya is an unknown quantity at this point
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).
ssskaya is offline   Reply With Quote
Old 04-02-2004, 06:15 PM   PM User | #9
dswimboy
Regular Coder

 
dswimboy's Avatar
 
Join Date: Nov 2003
Location: mostly in Ann Arbor
Posts: 458
Thanks: 0
Thanked 0 Times in 0 Posts
dswimboy is an unknown quantity at this point
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;
dswimboy is offline   Reply With Quote
Old 04-02-2004, 06:15 PM   PM User | #10
ssskaya
Regular Coder

 
Join Date: Oct 2002
Location: USA
Posts: 255
Thanks: 1
Thanked 0 Times in 0 Posts
ssskaya is an unknown quantity at this point
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..
ssskaya is offline   Reply With Quote
Old 04-02-2004, 06:23 PM   PM User | #11
ssskaya
Regular Coder

 
Join Date: Oct 2002
Location: USA
Posts: 255
Thanks: 1
Thanked 0 Times in 0 Posts
ssskaya is an unknown quantity at this point
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.
ssskaya is offline   Reply With Quote
Old 04-02-2004, 06:38 PM   PM User | #12
DesignersToolz
Regular Coder

 
Join Date: Mar 2004
Location: Jackson, Georgia
Posts: 102
Thanks: 0
Thanked 0 Times in 0 Posts
DesignersToolz is an unknown quantity at this point
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.
__________________
Designer's Toolz; Web & software development community.

Get your coding questions answered:
DTZ Forums
DesignersToolz is offline   Reply With Quote
Old 04-02-2004, 06:39 PM   PM User | #13
dswimboy
Regular Coder

 
dswimboy's Avatar
 
Join Date: Nov 2003
Location: mostly in Ann Arbor
Posts: 458
Thanks: 0
Thanked 0 Times in 0 Posts
dswimboy is an unknown quantity at this point
PHP Code:
if ($quote->change 0) { 
$color="#008000"
} elseif (
$quote->change 0) { 
$color="#FF0000"
} else { 
$color="#000000"
}
$quote->change number_format($quote->change2);
echo 
"<font color='$color'>$quote->change</font>"
dswimboy is offline   Reply With Quote
Old 04-02-2004, 06:45 PM   PM User | #14
DesignersToolz
Regular Coder

 
Join Date: Mar 2004
Location: Jackson, Georgia
Posts: 102
Thanks: 0
Thanked 0 Times in 0 Posts
DesignersToolz is an unknown quantity at this point
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;
?>
__________________
Designer's Toolz; Web & software development community.

Get your coding questions answered:
DTZ Forums
DesignersToolz is offline   Reply With Quote
Old 04-02-2004, 06:52 PM   PM User | #15
ssskaya
Regular Coder

 
Join Date: Oct 2002
Location: USA
Posts: 255
Thanks: 1
Thanked 0 Times in 0 Posts
ssskaya is an unknown quantity at this point
dswimboy:

how do you frame the scripts like you just did?
ssskaya is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:26 PM.


Advertisement
Log in to turn off these ads.