RE: [PHP] Seems Simple enough

2004-01-27 Thread Ford, Mike [LSS]
On 26 January 2004 16:56, Christopher J. Crane wrote:

 Ok here is the wierd thing.
 I pasted more code, it seems to not work because of me
 changing the number
 format.
 
 This works ...
  if($Balance = 10001) {
$Balance = number_format($Balance,2,'.',',');
echo font color=\green\\$$Balance/fontbr\n; } 
if($Balance = ) { $Balance =
number_format($Balance,2,'.',','); echo font
color=\red\\$$Balance/fontbr\n; }  else { $Balance =
number_format($Balance,2,'.',','); echo font
 color=\purple\\$$Balance/fontbr\n; } 
 
 This does not ...
   $SummaryResults = mysql_query(SELECT * FROM Accounting WHERE
 UserID='$UserID' LIMIT 1) or die(Invalid query);
   while($SummaryField = mysql_fetch_array($SummaryResults)) {
 $Balance = number_format($SummaryField[Balance],2,'.',',');

$Balnace is now (for example) 10,000.00; comma is not a valid character in a PHP 
number, so when interpreted as a number this will convert to 10.

  } if($Balance = 10001) { echo font
 color=\green\\$$Balance/fontbr\n; }
  elseif($Balance = ) { echo font

10 is =  so...

 color=\red\\$$Balance/fontbr\n; }
  else { echo font color=\purple\\$$Balance/fontbr\n; }

But I have to ask, why include all the invariant parts in every branch of your if-else 
chain?  As a general principle, I try to put only the parts that are genuinely 
different inside conditionals, so in this case I'd probably write something like:

   echo 'font color=';
   if ($Balance = 10001):
  echo 'green';
   elseif ($Balance = ):
  echo 'red';
   else:
  echo 'purple';
   endif;
   echo '$', number_format($Balance, 2, '.', ','), /fontbr /\n;

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning  Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211 
 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Seems Simple enough

2004-01-26 Thread Stuart
Christopher J. Crane wrote:
 if($Balance = 10001) { echo font
color=\green\\$$Balance/fontbr\n; }
 elseif($Balance = ) { echo font
color=\red\\$$Balance/fontbr\n; }
 else { echo font color=\purple\\$$Balance/fontbr\n; }
Works fine here. The elseif condition will be true if $Balance is 
undefined. Are you sure that variable exists and is actually set to 1?

--
Stuart
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


RE: [PHP] Seems Simple enough

2004-01-26 Thread Larry Brown
you need one of the to if's to be either $Balance =1 elseif($Balance 
1000) or $Balance  1 elseif($Balance = 1).  Yours has the first
saying everything equal to or greater than 10001 which 1 is not else
everything less than or equal to  which 1 is not. =)

Larry

