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 Dec 08, 2006 2:19 pm Reply with quote

Here is the issue.

I have some code in a page that gathers my team_id and tid and then that calls a function to check if a team already exist on that tid. If a enemy team exist then I get forwarded to the attack page.

Like so.

Code:
checkmove($tid, $team_id); //Check to see if enemy exist on territory

      if ($enemy = TRUE) {
      Header("Location: modules.php?name=Campaign&file=Attack&team_id=$team_id");
      }else{ //No enemy occupies territory
      echo "<P><font color=RED size=6><center>Movement Orders Have Been Sent!</center></font></p>";
      echo "<P><font color=RED size=5><center>...Redirecting</center></font></p>";
      
      }


And here is the function that checks.

Code:
//Check if enemy exist on territory if so attack

function checkmove($tid, $team_id){
require_once("mainfile.php");
$module_name = basename(dirname(__FILE__));
include_once("modules/$module_name/language/lang-english.php");
global $db, $prefix, $module_name, $admin_file;
define('INDEX_FILE', true);
$index = 1;

 //Find the value of the division ID.
  $sql = "SELECT div_id FROM " . $prefix . "_tc_teams WHERE team_id ='$team_id'";
  $result = $db->sql_query($sql);
  $mydiv = $db->sql_fetchrow($result);
  $div_id = $mydiv['div_id'];

//Look for enemy teams that may be on territory
$sql = "SELECT * FROM " . $prefix . "_tc_teams WHERE tid ='$tid' AND div_id != '$div_id'";
$result = $db->sql_query($sql);
$enemy = FALSE;
   if ($db->sql_numrows($result) > 0) {
$enemy = TRUE; //Enemy exist on territory
}
 return $enemy;
}


The $enemy should return True or False.

The problem is that first I manually verify that no other team occupies that tid, then make my move. I want to ensure the code is functioning correctly.

The result is it goes to the attack page but no enemy team is displayed. There is no enemy team dispayed because no other team_id occupies that tid so instead it should have just let me move and said this

Movement Orders Have Been Sent!

Here is a screenie:

Image

I'm pretty sure my return code is working, but don't understand where I went wrong.

Evil or Very Mad grrrrr.

I hate functions, but the only way to learn how to use them is to try.
 
View user's profile Send private message Visit poster's website ICQ Number
evaders99
Former Moderator in Good Standing



Joined: Apr 30, 2004
Posts: 3221

PostPosted: Fri Dec 08, 2006 4:09 pm Reply with quote

It doesn't look like checkmove is actually returning the proper variable. It should be put the checkmove return value into the $enemy variable like so

Code:


      $enemy = checkmove($tid, $team_id); //Check to see if enemy exist on territory
      if ($enemy = TRUE) {

_________________
- Star Wars Rebellion Network -

Need help? Nuke Patched Core, Coding Services, Webmaster Services 
View user's profile Send private message Visit poster's website
Donovan







PostPosted: Fri Dec 08, 2006 9:01 pm Reply with quote

Didn't help. I'm still getting the same thing.
 
evaders99







PostPosted: Fri Dec 08, 2006 10:31 pm Reply with quote

Well I would guess its your SQL query then

Go ahead and echo out that SQL command and try it in phpMyAdmin, make sure you get what you think you should be getting

Code:


$sql = "SELECT * FROM " . $prefix . "_tc_teams WHERE tid ='$tid' AND div_id != '$div_id'";
echo $sql;
die();
 
Donovan







PostPosted: Sat Dec 09, 2006 8:54 am Reply with quote

The echo displayed the following.

SELECT * FROM nuke_tc_teams WHERE tid ='36' AND div_id != '1'
 
fkelly
Former Moderator in Good Standing



Joined: Aug 30, 2005
Posts: 3312
Location: near Albany NY

PostPosted: Sat Dec 09, 2006 9:07 am Reply with quote

My 2 cents, just make MYSQL error checking part of your programs from the beginning and you can save a lot of time and a lot of posts. Stuff your query into a variable ... say $sql and then do a if(!result)) mysql_query($sql) and if the result doesn't return successful then do an echo like:

