acook Posted February 1, 2008 Share Posted February 1, 2008 I've just switched over to mySQL and I have a simple query that returns values: <html> <?php $link = mysql_connect("localhost:3306", "", "") or die ("Couldn't connec"); $db = "reporting"; mysql_select_db($db) or die("Could not select the database '" . $db . "'. Are you sure it exists?"); $query = "SELECT COUNT(Incident_Id) as IncidentId, Source FROM hd_reporting WHERE (Submitter = 'Me') GROUP BY Source"; //GET SOURCE OF TICKETS (ALL) $query2 = "SELECT Incident_Id FROM hd_reporting WHERE (Submitter = 'Me')"; //GET TOTAL TICKETS $result = mysql_query($query) or die("Query Failed"); $result2 = mysql_query($query2) or die("Query 2 Failed"); $tickettotal = mysql_num_rows($result2); $results_array = array(); ?> <b>Tickets by source</b><br> <table><tr><td><u># of Tickets</td><td><u>Source</td></tr> <?php while($row = mysql_fetch_assoc($result)) { echo "<tr><td>{$row['IncidentId']}</td><td>{$row['Source']}</td></tr>"; } ?> </table><br><br> <?php echo "Total number of tickets: $tickettotal"; ?> My result is: # of Tickets | Source ------------------------------- 1 | E-Mail 25 | Phone So far so good. Now what I'd like to do is get a percentage next to the numbers. I've already got the ticket total as $tickettotal. So I thought I could do: $percentage = number_format(mysql_num_rows($result) / $tickettotal * 100); The problem is, of course, the number of rows that get returned is 2. How can I have this count on the fly and do the percentage? I'm thinking it needs to pull from the array, but I'm not sure. The final output I'd like would be: # of Tickets | Source ------------------------------- 1 (4%) | E-Mail 25 (96%) | Phone Can anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/88911-solved-counting-results-in-an-array-to-make-a-percentage/ Share on other sites More sharing options...
acook Posted February 1, 2008 Author Share Posted February 1, 2008 I also tried... $results_array = array(); while(mysql_fetch_array($result, $a)) { $results_array[$a[1]] = $a[0]; } ?> <table><tr><td><u># of Tickets</td><td><u>Source</td></tr> <?php foreach ($results_array as $gp => $count) { $total=array_sum($results_array); //total # of tickets $per = number_format($count / $total * 100); echo "<tr><td>$count ($per%)</td><td>$gp</td></tr>"; } ?> But I seem to just get an empty result. Am I doing something wrong here? Quote Link to comment https://forums.phpfreaks.com/topic/88911-solved-counting-results-in-an-array-to-make-a-percentage/#findComment-455433 Share on other sites More sharing options...
rhodesa Posted February 1, 2008 Share Posted February 1, 2008 What about this? <?php while($row = mysql_fetch_assoc($result)){ $per = round(row['IncidentId'] / $tickettotal * 100); echo "<tr><td>{$row['IncidentId']} ({$per}%)</td><td>{$row['Source']}</td></tr>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88911-solved-counting-results-in-an-array-to-make-a-percentage/#findComment-455436 Share on other sites More sharing options...
acook Posted February 1, 2008 Author Share Posted February 1, 2008 Yup, that did it! I didn't know about "round". Thanks a lot!! SOLVED Quote Link to comment https://forums.phpfreaks.com/topic/88911-solved-counting-results-in-an-array-to-make-a-percentage/#findComment-455440 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.