Jump to content

Recommended Posts

The code listed below works with no errors, but I want to avoid the user from finding duplicate locations. How would I go about doing that?

 

<?PHP

require_once('auth.php'); 
include("gm_cnst.php"); 

//Set the chance to find another player
$chance = (($user[scouts]*$bonus[scouts]) + ($user[scouts]*$bonus[explore])) * rand(0,5);


//Set number of users in db
$qry="SELECT member_id FROM members";
$result = mysql_query($qry);
$count = 0;	
while(mysql_fetch_array($result)) 
{
$count++;
}   	

//Make sure the user has enough turns
      if($user[turns] <= 0)
   {
            header('refresh: 4; url= exploration.php');
            echo $error_header;
            echo "You do not have enough turns to do that.";
            echo $footer;
            exit();
   }

//Search for other players
      	if($chance <=0 and $chance < 999)
   	{
        header('refresh: 4; url= exploration.php');
        echo $error_header;
        echo "Your Scouts searched the lands, but only came back tired and hungry.";
        echo $footer;
        exit();
   	}      

		elseif($chance > 1000 and $chance < 4999)
	{
		$leader = rand(1,$count);
		$qry="SELECT * FROM members WHERE member_id='".$leader."'";
		$result = mysql_query($qry);
		$loc = mysql_fetch_array($result);
		echo "You have found the borders of ";
		echo $loc['kingdom']; 
		echo "<br>where "; 
		echo $loc['leader']; 
		echo " resides.<p>";
		echo $footer;
		mysql_query("INSERT INTO member_locations (leader, found) VALUES ($user[member_id],$leader)");
		exit();
	}

		elseif($chance > 5000 and $chance < 9999)
	{
		header('refresh: 4; url= exploration.php');
		echo $success_header;

			for ( $c = 1; $c <= 2; $c++) 
		{
			$leader = rand(1,$count);
			$qry="SELECT * FROM members WHERE member_id='".$leader."'";
			$result = mysql_query($qry);
			$loc = mysql_fetch_array($result);
			echo "You have found the borders of ";
			echo $loc['kingdom']; 
			echo "<br>where "; 
			echo $loc['leader']; 
			echo " resides.<p>";
			print_r($opp_leader);
			echo $footer;
			mysql_query("INSERT INTO member_locations (leader, found) VALUES ($user[member_id],$leader)");
			exit();
		}
	}
?>

Link to comment
https://forums.phpfreaks.com/topic/186466-trying-to-avoid-duplicate-results/
Share on other sites

You can use sessions to store an array of locations recently found and do a loop to check that array and if the location was found find a new one. If you want this to last longer than the session, permanently, I would suggest creating a new table in your database, "locations_found", which will list a memberid and locationid of the locations that a member has already found. Then you would just check against that to see if the locations has already been discovered by a user.

 

Hope that makes sense, good luck!

Well, I made a new table called member_locations that shows who finds what. The problem I am having is first retrieving this data to compare the results (as it is on numerous rows), and second, incorporating it into the code I have so far. The nestings are getting a little confusing to me. Currently members_location is set up like:

 

| leader | | found|

|    1      | |    2  |

|    1      | |    3  |

 

How do you fetch results from multiple rows?

 

I know that I would want to do something like IF $leader = found chose again, but it is getting the found I am having troubles with.

You can fetch the results of multiple rows via:

 

$sql = "SELECT found FROM locations_members WHERE leader = some_condition";
$query = mysql_query($sql) or trigger_error("Query Failed " . mysql_error());

$found = false;
while ($row = mysql_fetch_assoc($query)) {
      if ($row['found'] == $new_location) {
             echo 'Location already found.';
             $found = true;
             break;
     }
}

if (!$found) {
     // insert into memberlocations
}else {
    // do something that you want to do
}

 

That is what you are looking for I believe.

Well looking at the code I wrote I think this might be better suited for you:

 

$sql = "SELECT found FROM locations_members WHERE leader = some_condition";
$query = mysql_query($sql) or trigger_error("Query Failed " . mysql_error());

if (mysql_num_rows($query) > 0) {
    $locations = array();
    while ($row = mysql_fetch_assoc($query)) {
         $locations[] = $row['found'];
    }

   while (in_array($new_location, $locations)) {
          $new_location = "some random location";
   }
}

// insert the $new_location variable to the DB here

 

I was just bored and saw that the above would probably suit your needs better.

 

EDIT:

The one thing you will have to watch out for is if they have discovered everything then this will be an infinite loop, so yea.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.