Page 2 of 3
Re: isset and variables
Posted: Sun Jul 18, 2010 4:33 am
by alexrules01
well while I have this thread I might ask another question. For my game I want to list all he dogs one player owns, but I'm not sure how.
I've got this so far, I'm not sure where to go to from here
Thanks!
$pid = $playerinfo3['id'];
$playerinfo="SELECT * from greyhounds where pid='$pid'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);
Re: isset and variables
Posted: Sun Jul 18, 2010 4:48 am
by Jackolantern
This is the basic code to pull up and list multiple results from the database:
Code: Select all
$sql = "SELECT * FROM playermonster WHERE playerid = '$playerid'";
if ($result = mysql_query($sql)) {
while ($row = mysql_fetch_row($result)) {
$playerID = $row[0]; //stores the first field in a variable
echo("Player ID: ".$playerID); //prints out the first field
$playerName = $row[1]; //stores the second field in a variable
echo("Player name: ".$playerName); //prints out the second row
//..and so on, to print out or manipulate as many rows as you need.
} //end while
} else {
echo("No results found.");
}
The IF structure will process if there are results. The WHILE structure will loop through all of the results, performing the contained logic with each of the results one after another. When it reaches the end of the results, mysql_fetch_row() results FALSE, and the loop ends. Note that you cannot access the row data in an associated array style, such as $row['playerHealth']. Instead, you must access it as a traditional array, with each field stored in a number, so the first column value is $row[0], the second is $row[1], third is $row[2], etc.
Re: isset and variables
Posted: Sun Jul 18, 2010 5:53 am
by alexrules01
Thanks once again Jack, you got the answer to everything

Re: isset and variables
Posted: Sun Jul 18, 2010 6:05 am
by Jackolantern
I don't know about
that, but your very welcome

Re: isset and variables
Posted: Sun Jul 18, 2010 11:32 am
by alexrules01
Just one more little question, and hopefully this will be it. I'm using the code above, how woul I go about making each name a link or below/next to each name placing a link saying like "Check stats" as I want to be able to list all the dogs a player owns and from there he can look at their stats.
Re: isset and variables
Posted: Sun Jul 18, 2010 5:11 pm
by Jackolantern
You could do something like this in the WHILE loop:
Code: Select all
$dogID = $row[0]; //Assuming the first column is the dog's unique ID
$dogName = $row[1]; //Assuming the second row is the dog's name
echo "<a href='dogselected.php?dogid=".$dogID."'>Select ".$dogName."</a><br />";
But then on the next page you would have to check the database again to make sure the dog belongs to the player since it is being passed in the URL through a GET value:
Code: Select all
//make short variables of the dog ID from the URL and player ID from the session variable
$dogSelected = $_GET['dogid'];
$playerID = $_SESSION['playerid'];
//query db to make sure player has not entered someone else's dog into the URL
$sql = "SELECT * FROM playerdogs WHERE playerid = '$playerID' AND dogid = '$dogSelected'";
$rec = mysql_query($sql);
if (!$rec) {
echo "No dog found! Please try again!";
exit();
}
//get dog info from results
$dogInfo = mysql_fetch_assoc($rec);
//grab any dog variables
$dogName = $dogInfo['name'];
Of course this is not the full script (you at least need to also start the session and scrub the GET value to prevent SQL injection), but shows the check to make sure the player is not exploiting the URL GET value.
Re: isset and variables
Posted: Mon Jul 19, 2010 1:06 pm
by alexrules01
It works! I didn't know what I needed for the second page. Plugged in my own variables and such and it works

Thanks Jack

Re: isset and variables
Posted: Mon Jul 19, 2010 3:10 pm
by Jackolantern
Glad to hear it

Re: isset and variables
Posted: Sat Jul 24, 2010 2:41 am
by alexrules01
Hi again

Theres one thing i want to ask, similar to the stuff I asked above.
So I am writing the pages for a player to nominate there dogs for a race. On the firstpage there will be a list of the racetracks, and once you select your track, it goes to next page and you choose which one of your dogs you want to nominate and the distance you want them to run. Then it goes to a 3rd page which will insert all the info into the database.
SO its like Tracklist.php ---> Nominate1.php ---> Nominate2.php
The problem I am having is I want to carry the information from the tracklist page to nominate2, but because I am using a form on nominate1 and submit, I dont know how to carry the tracklist information over to nominate2.
Is there a way to carry information to the page?
I know that this problem probably could be fixed with javascript or ajax, one of the two...but I don't know them

Re: isset and variables
Posted: Sat Jul 24, 2010 6:27 am
by Jackolantern
If you want to pass data from one page's form directly to the next page, just set the form's method as "post", and then your NAME attributes from the form will be accessible in the next script through the $_POST variable (like $_POST['racetrack']). If you need to store something for several pages, store it in a session variable ($_SESSION['dogchoice'] = $dogSelected;).