Jump to content

Row calculation problem


jc_ply

Recommended Posts

Hey guys, you were so helpful with my last issue (which I'm very greatful for) I'm back to share another growing pain, and the last for now at least.

 

my row is looking like this:

 

network  |  user_amt  | war_one | war_two | war_three | war_four | war_five

sample            50              70            5              8              3            9

 

the war_one - five fields are just counters for the users selections but I need to calculate the highest value from the last five war fields (e.g 70)  and divide it by the total value of the war fields (e.g 95) then times by 100 to return a percentage along with the highest fields name.

 

I have similar set ups in the database where the same returns are needed but their universal and can be column calculated where as this table is network specific so proving to be a bit difficult (for my learner level php)

 

Any help would be as always greatly appreciated ,

 

Jason

Link to comment
https://forums.phpfreaks.com/topic/97573-row-calculation-problem/
Share on other sites

Maybe try making an array, finding the max value, then finding the sum value and dividing and multiplying by 100:

$query = mysql_query("SELECT * FROM table_name WHERE id='$id'", $db);
while($rows = mysql_fetch_array){
          $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_score) * 100;
          echo "Average Score is $percentage <br>";
}

 

Maybe try making an array, finding the max value, then finding the sum value and dividing and multiplying by 100:

$query = mysql_query("SELECT * FROM table_name WHERE id='$id'", $db);
while($rows = mysql_fetch_array){
          $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_score) * 100;
          echo "Average Score is $percentage <br>";
}

 

 

$rows is already an array, why would you put each individual result into another array?

sorry the code should have been

 

$query = mysql_query("SELECT * FROM table_name WHERE id='$id'", $db);
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_score) * 100;
          echo "Average Score is $percentage <br>";
}

 

re arraying because original question only wanted top value for 'war' values not the first user_amt value...

 

I am not an expert so would be interested to see another way this could be done..

sorry the code should have been

 

$query = mysql_query("SELECT * FROM table_name WHERE id='$id'", $db);
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_score) * 100;
          echo "Average Score is $percentage <br>";
}

 

re arraying because original question only wanted top value for 'war' values not the first user_amt value...

 

I am not an expert so would be interested to see another way this could be done..

 

Ah. You could just use the mysql MAX and SUM by just choosing the 'war' fields instead of everything. (*)

  • 3 weeks later...

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.