Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.