-Original Message-
From: Christopher J. Crane [mailto:[EMAIL PROTECTED]
Sent: Monday, January 26, 2004 11:29 AM
To: [EMAIL PROTECTED]
Subject: [PHP] Seems Simple enough


I have these lines of code, that I thought was simple enough, but it doesn't
work how I thought.
The Variable $Balance is set to 1 in the database, and I thought it
would be outputed as purple or the be found true of the else part of the
code. It comes out as red or the elseif part of the code.

 if($Balance = 10001) { echo font
color=\green\\$$Balance/fontbr\n; }
 elseif($Balance = ) { echo font
color=\red\\$$Balance/fontbr\n; }
 else { echo font color=\purple\\$$Balance/fontbr\n; }

I originally had it as follows, but that didn't work either.

 if($Balance  1) { echo font
color=\green\\$$Balance/fontbr\n; }
 elseif($Balance  1) { echo font
color=\red\\$$Balance/fontbr\n; }
 else { echo font color=\purple\\$$Balance/fontbr\n; }

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Seems Simple enough

2004-01-26 Thread Christopher J. Crane
Ok here is the wierd thing.
I pasted more code, it seems to not work because of me changing the number
format.

This works ...
 if($Balance = 10001) {
   $Balance = number_format($Balance,2,'.',',');
   echo font color=\green\\$$Balance/fontbr\n; }
 if($Balance = ) {
   $Balance = number_format($Balance,2,'.',',');
   echo font color=\red\\$$Balance/fontbr\n; }
 else {
   $Balance = number_format($Balance,2,'.',',');
   echo font color=\purple\\$$Balance/fontbr\n; }

This does not ...
  $SummaryResults = mysql_query(SELECT * FROM Accounting WHERE
UserID='$UserID' LIMIT 1) or die(Invalid query);
  while($SummaryField = mysql_fetch_array($SummaryResults)) {
$Balance = number_format($SummaryField[Balance],2,'.',',');
}
 if($Balance = 10001) { echo font
color=\green\\$$Balance/fontbr\n; }
 elseif($Balance = ) { echo font
color=\red\\$$Balance/fontbr\n; }
 else { echo font color=\purple\\$$Balance/fontbr\n; }

Stuart [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 Christopher J. Crane wrote:
   if($Balance = 10001) { echo font
  color=\green\\$$Balance/fontbr\n; }
   elseif($Balance = ) { echo font
  color=\red\\$$Balance/fontbr\n; }
   else { echo font color=\purple\\$$Balance/fontbr\n; }

 Works fine here. The elseif condition will be true if $Balance is
 undefined. Are you sure that variable exists and is actually set to 1?

 -- 
 Stuart

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Seems Simple enough

2004-01-26 Thread Stuart
Christopher J. Crane wrote:
This does not ...
  $SummaryResults = mysql_query(SELECT * FROM Accounting WHERE
UserID='$UserID' LIMIT 1) or die(Invalid query);
  while($SummaryField = mysql_fetch_array($SummaryResults)) {
$Balance = number_format($SummaryField[Balance],2,'.',',');
}
Display $Balance here. Just do a print $Balance. Something is wrong 
there, not below.

While we're at it, why are you looping through all of the rows setting 
$Balance each time? If it's only going to return one row, get that one 
row and use it!!

 if($Balance = 10001) { echo font
color=\green\\$$Balance/fontbr\n; }
 elseif($Balance = ) { echo font
color=\red\\$$Balance/fontbr\n; }
 else { echo font color=\purple\\$$Balance/fontbr\n; }
--
Stuart
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Seems Simple enough

2004-01-26 Thread Christopher J. Crane
Good Question on the looking though all rows. I never wrote code looking at
just that one code and getting the variable from the column. That is why I
limited the query to one LIMIT 1. I guess it is just me pasting code from
my other applications and not checking it out.

To the original problem, it all hinges on me changing the format of
$Balance. If I remove the line   $Balance =
number_format($SummaryField[Balance],2,'.',','); 
and replace it with  $Balance = $SummaryField[Balance];  it works fine.

Stuart [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 Christopher J. Crane wrote:
  This does not ...
$SummaryResults = mysql_query(SELECT * FROM Accounting WHERE
  UserID='$UserID' LIMIT 1) or die(Invalid query);
while($SummaryField = mysql_fetch_array($SummaryResults)) {
  $Balance = number_format($SummaryField[Balance],2,'.',',');
  }

 Display $Balance here. Just do a print $Balance. Something is wrong
 there, not below.

 While we're at it, why are you looping through all of the rows setting
 $Balance each time? If it's only going to return one row, get that one
 row and use it!!

   if($Balance = 10001) { echo font
  color=\green\\$$Balance/fontbr\n; }
   elseif($Balance = ) { echo font
  color=\red\\$$Balance/fontbr\n; }
   else { echo font color=\purple\\$$Balance/fontbr\n; }

 -- 
 Stuart

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Seems Simple enough

2004-01-26 Thread Stuart
Christopher J. Crane wrote:
To the original problem, it all hinges on me changing the format of
$Balance. If I remove the line   $Balance =
number_format($SummaryField[Balance],2,'.',','); 
and replace it with  $Balance = $SummaryField[Balance];  it works fine.
Well, duh! If you add periods and commas it will make it a text variable 
and will no longer equate to 1. You need to format it after the 
comparisons.

--
Stuart
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Seems Simple enough

2004-01-26 Thread Christopher J. Crane
Ok the problem seems to me, the format. I think that once I have the format
changed to include a comma seperation for thousands. I think at that point,
it is no longer a true number, so PHP deals with it differently.
Christopher J. Crane [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 Good Question on the looking though all rows. I never wrote code looking
at
 just that one code and getting the variable from the column. That is why I
 limited the query to one LIMIT 1. I guess it is just me pasting code
from
 my other applications and not checking it out.

 To the original problem, it all hinges on me changing the format of
 $Balance. If I remove the line   $Balance =
 number_format($SummaryField[Balance],2,'.',','); 
 and replace it with  $Balance = $SummaryField[Balance];  it works
fine.

 Stuart [EMAIL PROTECTED] wrote in message
 news:[EMAIL PROTECTED]
  Christopher J. Crane wrote:
   This does not ...
 $SummaryResults = mysql_query(SELECT * FROM Accounting WHERE
   UserID='$UserID' LIMIT 1) or die(Invalid query);
 while($SummaryField = mysql_fetch_array($SummaryResults)) {
   $Balance = number_format($SummaryField[Balance],2,'.',',');
   }
 
  Display $Balance here. Just do a print $Balance. Something is wrong
  there, not below.
 
  While we're at it, why are you looping through all of the rows setting
  $Balance each time? If it's only going to return one row, get that one
  row and use it!!
 
if($Balance = 10001) { echo font
   color=\green\\$$Balance/fontbr\n; }
elseif($Balance = ) { echo font
   color=\red\\$$Balance/fontbr\n; }
else { echo font color=\purple\\$$Balance/fontbr\n; }
 
  -- 
  Stuart

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php