Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading return value 1

Status
Not open for further replies.

Chomauk

Programmer
Jun 8, 2001
130
In my recordset I'm returning a value called GameDay which is equal to either 1, 2, 3, 4 or 5. When looping through the recordset, depending on the value of GameDay, I want to display a certain day when it changes. However, it appears when I'm reading the value it does not work.
I tried settype and I tried reading the case values inside single quotes and I tried comparing GameDay with $GameDay in the if statement. What am I doing wrong? Thanks
Code:
switch($ReturnRow1["GameDay"):
  case 1:
    $gameDay = "Thursday";
  case 2:
    $gameDay = "Friday";
  case 3;
    $gameDay = "Saturday";
  case 4:
     $gameDay = "Sunday";
  case 5:
    $gameDay = "Monday";
  default:
    $gameDay = "Does not work";
endswitch;

if($gameDay != $previousDay); {
echo $gameDay;
$previousDay = $gameDay;
}

"Failure is the tuition you pay for success."
~ Walter Brunell ~
 
first of all you're missing a square bracked right before ) on your first line.

then... you might want to use "1", "2"... for each case because you are comparing a string to an integer.
 
Thanks.

The square bracket exists in the original code...I accidently removed it while formating for my post...sorry.

GameDay is saved in the DB as an integer but I changed the numbers to "1", "2"...as you suggested with(unfortunately) the same results.

Any other suggestions would be greatly appreciated.

"Failure is the tuition you pay for success."
~ Walter Brunell ~
 
Does your variable $ReturnRow1 contain what you think it does before the switch. Put some debugging code in to see what you have:
Code:
echo '<pre>';print_r($ReturnRow1);echo "<pre>\n";

Is the switch code in a function. If so have you declared $ReturnRow1 to be global?

Ken
 
Switch


switch($ReturnRow1["GameDay"]):
case 1: //if $ReturnRow1["GameDay"] is equal to one the following code is executed
echo "its a 1";
break;
case 2:
echo "its a 2";
break;
case 3;
//etc..


default:
echo "$gameDay Does not work";
endswitch;



______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
I already put code in to display the actual value of GameDay and yes it is returned.

I put in as little code as possible to hopefully help solve the issue. Everything in the file works fine. Then I added the above code to the while loop and it reurns "Does not work" on every line. When I had it display the value of Gameday it displayed the correct value everytime.



"Failure is the tuition you pay for success."
~ Walter Brunell ~
 
P.S. It's not in a function....yet.

"Failure is the tuition you pay for success."
~ Walter Brunell ~
 
I would drop the switch statement completely and use
a) an array or
b) adjust the table structure

a: create an array that's global that contains the names of the days.
Code:
$wArray = array(1=>'Thursday',2=>'Friday',3=>'Saturday',4=>'Sunday',5=>'Monday');
#
# later you refer to the names as
$wArray[$ResultRow1['gameday']];

b:
Table should be structured to have the gameday be an ENUM column listing the full names of the days. Internally (in MySQL) the values are only stored once and the records contain references to the element, not the full text. However, any SELECT will return the full name to begin with. It makes the records also more human readable - who the he** knows what day "1" is. Thursday, eh?
 
Wait a sec:

In your switch are you doing:
Code:
...
case 1:
    $gameDay = "1";
  case 2:
    $gameDay = "2";
  ...
or are you doing
Code:
...
case 1:
    $gameDay = 1;
  case 2:
    $gameDay = 2;
  ...

because there's a huge difference...




[cheers]
Cheers!
Laura
 
I can't believe that I overlooked this.
The switch statements as shown here are syntactically wrong.
Code:
switch(expression){
# note: there is an opening curly brace, not a colon!
  case value1:
     whatever here
     break;
  case value2:
     whatever here
     break;
  default:
     whatever here
}

Your initial post has the colon, a missing square bracket and no break statements. The colon was carried forth by follow up posts. So where the missing break statements. If they are missing you'll always get the default.
 
oh my! I can't believe we ALL missed that!

Beer for you!

[cheers]
Cheers!
Laura
 
DRJ478:
If they are missing you'll always get the default

(I assume "they" refers to break statements.)

More exactly, if you are missing break statements at the ends of your case-sections inside a switch block, script execution inside that switch will start at the appropriate case and continue through other case-sections until the end of the switch. If your default case is at the end, it will be run.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
sleipnir214

That is exactly what I mean. I was kind'a getting tired of paraphrasing the manual page for switch. For anyone who wants to read it, here it is:

 
That fixed the switch statement...thanks much. The if statement, however, is seeing
Code:
if($gameDay != $previousDay); {
echo $gameDay;
$previousDay = $gameDay;
}
as always being true and I'm getting the same day repeatedly. Unless someone immediately sees what's wrong hopefully I'll figure it out. Maybe something like Trim if PHP has it...

thanks again all & DRJ here's a star...

"Failure is the tuition you pay for success."
~ Walter Brunell ~
 
Before the if and switch statement, issue a value to previousday that is null.

Therefore:
Code:
$previousDay = "";
switch statement...
if($gameDay != $previousDay); {
  echo $gameDay;
  $previousDay = $gameDay;
}

[cheers]
Cheers!
Laura
 
That fixed the switch statement...thanks much. The if statement, however, is seeing
Code:
if($gameDay != $previousDay); {
echo $gameDay;
$previousDay = $gameDay;
}
as always being true and I'm getting the same day repeatedly. Unless someone immediately sees what's wrong hopefully I'll figure it out. Maybe something like Trim if PHP has it...

thanks again all & DRJ here's a star...

"Failure is the tuition you pay for success."
~ Walter Brunell ~
 
I found the answer and it's case of putting something where it didn't belong instead of forgetting to put something where it belonged...I'm still learning...

Anyway I took out the semi-colon(;) at the end of the if and it works fine.

thanks again all.

"Failure is the tuition you pay for success."
~ Walter Brunell ~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top