Welcome Guest, Not a member yet? Register   Sign In
display data from the database (summarizes data)
#9

thank you for those who have tried to help me.


I have got what I want.

by using a query like this:
Code:
$this->db->query("
    SELECT income_user, SUM(income_value) AS income_value, GROUP_CONCAT(income_country SEPARATOR ',') AS income_country
    FROM incomes
    GROUP BY income_user
")->result();

results :
Code:
Array
(
   [0] => stdClass Object
       (
           [income_user] => 00001
           [income_value] => 1962.00
           [income_country] => 1US,2US,3US,3US,3US,3US,3US,3US,3US,3US,1US,1PH,1US,1US,1ID,1IN,1IN,1US,1US
       )

   [1] => stdClass Object
       (
           [income_user] => 00002
           [income_value] => 53.00
           [income_country] => 2US,1US,1US,1PH
       )

   [2] => stdClass Object
       (
           [income_user] => 00003
           [income_value] => 150.00
           [income_country] => 1US, 3PH, 2CN, 9TH
       )

)


the question:

how do I sum income_country per-country?


the method I just created is like this:
Code:
<tbody>
   <?php foreach ($data['users_incomes'] as $row): ?>
       <tr>
           <td>
               <?php echo $row->income_user ?>
           </td>
           <td>
           </td>
           <td>
               <?php foreach (explode(',', $row->income_country) as $test): ?>
                   <?php echo $test ?> // HERE
               <?php endforeach ?>
           </td>
           <td>
               <?php echo array_sum(explode(',', $row->income_country)) ?>
           </td>
           <td>
               <?php echo '$ '.$row->income_value ?>
           </td>
       </tr>
   <?php endforeach ?>
</tbody>
Reply


Messages In This Thread
RE: display data from the database - by php_rocs - 04-21-2019, 11:01 AM
RE: display data from the database - by DELE - 04-21-2019, 08:15 PM
RE: display data from the database (summarizes data) - by DELE - 04-22-2019, 05:42 PM



Theme © iAndrew 2016 - Forum software by © MyBB