jc_ply Posted March 22, 2008 Share Posted March 22, 2008 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 Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted March 22, 2008 Share Posted March 22, 2008 Try this <?php $query = "SELECT SUM(times_selected) as Total FROM war"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); $percent = $row['Total'] / ($row['Total'] x 100); echo $row['Total'].' | '. $percent; ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted March 22, 2008 Share Posted March 22, 2008 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); ?> Quote Link to comment Share on other sites More sharing options...
jc_ply Posted March 23, 2008 Author Share Posted March 23, 2008 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); ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted March 23, 2008 Share Posted March 23, 2008 Sorry, $max = array_max($data); should be $max = max($data); Quote Link to comment Share on other sites More sharing options...
jc_ply Posted March 23, 2008 Author Share Posted March 23, 2008 Thank you Barand you are a legend! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.