Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP
Author Message
Donovan
Client



Joined: Oct 07, 2003
Posts: 735
Location: Ohio

PostPosted: Fri Jan 05, 2007 9:30 am Reply with quote

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;
}
 
View user's profile Send private message Visit poster's website ICQ Number
Donovan







PostPosted: Fri Jan 05, 2007 9:45 am Reply with quote

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

PostPosted: Fri Jan 05, 2007 9:49 am Reply with quote

You can't have expressions like that after the case keyword. They have to be constants.

http://us3.php.net/manual/en/control-structures.switch.php
 
View user's profile Send private message
Donovan







PostPosted: Fri Jan 05, 2007 9:57 am Reply with quote

Darnit!!
 
Donovan







PostPosted: Fri Jan 05, 2007 10:02 am Reply with quote

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







PostPosted: Fri Jan 05, 2007 10:36 am Reply with quote

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







PostPosted: Fri Jan 12, 2007 6:10 pm Reply with quote

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







PostPosted: Sat Jan 13, 2007 12:42 pm Reply with quote

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







PostPosted: Sat Jan 13, 2007 2:24 pm Reply with quote

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







PostPosted: Sat Jan 13, 2007 3:26 pm Reply with quote

Use the unary negation operator:

Code:


$resourcepoints = -$info4['resourcepoints'];
 
Donovan







PostPosted: Sun Jan 14, 2007 10:17 pm Reply with quote

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

PostPosted: Mon Jan 15, 2007 3:11 am Reply with quote

For testing equality, you need to use ==
i.e.
Code:


$loserscores == 6

_________________
- 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! 
View user's profile Send private message Visit poster's website
Donovan







PostPosted: Mon Jan 15, 2007 11:44 am Reply with quote

Thanks.

I always do that Mad

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







PostPosted: Mon Jan 15, 2007 11:55 am Reply with quote

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







PostPosted: Mon Jan 15, 2007 12:05 pm Reply with quote

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







PostPosted: Mon Jan 15, 2007 8:35 pm Reply with quote

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







PostPosted: Mon Jan 15, 2007 8:38 pm Reply with quote

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

PostPosted: Tue Jan 16, 2007 7:45 pm Reply with quote

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!! 
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number
gregexp







PostPosted: Wed Jan 17, 2007 5:26 pm Reply with quote

I run php5 on my home server, perhaps that is how the redeclaration occured but non the less, this code is properly working.
 
Donovan







PostPosted: Wed Jan 17, 2007 5:39 pm Reply with quote

Thanks
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©