Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1070303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:32:30+00:00 2026-05-16T20:32:30+00:00

Hey everyone. While I’m trying to learn some PHP and mySQL, I ran into

  • 0

Hey everyone. While I’m trying to learn some PHP and mySQL, I ran into a problem that I’ve had some difficulty in solving. I need a PHP script that queries mySQL database for country, books, book_price. Here is my dbtable:

+----+-------+----------+------------------------+
| id |Country|Books     | Book_price             |
+----+-------+----------+------------------------+ 
|  1 | USA  | Zorro     |   10 |
|  2 | USA  | Zorro     |   20 |
|  3 | USA  | Zorro     |   50 |
|  4 | USA  | Leon      |  200 |
|  5 | USA  | Leon      |  240 |
|  6 | ITALY| Tarzan    |   70 |
|  7 | ITALY| Tarzan    |   30 |
|  8 | ITALY| Tarzan    |  100 |
|  9 | ITALY| Cobra     |  300 |
| 10 | ITALY| Cobra     |  320 |
| 11 | ITALY| Cobra     |  350 |
+----+------+-----------+------------------------+

I want to organize the results based upon the country, books, total book, total country and TOTAL GEN (which is sum of all total country) and the result to show like this:

+----------------------------------------------------+
| USA                                                |
+----------------------------------------------------+ 
|     Zorro    10  |
|              20  |
|              50  |
+----------------------------------------------------+
| Total Zorro:  80   |
+----------------------------------------------------+
|      Leon  200  |
|            240  |
+----------------------------------------------------+
| Total Leon:440   |
+----------------------------------------------------+
|Total USA:       520   |
+----------------------------------------------------+
|ITALY     |
+----------------------------------------------------+ 
|     Tarzan      70  |
|                 30  |
|                100  |
+----------------------------------------------------+
| Total Tarzan:200  |
+----------------------------------------------------+
|         Cobra  300  |
|                320  |
|                350  |
+----------------------------------------------------+
| Total Cobra: 970  |
+----------------------------------------------------+
|Total ITALY:       1170  |
+----------------------------------------------------+
|TOTAL GEN:       1690  |
+----------------------------------------------------+

Thank you

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:32:31+00:00Added an answer on May 16, 2026 at 8:32 pm
    $conn = mysql_connect("localhost", "mysql_user", "mysql_password");
    mysql_select_db("mydbname");
    
    $sql    = 'SELECT * FROM table_name';
    $result = mysql_query($sql);
    
    $data = array();
    while ($row = mysql_fetch_assoc($result)) {
        if ( empty($data[ $row['Country'] ]) ) {
            $data[ $row['Country'] ] = array();
        }
    
        if ( empty( $data[ $row['Country'] ][ $row['Books'] ] ) ) {
            $data[ $row['Country'] ][ $row['Books'] ] = array();
        }
    
        $data[ $row['Country'] ][ $row['Books'] ][] = $row['Book_price'];
    }
    
    
    $totalSum = 0;
    foreach ( $data as $country => $books ) {
    
        echo '<b>' . $country . '</b><br/>';
    
        $totalCountry = 0;
        foreach ( $books as $book => $prices ) {
            $sum = array_sum( $prices );
    
            echo '<u>' . $book . '</u><br/>';
    
            echo implode(',', $prices) . '<br/>;
    
            echo 'Total ' . $book . ':' . $sum . '<br/>';
    
            $totalCountry += $sum;
        }
    
    
        echo 'Total ' . $country . ':' . $totalCountry . '<br/>';
    
        echo '<hr/>';
    
        $totalSum += $totalCountry;
    
    }
    
    echo 'TOTAL GEN: ' . $totalSum;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey everyone! I'm building a webapp in PHP that uses a MySql DB. What
Hey everyone I am having a little problem with my jQuery I am trying
Hey everyone, I am trying to ( temporarily! ) do some ASP.NET and C#
Hey everyone. I am trying to incorporate a simple UINavigationController into my tab bar
Hey everyone, I'm working on a PHP application that needs to parse a .tpl
Hey there, everyone. A really random question, but I'm looking to get into some
Hey everyone, I'm working on a widget for Apple's Dashboard and I've run into
Hey everyone. The following is the code that I am using to play random
Hey everyone, great community you got here. I'm an Electrical Engineer doing some programming
hey everyone, here is the site SEE BELOW I have a slight jquery problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.