0

This may be a very simple question, but I've tried many different ways to attempt this only to find syntax errors. I have the following code:

 $query = "SELECT * FROM plyr where posid = '4' order by teamname";
 $result = mysql_query($query);
 while($row = mysql_fetch_assoc($result))
 {
 [
     $dd .= "<option value='{$row['name']}'>{$row['name']}, {$row['teamname']}]</option>";
 } 

 echo"<label for=\'choice4[]\'>Select Two Players:</label><br>
 <select multiple=\"multiple\" name=\"choice4[]\">
 <option value=\"$dd\">$dd</option>
 </select>";

to which I'd like to add some statistical averages to $dd. The statistics are stored in my db as integers, so I need to do some on the fly calculations to get averages to display. For example, I'd like to add PPG but need to add in an equation for pts/gm and not sure how to do that. I've tried to do {$row['pts'/'gm']} and {$row['pts']/$row['gm']} and other methods but I'm not hitting on the right one. Any help is very appreciated!

4

2 回答 2

1

for the calcuations part. i would suggest specifically casting those rows as integers/floats. use intval for whole numbers and floatval for decimals.

$ppg = intval($row['pts']) / floatval($row['gm']);

for the creating selects, i like to do it like this:

$query = "SELECT * FROM plyr where posid = '4' order by teamname";
$result = mysql_query($query);

$select = '<select name="whatever">';
while($row = mysql_fetch_assoc($result)) {
    $ppg = intval($row['pts']) / floatval($row['gm']);
    $select .= "<option value='".$row['name']."'>".$row['name'].", ".$row['teamname']."PPG: ".$ppg."</option>";
}
$select .= '</select>';
echo ($select);

but that's just me. i perfer the ".$var." syntax over the {$var} syntax.

UPDATE: i added the PPG calculation into the while loop and appended it into your select value. but you realize that this calculation will happen for ever row in your while loop right? if you wanted to do an overall, perhaps add the values to an array and use them outside your loop. e.g.

$query = "SELECT * FROM plyr where posid = '4' order by teamname";
$result = mysql_query($query);

$arr = array();

$select = '<select name="whatever">';
while($row = mysql_fetch_assoc($result)) {
    $arr[] = array(
        'pts' => intval($row['pts']),
        'gm' => floatval($row['gm'])
    );
    $select .= "<option value='".$row['name']."'>".$row['name'].", ".$row['teamname']."</option>";
}
$select .= '</select>';

//calculate average PPG
$pts = 0;
$gm = 0;
foreach($obj in $arr) {
    $pts += $obj['pts'];
    $gm += $obj['gm'];
}
$ppg = ($pts / count($arr)) / ($gm / count($arr));
echo ("average PPG: ".$ppg);
echo ($select);

im not really sure what PPG is or how you want to use it. but i hope one of these examples helps.

于 2013-01-04T19:02:56.573 回答
0

Why not set your variables in your while function, and do the math there? I'm not sure exactly what you're trying to add/subtract/divide/etc. but here's a start. If you let me know what you're wanting to do, maybe I can better understand and come up with a solution.

<?php

$query = "SELECT * FROM plyr where posid = '4' order by teamname";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
     $dd = '<option value="'.$row['name'].'">'.$row['name'].','.$row['teamname'].'</option>';
     // math equation
     $PPG = $pts / $gm;
}

 echo '<label for="choice4[]">Select Two Players:</label><br>
 <select multiple="multiple" name="choice4[]">
 <option value="'.$dd.'">'.$dd.'</option>
 </select>';

?>
于 2013-01-04T18:57:32.140 回答