Code:
 echo 'sql failed . <br />';

            echo 'query was ' . $sql . '<br />';
            echo mysql_errno() . '<br />';
            echo mysql_error() . '<br />';
            die();


It's awfully easy to make an error in a query and you won't have any idea unless you check and echo out the errno and error message and I find it helps to have the actual sql it was trying to execute right on the screen with it.
 
View user's profile Send private message Visit poster's website
Donovan







PostPosted: Sat Dec 09, 2006 4:48 pm Reply with quote

I've done that and added error checking.

I also put this in to see what happens on my move.php page after it calls the function.

Code:
$enemy = checkmove($tid, $team_id); //Check to see if enemy exist on territory 

echo $enemy;
die();


Nothing is displayed on move,php from the echo. $enemy contains nothing even tho in my function I am getting a result.

Here is what I'm trying to do in my move.php:

Code:
if ($op == "Move") {               

      $team_id = $_POST['team_id'];
      $tid = $_POST['tid'];      
      $action = 'Move';
      $dteam_id = 'N/A';      

      $sql = "UPDATE " . $prefix . "_tc_teams SET
      tid = '$tid'
      WHERE team_id = '$team_id'";

      $result = $db->sql_query($sql);   
      if (!$result) {
         echo("<p><font color=RED size=6><center>Error performing move. Data has not been sent!</center></font></p>");      
      }      
      

      // Post data to tracking table   
      $result1 = $db->sql_query ("INSERT INTO " . $prefix . "_eto_tracking (log_id, ateam_id, dteam_id, tid, move_dt, action)".
         "VALUES ('NULL','$team_id','$dteam_id','$tid',CURRENT_TIMESTAMP(),'$action')");
      
      //Update Fuel Points in lookup table.
      $rp_id = '1'; // Fuel Resource point ID
      $r_points = '-1'; //deducted fuel point for move
      $rp_details = 'Burned Fuel Moving on Map'; //Details
      $result2 = $db->sql_query ("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')");
      
      
      $enemy = checkmove($tid, $team_id); //Check to see if enemy exist on territory
      echo $enemy;
      die();

      if ($enemy = TRUE) {


//      checkmove($tid, $team_id); //Check to see if enemy exist on territory
//      if ($enemy = TRUE) {
      Header("Location: modules.php?name=Campaign&file=Attack&team_id=$team_id");
      }else{ //No enemy occupies territory
      echo "<P><font color=RED size=6><center>Movement Orders Have Been Sent!</center></font></p>";
      echo "<P><font color=RED size=5><center>...Redirecting</center></font></p>";
      
      }   

}


And my function:


Code:
//Check if enemy exist on territory if so attack

function checkmove($tid, $team_id){
require_once("mainfile.php");
$module_name = basename(dirname(__FILE__));
include_once("modules/$module_name/language/lang-english.php");
global $db, $prefix, $module_name, $admin_file;
define('INDEX_FILE', true);
$index = 1;

 //Find the value of the division ID.
  $sql = "SELECT div_id FROM " . $prefix . "_tc_teams WHERE team_id ='$team_id'";
  $result = $db->sql_query($sql);
  $mydiv = $db->sql_fetchrow($result);
  $div_id = $mydiv['div_id'];

//Look for enemy teams that may be on territory
$sql = "SELECT * FROM " . $prefix . "_tc_teams WHERE tid ='$tid' AND div_id != '$div_id'";
$result = $db->sql_query($sql);

if (!$result) {
   echo 'sql failed . <br />';
            echo 'query was ' . $sql . '<br />';
            echo mysql_errno() . '<br />';
            echo mysql_error() . '<br />';
            die();
}

$enemy = FALSE;
   if ($db->sql_numrows($result) > 0) {
$enemy = TRUE; //Enemy exist on territory
}
 return $enemy;
}
 
evaders99







PostPosted: Sun Dec 10, 2006 9:47 pm Reply with quote

Okay, so the only way $enemy returns true is if this query executes
Code:


