Author |
Message |
Donovan
Client

Joined: Oct 07, 2003
Posts: 735
Location: Ohio
|
Posted:
Fri Jan 05, 2007 9:30 am |
|
I have a function called checkRPlevel
and I want it to check several things based on the following conditions.
(RP is short for Resource Points.)
Unit with 5RP considered "Fully operational"
Unit with 3-4RP considered "Partially operational" - plays 1 man down
Unit with 1-2RP considered "Unoperational" - cannot move on map
Unit with 0RP must respawn at HQ.
So far I have the following.
Code:function checkRPlevel($r_points, $team_id, $rp_id){
include_once("header.php");
global $db, $prefix;
//Total RP in lookup table.
$result = $db->sql_query ("SELECT SUM(r_points) as resourcepoints FROM ".$prefix . "_eto_rpoints_lkup WHERE team_id ='$team_id' AND rp_id = '4' OR rp_id = '5'");
$info = $db->sql_fetchrow($result);
$resourcepoints = $info[resourcepoints];
if ($resourcepoints <= 2) {
OpenTable();
echo"You do not have enough Resource Points for this action."
echo"Movement on the Battlefield requires a RP level greater than 2."
CloseTable();
include("footer.php");
exit();
}
}
|
rp_id of 5 is when points were added to the team_id
rp_id of 4 is when points were deducted from team_id
That is why I SUM both to find the balance of points.
This is what I have in mind but am unsure if my syntax is correct.
Code:switch($resourcepoints)
{
case ($resourcepoints == 0):
echo "Your unit has Zero Resource Points and must respawn at HQ";
break;
case ($resourcepoints <= 2 && $resourcepoints != 0):
echo "This action is denied."
echo "You do not have enough Resource Points for this action."
echo "Movement on the Battlefield requires a RP level greater than 2."
break;
case ($resourcepoints <= 4 && $resourcepoints >= 3):
echo "Action is Accepted."
echo "Your Unit is partially operational with a balance of 3-4 Resource Points."
echo "You must play the next match 1 man down."
break;
default:
echo "Action is Accepted."
echo "Unit is considered Fully Operational."
break;
}
|
|
|
|
 |
 |
Donovan

|
Posted:
Fri Jan 05, 2007 9:45 am |
|
How much stuff can I fit inside each condition? Would each one need a
include_once("header.php");
OpenTable();
Closetabel();
And what happens with the default condition? Does it give the
echo "Action is Accepted."
echo "Unit is considered Fully Operational."
...and then fall out back to the place that called this function? |
|
|
|
 |
Gremmie
Former Moderator in Good Standing

Joined: Apr 06, 2006
Posts: 2415
Location: Iowa, USA
|
Posted:
Fri Jan 05, 2007 9:49 am |
|
|
|
 |
Donovan

|
Posted:
Fri Jan 05, 2007 9:57 am |
|
|
|
 |
Donovan

|
Posted:
Fri Jan 05, 2007 10:02 am |
|
So I guess it's back to this?
Code:if ($resourcepoints == 0) {
OpenTable();
echo "This action is denied."
echo "Your unit has Zero Resource Points and must respawn at HQ";
CloseTable();
} elseif ($resourcepoints <= 2 && $resourcepoints != 0) {
OpenTable();
echo "This action is denied."
echo "You do not have enough Resource Points for this action."
echo "Movement on the Battlefield requires a RP level greater than 2."
echo "Territory has not changed."
CloseTable();
} elseif ($resourcepoints <= 4 && $resourcepoints >= 3) {
OpenTable();
echo "Action is Accepted."
echo "Your Unit is partially operational with a balance of 3-4 Resource Points."
echo "Any pending attack means you must play the next match 1 man down."
CloseTable();
}else{
OpenTable();
echo "Action is Accepted."
echo "Unit is considered Fully Operational."
CloseTable();
}
|
|
|
|
|
 |
Gremmie

|
Posted:
Fri Jan 05, 2007 10:36 am |
|
I guess. I would move the open and close tables outside the if-else-if's since it is repeated in every case.
OpenTable();
if-else-if's
CloseTable(); |
|
|
|
 |
Donovan

