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 9116107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:35:31+00:00 2026-06-17T04:35:31+00:00

How to Group two Similar fields in php? I tried with GROUP BY DATE(bill.date)

  • 0

How to Group two Similar fields in php?

I tried with GROUP BY DATE(bill.date) , bill.agent_id but it is not working for me

Table Structure 1: https://i.stack.imgur.com/yvBF0.jpg (table name is bill_agents )
Table Structure 2: https://i.stack.imgur.com/38tKh.jpg (table name is bill )

Current Result

+---------+----+----+----+-----+----+
|         |  1 |  2 |  3 |   4 |  5 |
+---------+----+----+----+-----+----+
| Agent 1 | 35 |  0 |  0 |   0 |  0 |
| Agent 2 |  0 | 10 |  0 |   0 |  0 |
| Agent 1 |  0 |  0 | 12 |   0 |  0 |
| Agent 3 |  0 |  0 |  0 | 100 |  0 |
| Agent 6 |  0 |  0 |  0 |   9 |  0 |
| Agent 2 |  0 |  0 |  0 |   9 | 14 |
+---------+----+----+----+-----+----+

1,2,3,4,5 …. are the days from date

But I want To get Like The Following

+---------+----+----+----+-----+----+----+
|         |  1 |  2 |  3 |   4 |  5 |total
+---------+----+----+----+-----+----+----+
| Agent 1 | 35 |  0 | 12 |   0 |  0 |47  |
| Agent 2 |  0 | 10 |  0 |   0 | 14 |28  |
| Agent 3 |  0 |  0 |  0 | 100 |  0 |100 |
| Agent 6 |  0 |  0 |  0 |   9 |  0 | 9  |
+---------+----+----+----+-----+----+----+

Php Code pasted below that I am using now .

<table width="100%" border="1" cellspacing="4" cellpadding="1">
  <tr>
    <td>&nbsp;</td>
     <?php
for ($i=01; $i<=31; $i++)
  {?>
    <td><?php echo $i; ?></td>
    <?php

  }
?>
    <td>Total</td>
  </tr>
  <?php 

    $query4 = "SELECT bill.agent_id, bill.date, SUM(bill.amount + bill.cheque) AS total, bill_agents.id,bill_agents.name ".
          "FROM bill, bill_agents ".
          "WHERE bill.agent_id = bill_agents.id AND YEAR(date)  = YEAR(CURDATE()) AND MONTH(date) = MONTH(CURDATE()) ". 
          "GROUP BY bill.agent_id , DATE(bill.date)   ".

              // "GROUP BY bill.agent_id , DATE(bill.date)  ".
              "ORDER BY bill.date ASC";

    $result4 = mysql_query($query4) or die('Error, query failed1'); 
    if  (mysql_num_rows($result4)>0){
    mysql_data_seek($result4, 0);   

?>
  <?php $total_1 = 0;  while($row4 = mysql_fetch_array($result4, MYSQL_ASSOC)){?>
  <?php $date =    $row4['date'];

    $var = $date;
    $date = date("d-m-Y", strtotime($var) );
    $date=substr($date, 0, -8); 

    echo $date;

    ?>
  <tr>
    <td><?php echo $row4['name']; ?></td>
    <?php
for ($i=01; $i<=31; $i++)
  {?>
    <td><?php if ($date == $i) echo $row4['total']; ?></td>
    <?php

  }
?>
    <td></td>
  </tr>
  <?php } } ?>
  <tr>
    <td colspan="31"></td>
    <td>Total</td>
    <td></td>
  </tr>
</table>
  • 1 1 Answer
  • 0 Views
  • 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-06-17T04:35:33+00:00Added an answer on June 17, 2026 at 4:35 am

    Because I don’t know if I understood your question here is a short sample which could point you in the right direction:

    $query4 = "SELECT bill.agent_id, "
            . "       bill.date, "
            . "       SUM(bill.amount + bill.cheque) AS total, "
            . "       bill_agents.id, "
            . "       bill_agents.name "
            . "FROM bill, bill_agents "
            . "WHERE bill.agent_id = bill_agents.id "
            . "AND YEAR(date) = YEAR(CURDATE()) "
            . "AND MONTH(date) = MONTH(CURDATE()) "
            . "GROUP BY bill.agent_id, DATE(bill.date)   "
            . "ORDER BY bill.date ASC";
    
    $result4 = mysql_query($query4) or die('Error, query failed1');
    if (mysql_num_rows($result4) > 0) {
        mysql_data_seek($result4, 0);
    
        $agents = array();
        while (true == ($row4 = mysql_fetch_assoc($result4))) {
            $agents[$row4['agent_id']][$row4['date']] = $row4;
        } 
    
        var_dump($agents);
    }
    

    Take a look at the output and see if you can work further with that $agents variable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two similar queries on same table and same where condition but different
I have two models, user and group. I also have a joining table groups_users.
I have the following two table: -Groups- Id Name -Members- Id GroupId (Group.Id is
My question is similar to SQL select Group query . But there is change
In sunspot solr we could group records with similar attributes via facets. But is
In Jira (4.0), under Global Permissions, I have the Jira Users group containing two
I have a radio group defined with two buttons for black and color. When
Let's say I have two entities: Group and User. Every user can be member
Possible Duplicate: Retrieving the last record in each group I have two tables set
i have these two models public class Group { [Key] public int GroupID {

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.