Question with selecting info from tables

For discussions about game development that does not fit in any of the other topics.
Post Reply
Baseball435
Posts: 548
Joined: Sun May 30, 2010 3:49 am

Question with selecting info from tables

Post by Baseball435 »

Hey everyone. I was wondering how I would make a high score type of thing where they rank up and then I have something that selects the top 10 players and prints them out. I don't know how I would have it constantly update so that it checks who has the highest 'level' and then moves them to a higher level 'rank' in the mysql table. So in the table would be 'level' and 'rank' and then how would I have it update to see if the players level is higher than someone else and then move them up in 'rank' and then how to do SELECT * from users in the query so that it selects the top ten. Thanks!
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Question with selecting info from tables

Post by Jackolantern »

They don't need to be moved in the MySQL table. Data's order in their table is not important. All you have to do is have a field in the table that the results are ordered by, and arrange the results by that field, limiting the results to however many you want to show, like "ORDER BY score ASC LIMIT 20", would order results based on the "score" field in ascending order, and only return 20 results.

See this thread for discussion and code dealing with a high score script. I think it starts on about the 2nd page or so. You could then just have the high score page run whenever anyone looks at the high score page. Then if your game gets busy you can shift it to a cron job.
The indelible lord of tl;dr
Baseball435
Posts: 548
Joined: Sun May 30, 2010 3:49 am

Re: Question with selecting info from tables

Post by Baseball435 »

Okay thanks! And also what exactly is a cron job? I've heard of it but never looked into it
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Question with selecting info from tables

Post by Jackolantern »

It just so happens Halls made a video tutorial about them!
The indelible lord of tl;dr
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Question with selecting info from tables

Post by Callan S. »

Jackolantern wrote:They don't need to be moved in the MySQL table. Data's order in their table is not important. All you have to do is have a field in the table that the results are ordered by, and arrange the results by that field, limiting the results to however many you want to show, like "ORDER BY score ASC LIMIT 20", would order results based on the "score" field in ascending order, and only return 20 results.
How does it pass on the values for that? I'm used to something like ['score'], but with 20 or whatever number, does it use something like ['score'][0] to pass on each entry?
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Question with selecting info from tables

Post by Jackolantern »

Sadly, no, you can't access them that way. Here is a script as an example:

Code: Select all

//check to see what monsters the logged in player has from the 'playermonster' table
					$sql = "SELECT * FROM playermonster WHERE playerid = '$playerid'";
					if ($result = mysqli_query($db, $sql)) {
						//fetch assoc array and set up table
						?>
						<table border="3" cellpadding="3" cellspacing="0">
						<tr>
							<th align="center">Name</th>
							<th align="center">Level</th>
							<th align="center">Monster Type</th>
							<th align="center">Monster Species</th>
							<th align="center">Max Health</th>
							<th align="center">Attack</th>
							<th align="center">Defense</th>
							<th align="center">Speed</th>
							<th align="center">Mind</th>
							<th align="center">Will</th>							
						</tr>
						<?php 
						while ($row = mysqli_fetch_row($result)) {						
							
							//create the table to choose a monster
							?> 
							
							<tr>
							    
								<td align="center"><?php echo("<a href='monsterselected.php?mid=$row[0]'>$row[2]</a>") ?> </td>
									<td align="center"><?php echo($row[13]) ?> </td>
									<td align="center"><?php echo($row[3]) ?> </td>
									<td align="center"><?php echo($row[4]) ?> </td>
									<td align="center"><?php echo($row[6]) ?> </td>
									<td align="center"><?php echo($row[8]) ?> </td>
									<td align="center"><?php echo($row[9]) ?> </td>
									<td align="center"><?php echo($row[10]) ?> </td>
									<td align="center"><?php echo($row[11]) ?> </td>
									<td align="center"><?php echo($row[12]) ?> </td>
															
							</tr>							
						
						<?php //go back into PHP for rest of script
						}
Keep in mind that when you use mysqli_fetch_row(), it returns a standard array, not an associated array. The contents of each column is stored like: $row[0] for the first column, $row[1] for the 2nd, $row[9] for the 10th, etc.
The indelible lord of tl;dr
Post Reply

Return to “General Development”