-1

I have a form which store age range of participants in a program. The area where I need help is, how can I access the age ranges, count and display it? For example:

10 - 19 years - 5 people,
20 - 29 years - 2 people etc

Here is my code for retrieving the list

    <?php
            try{
            // get the age range from the database
            $ageQuery = "SELECT ageRange FROM event WHERE eventUserName = :eventUserName ORDER BY id DESC";
    
            $getAges = $conn->prepare($ageQuery);
            
            $getAges->bindValue(':eventUserName', $eventusername);
            $getAges->execute();
            }
            catch (PDOException $e)
            {
            $error[] = 'Invalid data supplied.';
            } 
        ?>

How can I get the result highlighted above?

halfer
  • 19,824
  • 17
  • 99
  • 186
papadammy
  • 11
  • 4
  • Are you asking how to get the result from a PDO query? There are plenty of tutorials / examples already which show that. What have you researched or tried already? https://stackoverflow.com/questions/10911757/how-to-use-pdo-to-fetch-results-array-in-php should help you at least. Once you've got the results, you can generate the necessary HTML strings with the values included. – ADyson Oct 28 '20 at 11:50
  • I'm confused, you just need to loop through the results? `$results = $getAges->fetchAll()` – GrumpyCrouton Oct 28 '20 at 11:51
  • Go read up on the use of GROUP BY and COUNT in a database beginner tutorial. – CBroe Oct 28 '20 at 11:53
  • rowCount() >= 1) { $row = $getAges->fetch(); $ageDistribution = $row['ageRange']; } $row = array($row); foreach(array_count_value($row) as $key => $val){ echo "$key" . '' . "$val" . '' . "
    "; } I have tried this and it didn't work.
    – papadammy Oct 28 '20 at 12:02
  • $a1=array("first", "second", "third","fourth","fifth","second","third"); $new_array=array_count_values($a1); while (list ($key, $val) = each ($new_array)) { echo "$key -> $val
    "; } This would have worked but each is now deprecated.
    – papadammy Oct 28 '20 at 12:09
  • @papadammy As you can hopefully see for yourself, code placed in the comments is hard to read. Please add it to your question (using the "edit" button just below the blue php & mysql tags) and format it nicely like the rest of the code you first added. Once you've done that, I'll take a proper look at it. Also, rather than saying "didn't work", please describe exactly what goes wrong when you try to run it - e.g. do you get any errors (of course, before you run the code first please ensure error logging is on and PDO is set to throw exceptions!), or any unexpected output? Thanks. – ADyson Oct 28 '20 at 13:32
  • Thank you all respondents. I was able to use fetchAll(PDO::FETCH_ASSOC), array_column and array_count_values to solve my problem. – papadammy Oct 30 '20 at 01:41
  • That's great. If you found a solution please write it below as an Answer for the benefit of everyone. If people find it useful they will upvote it, which increases your reputation score. – ADyson Oct 30 '20 at 07:28

1 Answers1

1
try{
// get the age range from the database
$ageQuery = "SELECT ageRange FROM event WHERE eventUserName = :eventUserName ORDER BY id DESC";
   
$getAges = $conn->prepare($ageQuery);
            
$getAges->bindValue(':eventUserName', $eventusername);
$getAges->execute();
}
catch (PDOException $e)
{
$error[] = 'Invalid data supplied.';
} 
 
if($getAges->rowCount() >= 1) 
{ 
$rowAge = $getAges->fetchAll(PDO::FETCH_ASSOC);
}
if(!empty($rowAge)) $rowAge = array_column($rowAge, 'ageRange');

This area goes into html side of your page.

$rowGender = array_count_values($rowGender);
foreach($rowGender as $key => $val){
echo "$key" . ' <span class="badge">' . "$val" . '</span>' . "<br>"; 
}
}                                                                                    else{
                                                                                        echo '<span class="badge">' . "Not Available!" . '</span>';
}

You should expect result like this:


    Age Distribution<br/>
    20 - 29 Years - 1<br/>
    30 - 39 Years - 2<br/>
    40 - 49 Years - 1<br/>
    The number indicate the frequency of people within the stated age range. Please note that this is PHP7 and some radical changes were made to the version.

papadammy
  • 11
  • 4