Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 01-04-2011, 08:18 AM If Statement Ignored
Chris_123K's Avatar
Super Talker

Posts: 128
Name: Chris Kummelstedt
Trades: 0
Thanks for reading this

I have this function that based on the local currency of the user ($currency_to) and currency the data is being quoted in, currently fixed as CNY ($currency_from), uses the yahoo finance currency calculator to take a number ($figure) and spit back either:

a) The value in the currency it is being displayed in
Code:
return round($exchange_rate*$figure, -1) . ' ' . $currency_to;
or

b) if the value is quoted as 0 or nothing (null) it supposed to notify the user that its not applicable
Code:
 return "N/A";
The problem is that it does not care if the value is 0 or null, it just gives the value in the (a) format i.e. 0 USD

Here is the function:

Code:
<?php

function display_price ($currency_from, $currency_to,$figure ) {
$url = "http://quote.yahoo.com/d/quotes.csv?s="
. $currency_from . $currency_to . "=X"
. "&f=l1&e=.csv";
$open = fopen($url, "r");
$exchange_rate = fread($open, 2000);
fclose($open);

if ($exchange_rate == "0.00")
   $exchange_rate = "1";
else
$error = "sorry, we could not determine your location and display the prices in your local area,";


if ($figure=="null")
  return "N/A";
else if ($figure=="0")
  return "N/A";
else
  return round($exchange_rate*$figure, -1) . ' ' . $currency_to;  

}
$currency_from="CNY";?>
Also including the bit where it is being spat back, though I am pretty sure the problem is with the function.

Code:
<?php
mysql_connect($server,$user,$password);
@mysql_select_db($database) or die("Sorry, unable to select database");
$result = mysql_query("SELECT * FROM drup_content_type_school ORDER BY field_schoolname_value");
$i=0;
 while($row = mysql_fetch_array($result))
  {
if ($i % 2)
   $class= "tr3";
else 
  $class= "tr2";
  echo "<tr class='" . $class . "'>
<td class='td1'><a href='http://compare-chineselanguageschools.com/content/output-school?school=" . $row[field_schoolname_value] . "'>" . $row['field_schoolname_value'] . "</a></td>
<td class='td2'>" . $row['field_est_value'] . "</td>
<td class='td3'>" . $row['field_max_class_size_value'] . "</td>
<td class='td1'>" . display_price($currency_from, $currency_to, $row[$price]) . "</td>
<td class='td1'>" . $row['field_max_class_size_value'] . "</td>
</tr>
";
$i++;
}

  ?>
Thank you so much for any input.
Chris_123K is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-04-2011, 09:25 AM Re: If Statement Ignored
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
a null value is never going to be == "null", you should use is_null().

If you need to evalute null OR 0, the expression would look like this:

PHP Code:
if(is_null($figure) || $figure === 0) {
    return 
'N/A';

A simple !$figure should also suffice, though I think my example is more readable.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 01-04-2011, 09:31 AM Re: If Statement Ignored
Chris_123K's Avatar
Super Talker

Posts: 128
Name: Chris Kummelstedt
Trades: 0
Hi wayfarer07,

you are star, helped me out twice now with the same site,

very very thankful
Chris_123K is offline
Reply With Quote
View Public Profile
 
Old 01-04-2011, 10:29 AM Re: If Statement Ignored
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
A better catch-all would be to use empty because it would evaluate true for NULL, false, 0 or empty string values (including spaces or new lines).

if (empty($figure)) return 'N/A';
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 01-04-2011, 11:23 AM Re: If Statement Ignored
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
true, empty() is very useful like that, and it includes the value of 0, or even an empty array() or an empty string "".
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 01-04-2011, 06:23 PM Re: If Statement Ignored
Chris_123K's Avatar
Super Talker

Posts: 128
Name: Chris Kummelstedt
Trades: 0
thanks for the help guys,

i ran into another little hick up further down the road and the empty() solved that.


its fantastic that there are people on the net that so readily share their knowledge, few other places in society where people help completely random strangers.
Chris_123K is offline
Reply With Quote
View Public Profile
 
Old 01-09-2011, 05:15 PM Re: If Statement Ignored
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Quote:
Originally Posted by wayfarer07 View Post
a null value is never going to be == "null"
If you want to stick with that, just make it $var==null, with no quotes.
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is online now
Reply With Quote
View Public Profile
 
Reply     « Reply to If Statement Ignored
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.84764 seconds with 12 queries