...

View Full Version : PHP Function - Image Selection



ssskaya
04-02-2004, 03:42 AM
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!

sad69
04-02-2004, 07:40 AM
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.

ssskaya
04-02-2004, 10:52 AM
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!

dswimboy
04-02-2004, 04:55 PM
assuming the varible $quote->change is a number, i'd just use greater than and less than comparisons.


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
04-02-2004, 04:58 PM
if it isn't a number, i suppose you could use RegEx much the same way you used the .indexOf method

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.

ssskaya
04-02-2004, 06:47 PM
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; ?>

dswimboy
04-02-2004, 06:53 PM
maybe modify the previous script like this:

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.

ssskaya
04-02-2004, 07:07 PM
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).

dswimboy
04-02-2004, 07:15 PM
okay then how about this:

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;

ssskaya
04-02-2004, 07:15 PM
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>";

ssskaya
04-02-2004, 07:23 PM
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.

DesignersToolz
04-02-2004, 07:38 PM
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.

dswimboy
04-02-2004, 07:39 PM
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>";

DesignersToolz
04-02-2004, 07:45 PM
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;




<?php
$string = "24764.47464";
$pos = strpos($string, ".");
$allowedlen = $pos+3;
$value = substr($string,0, $allowedlen);
print $value;
?>

ssskaya
04-02-2004, 07:52 PM
dswimboy:

how do you frame the scripts like you just did?

ssskaya
04-02-2004, 07:53 PM
DesignersToolz:

How about the thousand comma separator?

DesignersToolz
04-02-2004, 10:41 PM
how do you frame the scripts like you just did?

you put [ code ] [/ code ] around your code without the spaces.



How about the thousand comma separator?


Err, I'm sorry to say but I have to go for now. If noones has replied by the time I get back i'll post code for you. :(

DesignersToolz
04-03-2004, 02:36 AM
Hey you know what I don't really know of a good way to do it. I could write you a code but it would be needlessly too long and I'm sure someone else here can point you in a beter direction sorry :(

ssskaya
04-03-2004, 05:49 PM
I found thhe sciprt below. Works fine. I still have a problem tough.

My variable has a + sign in front of it, and this script eliminates it.. still this script will be useful for most people.



$digitformatted = sprintf("%01.2f", $variable);
Can someone post something that combines this with a thousand separator?

DesignersToolz
04-03-2004, 06:03 PM
Do you know how many commas you will need? Just one? Two? Or will it vary..

dswimboy
04-03-2004, 07:19 PM
on the toolbar of the editor, there is the "Framer" for Code, symobolized by a pound sign (#). just to the right of it, is a sheet of paper that says PHP. you should only use it for PHP.

DesignersToolz
04-03-2004, 08:23 PM
You still with us?

Nightfire
04-03-2004, 08:25 PM
I don't understand why this thread's continuing. Dswimboy gave the answer a while back.


$quote->change = number_format($quote->change, 2);

Will add commas and decimal points in the correct places.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum