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

  • Home
  • SEARCH
  • 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 9135785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:52:06+00:00 2026-06-17T08:52:06+00:00

Table Structure bill CREATE TABLE IF NOT EXISTS `bill` ( `id` int(11) NOT NULL

  • 0

Table Structure bill

CREATE TABLE IF NOT EXISTS `bill` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `agent_id` int(11) NOT NULL,
  `date` date NOT NULL,
  `amount` int(100) NOT NULL,
  `cheque` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ;

--
-- Dumping data for table `bill`
--

INSERT INTO `bill` (`id`, `agent_id`, `date`, `amount`, `cheque`) VALUES
(1, 1, '2013-01-01', 10, 15),
(2, 1, '2013-01-01', 10, 0),
(3, 2, '2013-01-02', 10, 0),
(4, 1, '2013-01-03', 12, 0),
(5, 3, '2013-01-04', 100, 0),
(6, 2, '2013-01-05', 14, 0),
(7, 5, '2013-01-05', 18, 0),
(8, 7, '2013-01-05', 22, 0),
(9, 6, '2013-01-04', 19, 0),
(10, 6, '2013-01-06', 20, 0),
(11, 5, '2013-01-07', 21, 0),
(12, 6, '2013-01-08', 11, 0),
(13, 7, '2013-02-09', 19, 0),
(14, 6, '2013-01-10', 150, 0),
(15, 1, '2012-12-31', 100, 0),
(16, 1, '2012-01-11', 500, 20),
(17, 1, '2013-02-01', 210, 0),
(18, 1, '2013-02-18', 100, 0),
(19, 6, '2013-01-31', 100, 0),
(20, 3, '2013-01-11', 250, 50);

Table Structure bill_agents

CREATE TABLE IF NOT EXISTS `bill_agents` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(11) NOT NULL,
  `target` int(11) NOT NULL,
  `join_date` date NOT NULL,
  `resign_date` date NOT NULL,
  `hide` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

--
-- Dumping data for table `bill_agents`
--

INSERT INTO `bill_agents` (`id`, `name`, `target`, `join_date`, `resign_date`, `hide`) VALUES
(1, 'Agent 1', 10, '2013-01-01', '0000-00-00', 0),
(2, 'Agent 2', 5000, '2013-01-01', '0000-00-00', 0),
(3, 'Agent 3', 5000, '2013-01-01', '0000-00-00', 0),
(4, 'Agent 4', 5000, '2013-01-03', '2013-01-06', 0),
(5, 'Agent 5', 5000, '2013-01-01', '0000-00-00', 0),
(6, 'Agent 6', 5000, '2013-01-02', '0000-00-00', 0),
(7, 'Agent 7', 5000, '2013-01-02', '0000-00-00', 1);

My Current result: current result

( 1-31 are the dates) with following code

<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 DISTINCT 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);   

?>
  <?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>

What I am trying to create is i don’t want to duplicate agents name if more than one entry in database it should come under the current date only and grand total also should be . how is is it possible sql query ? or do i need to create with other code?

Here is an example what i am trying to create expected result

  • 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-17T08:52:07+00:00Added an answer on June 17, 2026 at 8:52 am
    $select_agent_id = mysql_query("select agent_id from bill group by agent_id");
    while ($fetch = mysql_fetch_array($select_agent_id)) {
        $agent_id[] = $fetch['agent_id'];
    }
    $number_of_agents = count($agent_id);
    
        $output = "<table border='2px' bordercolor='#460000'>";
        $output .="<tr>";
        $output .="<td>Name</td>";
        for($i=1; $i<=31; $i++) {
            $output .="<td>".$i."</td>";
        }
        $output .="<td>Total</td>"; 
        $output .="</tr>";
    
    foreach($agent_id as $id) {
    
        $select_records = mysql_query("select *, sum(amount) from bill where agent_id = $id 
                          and date between '2013-01-01' and '2013-01-31' group by date");
    
        $select_agent_name = mysql_query("select name from bill_agents where id=$id");
        $fetch_name = mysql_fetch_array($select_agent_name);
        $agent_name = $fetch_name['name'];
    
    
        $rows_count = mysql_num_rows($select_records);
        while($fetch_dates = mysql_fetch_array($select_records)) {
            $dates[] =substr($fetch_dates['date'],-2,2);
            $amounts[] = $fetch_dates['sum(amount)'];
        }
        $output .= "<tr>";
        $output .="<td>".$agent_name."</td>";
    
        for($i=1;$i<=31;$i++) {
    
            if(!empty($dates)) {
    
                if($i==$dates[0]) {
                    $output .="<td>".$amounts[0]."</td>";
                    array_shift($amounts);
                    array_shift($dates);
                } else {
                    $output .= "<td>&nbsp;</td>";
                }
            } else {
                $output .= "<td>&nbsp;</td>";
            }
    
        }
        $total_amount_query = mysql_query("select sum(amount) from bill where agent_id = $id 
                          and date between '2013-01-01' and '2013-01-31' group by agent_id");
        $fetch_total_amount = mysql_fetch_array($total_amount_query);
        $output .="<td>".$fetch_total_amount['sum(amount)']."</td>";                  
        $output .= "</tr>";
    
    }
    $output .="</table>";
    echo $output;
    

    @Preethu Alex : Above is you code. Please check and let me know. Hope this helps. I have not done any checking for SQL INJECTION. I have also given some dates as static in the query. Please modify it accodingly

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

Sidebar

Related Questions

Table Structure CREATE TABLE IF NOT EXISTS `blogs` ( `id` int(11) NOT NULL auto_increment,
My table structure is below: CREATE TABLE [ACC].[Document]( [DocumentID] [int] IDENTITY(1,1) NOT NULL, [Date]
My table structure is: TABLE `licenses` ( `id` int(11) NOT NULL auto_increment, `code` varchar(30)
Simplified Table structure: CREATE TABLE IF NOT EXISTS `hpa` ( `id` bigint(15) NOT NULL
Table structure: CREATE TABLE IF NOT EXISTS `logs` ( `id` bigint(20) unsigned NOT NULL
My table structure is: CREATE TABLE IF NOT EXISTS `users_settings_temp` ( `id` int(10) unsigned
My table structure: CREATE TABLE `userimageview` ( `user_id` int(11) unsigned NOT NULL, `image_id` int(11)
My Table Structure as follow, CREATE TABLE tbl_Info ( [SSEID] BIGINT NOT NULL IDENTITY(1,1),
Table structure: CREATE TABLE `mytable` ( `id` varchar(8) NOT NULL, `event` varchar(32) NOT NULL,
Existing table structure CREATE TABLE [MYTABLE]( [ROW1] [numeric](18, 0) NOT NULL, [ROW2] [numeric](18, 0)

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.