Jump to content

Percentage calculation help?


jc_ply

Recommended Posts

Hey guys having a bit of trouble with by flash back end (mySQL) and was wondering if you could share the wisdom?

 

I always feel illustration helps so heres my table...(called war)...dont ask

 

war_id  |  times_selected

1          |      5

2          |      3

3          |      9

4          |      17

5          |      3

 

Basically I need to select the highest value of times_selected (17 for example but is actually a counter updated from flash) and find its percentage to return along with its "war_id (4)

 

My psudeo code was along the lines of

 

-----

 

SELECT SUM(times_selected) as "Total"

FROM war

 

($Highest value of times_selected / "Total" x 100 = $var

 

PRINT $highest value war_id, $var (its percentage)

 

-----

 

As you can tell im not too hot with the PHP/mySQL right now and have been pouring over syntax combinations on and off for a while with no success. Also the print is to return it to flash as im sure you know.

 

Any help would be greatly appreciated.

 

Jason

Link to comment
Share on other sites

try

<?php
$sql = "SELECT war_id, times_selected FROM war";
$res = mysql_query ($sql);
$data = array();
while (list ($id, $times) = mysql_fetch_row($res))
{
    $data[$id] = $times;
}

$max = array_max($data);
$maxid = array_search ($max, $data);
$total = array_sum ($data);

printf ('Highest value ID : %d selected %d%% times', $maxid, $max * 100 / $total);
?>

Link to comment
Share on other sites

Wow guys thanks for the responses, tried and fiddled with both but not getting any luck. Im thinking the likelyhood is though that Im doing something wrong and your code is right. Below is exactly what my php file looks like. Any thing stupid Im doing?

 

Also if I could get the results (times_selected's total, highest number and its ID) I could just do the percentage calculation in flash. Is that easier?

 

Thanks

 


<?php
// 1. Create a database connection
$connection = mysql_connect("Localhost","*****","*****");
if (!$connection) {
die("Database connection failed: " . mysql_error());
}

// 2. Select a database to use
$db_select = mysql_select_db("digitall_iview",$connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}

// Imported variables flash

$name = $_POST['user_name'];
$war_id = $_POST['war_id'];
$pol_id = $_POST['pol_id'];
$apo_id = $_POST['apo_id'];
$network = $_POST['network'];

// Inserts users selections into a new user database entry

$query = "INSERT INTO users (user_name, network, war, politics, apoca
) VALUES ('$name', '$network', '$war_id', '$pol_id', '$apo_id')";
if(mysql_query($query, $connection)) {
// success!
} else {
// Display error message.
echo "Subject creation failed. ";
};

// Increments chosen image by 1

$query = mysql_query("UPDATE war SET
selected = selected + 1
WHERE war_id = '$war_id'");

// Increments the users amount by 1 for chosen network

$query = mysql_query("UPDATE net_war SET
user_amt = user_amt +1, war_one = war_one +1
WHERE network = '$network'");
*/

$sql = "SELECT war_id, selected FROM war";
$res = mysql_query ($sql);
$data = array();
while (list ($id, $times) = mysql_fetch_row($res))
{
$data[$id] = $times;
}

$max = ($data);
$maxid = array_search ($max, $data);
$total = array_sum ($data);

print ('Highest value ID : %d selected %d%% times',$maxid, $max * 100 / $total);

mysql_close($connection);

?>

Link to comment
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.