jc_ply Posted April 11, 2008 Share Posted April 11, 2008 Hey guys been staring at this one for a while and can't figure out why I get nothing back. Any help would be great $query = mysql_query("SELECT * FROM net_war WHERE network='london'"); while($rows = mysql_fetch_array($query)){ $score_array = array($rows[war_one], $rows[war_two], $rows[war_three], $rows[war_four], $rows[war_five]); $max_score = MAX($score_array); $sum_scores = SUM($score_array); $percentage = ($max_score / $sum_scores) * 100; // echo "Average Score is $percentage <br>"; } the table looks something like this (net_war) id | network | user_amt | war_one | war_two | war_three | war_four | war_five 1 london 90 50 3 17 21 37 I basically need the name of the highest value (war_one) and its percentage in relation to the other war_. Looking at it seems fine to me but I'd rather be proved wrong then sit here for another 5 hours thinking I'm right. Cheers. Link to comment https://forums.phpfreaks.com/topic/100597-simple-function-help/ Share on other sites More sharing options...
zenag Posted April 11, 2008 Share Posted April 11, 2008 if u need the fieldname of highestvalue u must write another sql query... Link to comment https://forums.phpfreaks.com/topic/100597-simple-function-help/#findComment-514484 Share on other sites More sharing options...
zenag Posted April 11, 2008 Share Posted April 11, 2008 it will give the maximum fieldname... $query = mysql_query("SELECT * FROM net_war WHERE network='london'"); while($rows = mysql_fetch_array($query)){ $score_array = array("war_one"=>$rows[war_one], "war_two"=>$rows[war_two], "war_three"=>$rows[war_three], "war_four"=>$rows[war_four], "war_five"=>$rows[war_five]); $max_score = MAX($score_array); $sum_scores = SUM($score_array); $key = array_search($max_score, $score_array); echo "<td>Maximum score in " , $key , "</td></tr>"; $percentage = ($max_score / $sum_scores) * 100; // echo "Average Score is $percentage "; Link to comment https://forums.phpfreaks.com/topic/100597-simple-function-help/#findComment-514487 Share on other sites More sharing options...
jc_ply Posted April 11, 2008 Author Share Posted April 11, 2008 Thanks for the quick reply zenag but still no luck...checked and double checked the page just returns nothing like before any more advice?. Link to comment https://forums.phpfreaks.com/topic/100597-simple-function-help/#findComment-514494 Share on other sites More sharing options...
zenag Posted April 11, 2008 Share Posted April 11, 2008 try this example code.... <?php $mysqli = new mysqli("localhost","root","root","world"); $result = $mysqli->query("select * from tblmarks"); $marks = array(); echo "<table border='1' cellpadding='1' cellspacing='2' align='center'>"; echo "<tr>"; echo "<td><b>Name</b></td><td><b>Physics</b></td><td><b>Chemistry</b></td><td><b>Mathematics</b></td><td><b>Max</b></td>"; echo "</tr>"; while($row = $result->fetch_array()) { echo "<tr>"; echo "<td>".$row['name']."</td><td>".$row['physics']."</td><td>".$row['chemistry']."</td><td>".$row['mathematics']."</td>"; $marks = array("physics" => $row['physics'], "chemistry" => $row['chemistry'], "mathematics" => $row['mathematics']); $mx = max($marks); $key = array_search($mx, $marks); echo "<td>Maximum mark in " , $key , "</td></tr>"; } echo "</table>"; ?> Link to comment https://forums.phpfreaks.com/topic/100597-simple-function-help/#findComment-514497 Share on other sites More sharing options...
jc_ply Posted April 11, 2008 Author Share Posted April 11, 2008 thanks but the example will just be another headache for me at the moement. Link to comment https://forums.phpfreaks.com/topic/100597-simple-function-help/#findComment-514508 Share on other sites More sharing options...
zenag Posted April 11, 2008 Share Posted April 11, 2008 it works for me... $query = mysql_query("SELECT * FROM net_war WHERE network='london'"); $score_array = array(); while($rows = mysql_fetch_array($query)){ $score_array = array("war_one"=>$rows['war_one'], "war_two"=>$rows['war_two'], "war_three"=>$rows['war_three'], "war_four"=>$rows['war_four'], "war_five"=>$rows['war_five']); //print_r($score_array); $max_score = max($score_array); $sum_scores = sum($score_array); $key = array_search($max_score, $score_array); } echo "maximum value is ".$key; Link to comment https://forums.phpfreaks.com/topic/100597-simple-function-help/#findComment-514519 Share on other sites More sharing options...
zenag Posted April 11, 2008 Share Posted April 11, 2008 $query = mysql_query("SELECT * FROM net_war WHERE network='london'"); $score_array = array(); while($rows = mysql_fetch_array($query)){ $score_array = array("war_one"=>$rows['war_one'], "war_two"=>$rows['war_two'], "war_three"=>$rows['war_three'], "war_four"=>$rows['war_four'], "war_five"=>$rows['war_five']); //print_r($score_array); $max_score = max($score_array); //$sum_scores = sum($score_array); $key = array_search($max_score, $score_array); } echo "maximum value is ".$key; Link to comment https://forums.phpfreaks.com/topic/100597-simple-function-help/#findComment-514524 Share on other sites More sharing options...
zenag Posted April 11, 2008 Share Posted April 11, 2008 except that sum values...u could assign sum values in sql query.. as select fieldnames,sum(fieldname) from tablname....like that... Link to comment https://forums.phpfreaks.com/topic/100597-simple-function-help/#findComment-514525 Share on other sites More sharing options...
jc_ply Posted April 11, 2008 Author Share Posted April 11, 2008 Thanks zenag got the highest value name with your query your a star. working on the percentage now, could you further explain your suggestion by any chance? Link to comment https://forums.phpfreaks.com/topic/100597-simple-function-help/#findComment-515133 Share on other sites More sharing options...
Barand Posted April 11, 2008 Share Posted April 11, 2008 to sum an aray of values $sum = array_sum($score_array); Link to comment https://forums.phpfreaks.com/topic/100597-simple-function-help/#findComment-515140 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.