|
Posted:
Fri Jan 12, 2007 6:10 pm |
|
Well I think I'm doing this wrong. I need to to return back to the code if they have enough Resource Points after the function check.
Not working as needed.
In my Move form I have the following:
Code:$rplevel = checkRPlevel($r_points, $team_id, $rp_id); //Check to see if they have enough RP
if ($rplevel = FALSE) {
exit();
}
|
Checking the level of their points.
And then the function.
Code:function checkRPlevel($r_points, $team_id, $rp_id){
include_once("header.php");
global $db, $prefix;
//Total RP in lookup table.
$result = $db->sql_query ("SELECT SUM(r_points) as resourcepoints FROM ".$prefix . "_eto_rpoints_lkup WHERE team_id ='$team_id' AND rp_id = '4' OR rp_id = '5'");
$info = $db->sql_fetchrow($result);
$resourcepoints = $info[resourcepoints];
if ($resourcepoints == 0) {
OpenTable();
echo "<center>";
echo "This action is denied.</br>";
echo "Your Unit has $resourcepoints Resource Points.</br>";
echo "Contact your division SAC and get resupplied with Resource Points";
echo "</center>";
CloseTable();
$rplevel = FALSE;
include("footer.php");
exit();
}elseif ($resourcepoints <= 2 && $resourcepoints != 0) {
OpenTable();
echo "<center>";
echo "This action is denied.</br>";
echo "You do not have enough Resource Points for this action.</br>";
echo "Movement on the Battlefield requires a RP level greater than 2.</br>";
echo "Your Unit has $resourcepoints Resource Points.</br>";
echo "Territory has not changed.";
echo "</center>";
CloseTable();
$rplevel = FALSE;
include("footer.php");
exit();
}elseif ($resourcepoints <= 4 && $resourcepoints >= 3) {
OpenTable();
echo "<center>";
echo "Action is Allowed.</br>";
echo "Your Unit is partially operational with a balance below 5.</br>";
echo "Your Unit has $resourcepoints Resource Points.</br>";
echo "Any pending attack means you must play the next match 1 man down.";
echo "</center>";
CloseTable();
$rplevel = TRUE;
}else{
OpenTable();
echo "<center>";
echo "Action is Allowed.</br>";
echo "Unit is considered Fully Operational.</br>";
echo "Your Unit has $resourcepoints Resource Points.</br>";
echo "</center>";
CloseTable();
$rplevel = TRUE;
}
return $rplevel;
}
|
I'm thinking I need to return back from where I called the function.
Hence is why I have $rplevel = checkRPlevel
Then check the value of $rplevel. |
|
|
|
 |
Gremmie

|
Posted:
Sat Jan 13, 2007 12:42 pm |
|
Donovan wrote: |
In my Move form I have the following:
Code:$rplevel = checkRPlevel($r_points, $team_id, $rp_id); //Check to see if they have enough RP
if ($rplevel = FALSE) {
exit();
}
|
|
Well I didn't follow all that, but I noticed right away you made the classic assignment error mistake in an if-statement. Change that to:
Code:
if ($rplevel == false)
|
or even
Code:
if ($rplevel === false)
|
if your function can return 0. |
|
|
|
 |
Donovan

|
Posted:
Sat Jan 13, 2007 2:24 pm |
|
oops.
Thanks.
Hey how could I turn a variable into a negative number.
I have been reading about concatination.
For instance:
Code:$result4 = $db->sql_query ("SELECT SUM(r_points) as resourcepoints FROM ".$prefix . "_eto_rpoints_lkup WHERE team_id ='$team_id' AND rp_id = '4' OR rp_id = '5'");
$info4 = $db->sql_fetchrow($result4);
$resourcepoints = $info4[resourcepoints];
|
I need $resourcepoints to become negative. |
|
|
|
 |
Gremmie

|
Posted:
Sat Jan 13, 2007 3:26 pm |
|
Use the unary negation operator:
Code:
$resourcepoints = -$info4['resourcepoints'];
|
|
|
|
|
 |
Donovan

