Member Avatar for cuonic

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

Recommended Answers

All 4 Replies

Member Avatar for diafol

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.

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

if(substr_count($stockdata['diff'],'-')>0){
//while anything you want
}
Member Avatar for diafol
$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.

Member Avatar for cuonic

Thanks ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.