Jump to content

Simple function help?


jc_ply

Recommended Posts

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

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

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

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


$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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.