Newbie Question
Moderator: General Moderators
Newbie Question
OK...im building this stats tracking for a video game where I input final results into the DB and it generates some output...i have most of it working...here is a Breif descrption of my problem
PHP:--------------------------------------------------------------------------------
<?php
$db = mysql_connect($databaseserver, $databaseuser, $databasepass);
mysql_select_db($databasename,$db);
$sortby = "margin_permap ASC";
$sql="SELECT * FROM $playerstable ORDER BY $sortby";
$result=mysql_query($sql,$db) or die (mysql_error());
$num = mysql_num_rows($result);
$cur = 1;
echo "<ol>";
while ($num >= $cur) {
$row = mysql_fetch_array($result);
$name = $row["name"];
$squad = $row["squad"];
$scores = $row["scores"];
$kills = $row["kills"];
$deaths = $row["deaths"];
$maps_played = $row["maps_played"];
$margin_permap = $row["margin_permap"];
$margin = $scores-$deaths;
$ratio = $kills/$deaths;
$deaths_permap = $deaths/$maps_played;
$scores_permap = $scores/$maps_played;
$kills_permap = $kills/$maps_played;
$margin_permap = $margin/$maps_played;
?>
--------------------------------------------------------------------------------
I Can Not get the Output to show $MARGIN_PERMAP in the correct order best to worst..I can change the $sortby to kills and it will output correctly so that kinda tells me the Calculation Im making in this cant do the Fuction twice?? Its not storing the calculations to the DB for these:
$margin = $scores-$deaths;
$ratio = $kills/$deaths;
$deaths_permap = $deaths/$maps_played;
$scores_permap = $scores/$maps_played;
$kills_permap = $kills/$maps_played;
$margin_permap = $margin/$maps_played;
by looking at the above info I supplied can you understand what Im trying to do...here is a Link of the output and notice how the LAST COLUMN doesnt sortby best to worst score..ADMIN should be listed in 1st place with 18.17
My Code may be CRUDE but I am TOTAL Newbie..actually just finished reading my 1st book...lol...but I love learning from everyone...any help would be appreciated...i think i know what problems are..just dont know how to fix itOUTPUT PIC
PHP:--------------------------------------------------------------------------------
<?php
$db = mysql_connect($databaseserver, $databaseuser, $databasepass);
mysql_select_db($databasename,$db);
$sortby = "margin_permap ASC";
$sql="SELECT * FROM $playerstable ORDER BY $sortby";
$result=mysql_query($sql,$db) or die (mysql_error());
$num = mysql_num_rows($result);
$cur = 1;
echo "<ol>";
while ($num >= $cur) {
$row = mysql_fetch_array($result);
$name = $row["name"];
$squad = $row["squad"];
$scores = $row["scores"];
$kills = $row["kills"];
$deaths = $row["deaths"];
$maps_played = $row["maps_played"];
$margin_permap = $row["margin_permap"];
$margin = $scores-$deaths;
$ratio = $kills/$deaths;
$deaths_permap = $deaths/$maps_played;
$scores_permap = $scores/$maps_played;
$kills_permap = $kills/$maps_played;
$margin_permap = $margin/$maps_played;
?>
--------------------------------------------------------------------------------
I Can Not get the Output to show $MARGIN_PERMAP in the correct order best to worst..I can change the $sortby to kills and it will output correctly so that kinda tells me the Calculation Im making in this cant do the Fuction twice?? Its not storing the calculations to the DB for these:
$margin = $scores-$deaths;
$ratio = $kills/$deaths;
$deaths_permap = $deaths/$maps_played;
$scores_permap = $scores/$maps_played;
$kills_permap = $kills/$maps_played;
$margin_permap = $margin/$maps_played;
by looking at the above info I supplied can you understand what Im trying to do...here is a Link of the output and notice how the LAST COLUMN doesnt sortby best to worst score..ADMIN should be listed in 1st place with 18.17
My Code may be CRUDE but I am TOTAL Newbie..actually just finished reading my 1st book...lol...but I love learning from everyone...any help would be appreciated...i think i know what problems are..just dont know how to fix itOUTPUT PIC
if you're calculating $margin_permap with stuff you get out of the db ($margin_permap = $margin/$maps_played; ) what's in the db for margin_permap? is it possible that it's sorting by that but it's a different order than what you think? i mean...if you change it after the query, it must be something different in the db when you make the query so maybe it's working correctly, just the stuff in it isn't right (or whatever)....hope that makes sense.
you say that it's not storing the calculations for those variables... do you mean it's not storing them in the DB? are you making another query later on along the lines of
when you get information from a database, you get a COPY of it, so that when you edit the copy you received, it doesn't actually change the DB... you have to do that manually.
Code: Select all
"UPDATE $playerstable SET margin_permap='$margin_permap',їall the other variables] WHERE їunique ID stuff]"- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
There's not a lot of point storing values in a table that are just calculated from other values in the table because you can get all the info you need when you do your select,
Then to order by margin_permap have:
You can access all the variables in a similar way to what you did before:
Mac
Code: Select all
$sql="SELECT name, squad, scores, kills, deaths, maps_played, margin_permap, (scores-deaths) AS margin, (kills/deaths) AS ratio, (deaths/maps_played) AS deaths_permap, (scores/maps_played) AS scores_permap, (kills/maps_played) AS kills_permap, ((scores-deaths)/maps_played) AS margin_permap FROM $playerstable ORDER BY $sortby";Code: Select all
$sortby = "margin_permap ASC";Code: Select all
$result=mysql_query($sql) or die (mysql_error());
echo '<ol>';
while ($row = mysql_fetch_assoc($result)) {
$name = $rowї'name'];
.
.
.
$maps_played = $rowї'maps_played'];
$margin = $rowї'margin'];
.
.
.
$margin_permap = $rowї'margin_permap'];
}- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Code: Select all
<?php
$db = mysql_connect($databaseserver, $databaseuser, $databasepass);
mysql_select_db($databasename,$db);
$sortby = "margin_permap ASC";
$sql = "SELECT name, squad, scores, kills, deaths, maps_played, (scores-deaths) AS margin, (kills/deaths) AS ratio, (deaths/maps_played) AS deaths_permap, (scores/maps_played) AS scores_permap, (kills/maps_played) AS kills_permap, ((scores-deaths)/maps_played) AS margin_permap FROM $playerstable ORDER BY $sortby";
$result=mysql_query($sql) or die (mysql_error());
echo "<ol>";
while ($row = mysql_fetch_assoc($result)) {
$name = $rowї"name"];
$squad = $rowї"squad"];
$scores = $rowї"scores"];
$kills = $rowї"kills"];
$deaths = $rowї"deaths"];
$maps_played = $rowї"maps_played"];
$margin = $rowї"margin"];
$kills_permap = $rowї"kills_permap"];
$deaths_permap = $rowї"deaths_permap"];
$ratio = $rowї"ratio"];
$scores_permap = $rowї"scores_permap"];
$margin_permap = $rowї"margin_permap"];
}
?>
<tr>
<td width="12%" bgcolor="#222222" align="center" nowrap> <font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#CCCCCC">
<?php echo "$name" ?>
</font></td>
<td width="8%" bgcolor="#222222" align="center" nowrap> <font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#CCCCCC">
<?php echo "$squad" ?>
</font></td>
<td width="8%" bgcolor="#111111" align="center" nowrap> <font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#CCCCCC">
<?php echo "$scores" ?>
</font></td>
<td width="8%" bgcolor="#222222" align="center" nowrap> <font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#CCCCCC">
<?php echo printf("%.2f", $scores_permap); ?>
</font></td>
<td width="8%" bgcolor="111111" align="center" nowrap> <font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#CCCCCC">
<?php echo "$kills" ?>
</font></td>
<td width="8%" bgcolor="#222222" align="center" nowrap> <font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#CCCCCC">
<?php echo printf("%.1f", $kills_permap); ?>
</font></td>
<td width="8%" bgcolor="111111" align="center" nowrap> <font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#CCCCCC">
<?php echo "$deaths" ?>
</font></td>
<td width="8%" bgcolor="#222222" align="center" nowrap> <font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#CCCCCC">
<?php echo printf("%.1f", $deaths_permap); ?>
</font></td>
<td width="8%" bgcolor="111111" align="center" nowrap> <font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#CCCCCC">
<?php echo "$maps_played" ?>
</font></td>
<td width="8%" bgcolor="#222222" align="center" nowrap> <font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#CCCCCC">
<?php echo printf("%.2f", $ratio); ?>
</font></td>
<td width="8%" bgcolor="#222222" align="center" nowrap> <font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#CCCCCC">
<?php echo printf("%.2f", $margin); ?>
</font></td>
<td width="8%" bgcolor="#222222" align="center" nowrap> <font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#CCCCCC">
<?php echo printf("%.2f", $margin_permap); ?>
</font></td>
</tr>
<?php
echo "</ol>";
?>- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Your problem is with the while loop and what it emcompases,
Every time that loop is run the values of the variables are all overwritten so all you have saved by the end is the last row from the result set.
Try changing
to
Note how the closing brace (}) for the while loop has been moved to after the HTML (I removed the closing <ol> tag as it didn't seem to have any purpose). This way the HTML becomes part of the loop and is created for each record in the result set.
Mac
Code: Select all
while ($row = mysql_fetch_assoc($result)) {
$name = $rowї"name"];
$squad = $rowї"squad"];
$scores = $rowї"scores"];
$kills = $rowї"kills"];
$deaths = $rowї"deaths"];
$maps_played = $rowї"maps_played"];
$margin = $rowї"margin"];
$kills_permap = $rowї"kills_permap"];
$deaths_permap = $rowї"deaths_permap"];
$ratio = $rowї"ratio"];
$scores_permap = $rowї"scores_permap"];
$margin_permap = $rowї"margin_permap"];
}Try changing
Code: Select all
// bunch of php
$scores_permap = $rowї"scores_permap"];
$margin_permap = $rowї"margin_permap"];
}
?>
<!-- LOTS OF HTML -->
<?php
echo "</ol>";
?>Code: Select all
// bunch of php
$scores_permap = $rowї"scores_permap"];
$margin_permap = $rowї"margin_permap"];
?>
<!-- LOTS OF HTML -->
<?php
}
?>Mac