|
Posted:
Sun Jan 14, 2007 10:17 pm |
|
d***
This used to work.
Code:if ($loserscores = 6) {
$r_points = -1;
}elseif (($loserscores = 4) && ($loserscores = 5)){
$r_points = -2;
}elseif (($loserscores >= 1) && ($loserscores <= 3)){
$r_points = -3;
}else{
$result = $db->sql_query ("SELECT SUM(r_points) as resourcepoints FROM ".$prefix . "_eto_rpoints_lkup WHERE team_id ='$team_id' AND rp_id = '4' OR rp_id = '5'");
$info = $db->sql_fetchrow($result);
$resourcepoints = -$info['resourcepoints'];
$r_points = $resourcepoints;
}
|
Until I added the last bit of code. The problem is the match result was the loser got a score of 0 so the last }else{ should have come into effect.
It didn't. This was what was input into the table.
$r_points = -3;
I know the query works as I ran it in phpMyadmin. It SUM's the values correctly. |
|
|
|
 |
evaders99
Former Moderator in Good Standing

Joined: Apr 30, 2004
Posts: 3221
|
Posted:
Mon Jan 15, 2007 3:11 am |
|
For testing equality, you need to use ==
i.e.
|
_________________ - Only registered users can see links on this board! Get registered or login! -
Need help? Only registered users can see links on this board! Get registered or login! |
|
|
 |
Donovan

|
Posted:
Mon Jan 15, 2007 11:44 am |
|
Thanks.
I always do that
Here is what it looks like now but it is still assigning a -3 to a loser score of 0
Code:function reconcileMatch($game_id, $team_id) {
include_once("header.php");
campaign_admin_menu(_CAMPAIGN_ADMIN);
global $module_name, $db, $prefix;
if ($loserscores == 6) {
$r_points = -1;
}elseif (($loserscores >= 4) && ($loserscores <= 5)){
$r_points = -2;
}elseif (($loserscores >= 1) && ($loserscores <= 3)){
$r_points = -3;
}else{
$result = $db->sql_query ("SELECT SUM(r_points) as resourcepoints FROM ".$prefix . "_eto_rpoints_lkup WHERE team_id ='$team_id' AND rp_id = '4' OR rp_id = '5'");
$info = $db->sql_fetchrow($result);
$resourcepoints = -$info['resourcepoints'];
$r_points = $resourcepoints;
}
//Update Resource Points in lookup table for loss.
$rp_id = '5';
$rp_details = 'Suffered Loss in Combat';
$sql = "INSERT INTO " . $prefix . "_eto_rpoints_lkup (pid, rp_id, rpoint_dt, team_id, r_points, rp_details)".
"VALUES ('NULL', '$rp_id', CURRENT_TIMESTAMP(), '$team_id', '$r_points', '$rp_details')";
|
|
|
|
|
 |
Gremmie

|
Posted:
Mon Jan 15, 2007 11:55 am |
|
Not sure if this is related to your problem, but you probably want to write your WHERE clause on your query like this:
Code:
WHERE team_id ='$team_id' AND (rp_id = '4' OR rp_id = '5')
|
You had it without parens around the OR clause. That will select more rows than what you want I think. You want WHERE team_id = $team_id AND (rp_id is either 4 or 5), right? |
|
|
|
 |
Donovan

|
Posted:
Mon Jan 15, 2007 12:05 pm |
|
A rp_id of 4 means they have been reenforced (points were added), and an rp_id of 5 is when points were deducted ( they lost in a match)
So I want to capture both in the query to total their resource points. They may not have an rp_id that equals 5 as they may have not yet lost a match so that is why I have the OR. |
|
|
|
 |
Gremmie

|
Posted:
Mon Jan 15, 2007 8:35 pm |
|
But in any case you want all teams that match $team_id, right? If so you'll need the parens as I have indicated. If not, disregard. |
|
|
|
 |
Gremmie

|
Posted:
Mon Jan 15, 2007 8:38 pm |
|
What I am trying to say is....the way you have it now, because AND is a higher precedence than OR you have essentially written this:
WHERE (team_id ='$team_id' AND rp_id = '4') OR rp_id = '5'
that will match the team with team_id = $team_id AND rp_id = 4
OR
any team (!!!!) that has rp_id = 5
Is that really what you want? That's what I was trying to say.
Hope that helps. |
|
|
|
 |
gregexp
The Mouse Is Extension Of Arm

Joined: Feb 21, 2006
Posts: 1497
Location: In front of a screen....HELP! lol
|
Posted:
Tue Jan 16, 2007 7:45 pm |
|
After looking at your code and making php run through 0-6 I found that the if statement of $loserscores = 6 was infact causing php to redeclare the variable.
So take evaders advice. Secondly,
}elseif (($loserscores == 4) && ($loserscores == 5)){
should be
}elseif (($loserscores == 4) OR ($loserscores == 5)){
4 can never also be 5 and vise versa, So that was simple enough.
So now heres your code redone.:
if ($loserscores == 6) {
$r_points = -1;
}elseif (($loserscores == 4) OR ($loserscores == 5)){
$r_points = -2;
}elseif (($loserscores >= 1) && ($loserscores <= 3)){
$r_points = -3;
}else{
$r_points = -15;//set this to not use the database I dont have lol. you need to change it.
}
Thats the working code, to test codes like this, use this syntax.
//Hardcode it all, just for testing. start with zero and work your way upto the max you want, 7 in this case, then add one.(loop until its complete.
for($loserscores=0;$loserscores<7;$loserscores++){
//echo $loserscores;
if ($loserscores == 6) {
$r_points = -1;
}elseif (($loserscores == 4) OR ($loserscores == 5)){//if possible always use == in order to check aqurancy and prevent bad things from happening.
$r_points = -2;
}elseif (($loserscores >= 1) && ($loserscores <= 3)){
$r_points = -3;
}else{
$r_points = -15;
}
echo $loserscores.'++'.$r_points.'<BR>';// echo the output to verify it.
}
This will output:
0++-15//This is the else statement, remember I hardcoded the output.
1++-3
2++-3
3++-3
4++-2
5++-2
6++-1
Now all this is correct. Have fun with it. |
_________________ For those who stand shall NEVER fall and those who fall shall RISE once more!! |
|
 |
 |
gregexp

|
Posted:
Wed Jan 17, 2007 5:26 pm |
|
I run php5 on my home server, perhaps that is how the redeclaration occured but non the less, this code is properly working. |
|
|
|
 |
Donovan

|
Posted:
Wed Jan 17, 2007 5:39 pm |
|
|
|
 |
|