0

Hi... Again :)

Just after i fixed my previous problem thanks to me failing to put an echo before ".."
I worked out that the incorrect image was showing. I'm making a stock trading website, and there will be an red arrow pointing downwards when it detects at - (negative sign). If it doesn't detect it, then it displays the green up arrow.

Problem is, it is showing the green arrow for negative values, and the green arrow for positive values....

Here's the code :

<?php if($_SESSION['loggedin'] === "true") { ?>
<?php include("includes/getstock.php"); ?>

<br/>

<br />
<?php $stockdata = GetStockInfo("GOOG"); ?>

Stock Name : <?php echo $stockdata['name']; ?><br/>
Stock Price : <?php echo $stockdata['price']; ?><br/>
Stock Difference : 
<?php if(strpos($stockdata['diff'], "-")) { echo "<img src=\"images/down.png\" />"; } else { echo "<img src=\"images/up.png\" /> "; } ?>
<?php echo $stockdata['diff']; ?><br/><br/>
<?php $stockdata = GetStockInfo("AAPL"); ?>
Stock Name : <?php echo $stockdata['name']; ?><br/>
Stock Price : <?php echo $stockdata['price']; ?><br/>
Stock Difference : 
<?php if(strpos($stockdata['diff'], "-")) { echo "<img src=\"images/down.png\" />"; } else { echo "<img src=\"images/up.png\" /> "; } ?>
<?php echo $stockdata['diff']; ?>

<?php } ?>

I'm "trying" to use strpos to find the negative sign, but somehow it isn't working out.

Any help would be greatly appreciated ;)

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by cuonic
0

I'd do it slightly differently.

Get the difference and see if it is < 0. If so, it's negative. I'd wouldn't use image tags either, just add a class to the 'Stock Difference :' tag (which you don't seem to have). This class could then be referenced in a css file. You should enclose your labels within some sort of tags, e.g. li or p.

0

Why dont You use this to check if "-" exist in the string

if(substr_count($stockdata['diff'],'-')>0){
//while anything you want
}
0
$class = ($stockdata['diff'] < 0) ? 'neg' : 'pos';

....

<p class="<?php echo $class;?>">Stock Difference : <?php echo $stockdata['diff']; ?></p>

Then apply some CSS to .pos and .neg, e.g. a background image and coloured text.

Edited by diafol: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.