Page 1 of 1
Question with selecting info from tables
Posted: Thu Sep 09, 2010 1:52 am
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!
Re: Question with selecting info from tables
Posted: Thu Sep 09, 2010 3:02 am
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.
Re: Question with selecting info from tables
Posted: Thu Sep 09, 2010 11:49 am
by Baseball435
Okay thanks! And also what exactly is a cron job? I've heard of it but never looked into it
Re: Question with selecting info from tables
Posted: Thu Sep 09, 2010 2:35 pm
by Jackolantern
It just so happens Halls made a
video tutorial about them!
Re: Question with selecting info from tables
Posted: Mon Sep 20, 2010 12:11 am
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?
Re: Question with selecting info from tables
Posted: Mon Sep 20, 2010 1:00 am
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.