$sql = "SELECT * FROM " . $prefix . "_tc_teams WHERE tid ='$tid' AND div_id != '$div_id'";

And it returns more than 0 rows
Code:


if ($db->sql_numrows($result) > 0) {

There must be some logical problem in your code, perhaps it is not passing the right $tid or $div_id
 
Donovan







PostPosted: Mon Dec 11, 2006 8:40 am Reply with quote

But the echo of my $sql variable displayed

Code:
SELECT * FROM nuke_tc_teams WHERE tid ='36' AND div_id != '1'


Which says to me that both tid and div_id are correct and contain the proper values.

I ran another test in the SQL pane

SELECT * FROM nuke_tc_teams WHERE tid ='19' AND div_id !='1'

which returned an emty set as it should.

But this in my function

Code:
$enemy = FALSE;

   if ($db->sql_numrows($result) > 0) {
$enemy = TRUE; //Enemy exist on territory
}
echo 'query was ' . $enemy . '<br />';


returns "query was" and enemy is blank

Is my problem that it is returning an empty set versus returning a 0?
 
Guardian2003
Site Admin



Joined: Aug 28, 2003
Posts: 6799
Location: Ha Noi, Viet Nam

PostPosted: Mon Dec 11, 2006 8:53 am Reply with quote

I'm far from an expert but I'm just wondering why in one snippet you are assigning $enemy to a function
Code:
$enemy = checkmove($tid, $team_id);
and then elsewhere you are assigning it as a logic True/False.
 
View user's profile Send private message Send e-mail
evaders99







PostPosted: Mon Dec 11, 2006 9:08 am Reply with quote

He's talking about two different pieces of code Guardian .. using the same variable name $enemy inside the function as well as outside. Certainly should work fine

My q: why do you need all this code after the function definition
Code:


function checkmove($tid, $team_id){
require_once("mainfile.php");
$module_name = basename(dirname(__FILE__));
include_once("modules/$module_name/language/lang-english.php");



---------------------------

Alright, I see where the error is, your conditional here is wrong

Code:


if ($enemy = TRUE) {

SIMPLY CHANGE TO

if ($enemy) {
 
fkelly







PostPosted: Mon Dec 11, 2006 9:49 am Reply with quote

Also, and I had to confirm this for myself, try this little bit of code and see what the results are:

Code:
<?php

   $test = FALSE;
   echo 'value of  test ' . $test . '<br>';
?>

and i know I didn't close the br :)
 
Donovan







PostPosted: Mon Dec 11, 2006 10:01 am Reply with quote

Code:
if ($enemy = TRUE) { 


SIMPLY CHANGE TO

if ($enemy) {


This was the fix. Thanks Evaders. I don't know why I had all that in my function. I know at one time I had my globals outside of the function and was getting member not found errors. Maybe that is why I put all that there.
 
Donovan







PostPosted: Mon Dec 11, 2006 11:12 am Reply with quote

One last question on this topic.

If I use the exit() at the end of this code I loose my right blocks. How do I avoid this but still kill the rest of the page from displaying.

Code:
$enemy = checkmove($tid, $team_id); //Check to see if enemy exist on territory

if ($enemy) {
   exit();
}else{
Opentable();
echo"<center><h3><b>" . _ATTACKTEAMS. "</b></h3></center><br><br>"
."<center><b><font color=RED size=4> There are no enemy forces positioned on your territory! </font></b></center><br><br>"
."<center><b> Your team is not in a position to attack at this time! </b></center><br><br>"
."<center><b> Move your team to a territory and engage enemy forces! </b></center><br><br>"
."<br>";
Closetable();
exit();
}
 
evaders99







PostPosted: Mon Dec 11, 2006 12:51 pm Reply with quote

You don't need an exit() command unless you want the PHP script to immediately stop.
Just use the proper if statements to show whatever content needs to be displayed.

If there really is a reason you need to break the flow of your program, and still display right blocks, you can immediately pass the file to footer.php and that will close things out
Code:


include('footer.php');
 
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 ©