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: http://i.stack.imgur.com/yvBF0.jpg (table name is bill_agents )
Table Structure 2: http://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 |
+---------+----+----+----+-----+----+
But I want To get Like The Following
+---------+----+----+----+-----+----+
| | 1 | 2 | 3 | 4 | 5 |
+---------+----+----+----+-----+----+
| Agent 1 | 35 | 0 | 12 | 0 | 0 |
| Agent 2 | 0 | 10 | 0 | 0 | 14 |
| Agent 3 | 0 | 0 | 0 | 100 | 0 |
| Agent 6 | 0 | 0 | 0 | 9 | 0 |
+---------+----+----+----+-----+----+
Php Code pasted below that I am using now .
<table width="100%" border="1" cellspacing="4" cellpadding="1">
<tr>
<td> </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>
My understanding is, you need total amount processed by a agent and in a particular month of year.
Then change your query as follows,
<?php
$query = "SELECT b.date,b.agent_id,(SUM(b.amount) + SUM(b.cheque)) AS total,ba.id,ba.name
FROM bill_agent ba LEFT JOIN bill b ON b.agent_id = ba.id
WHERE YEAR(b.date) = YEAR(CURDATE()) AND MONTH(b.date) = MONTH(CURDATE())
GROUP BY b.date";
?>
Related
Basically I want to do a search by category and title. My problem is that the two are located in separate tables. I was thinking of putting 2 variables in the mysqli_num_rows or mysqli_fetch_array but I don't think it's the right idea. The $search variable is working already but I don't know what I will do for $searchcat that is a different table.
<table border="1">
<tr>
<th>ID</th>
<th>Survey Title</th>
<th>Category</th>
</tr>
<?php
if (isset($_POST['submit']))
{
include 'testdb.php'; //connection is written in other page (db.php)
$var =$_POST['search'] ;
$searchtype = $_POST['searchtype'];
$my_query="SELECT s.survey_id, s.title,c.categoryname
FROM survey_header as sh
JOIN survey AS s ON sh.survey_id=s.survey_id
JOIN category AS c ON sh.category_id=c.category_id
WHERE $searchtype LIKE '%".$var."%'
ORDER BY title ASC";
$get_data= mysqli_query($con, $my_query) or die ("Couldn't execute query: ".mysqli_error());
if (mysqli_num_rows($get_data) > 0 )
{
echo "<h3>Search results:</h3>";
while($show = mysqli_fetch_array($get_data))
{
$id = $show['survey_id'];
$title = $show['title'];
$category = $show['categoryname']; //
echo "<tr align='center'>";
echo "<td><font color='black'>" .$id. "</font></td>";
echo "<td><font color='black'>" .$title. "</font></td>";
echo "<td><font color='black'>" .$category. "</font></td>";
}
}
else{
echo "No Records found!";
}
}
?>
</table>
</body>
This is table category (categoryname is what I need)
+-------------+---------------+-------------+
| category_id | categoryname | datecreated |
| 1 | Philosophical | |
| 4 | Political | |
| 6 | Social | |
This is table survey (title is all I need)
| 1 | survey_id | title | description | duration | gender | age_group_from | age_group_to |
| 2 | 44 | game1 | description1 | 5 | male | 0 | 18 |
| 3 | 45 | game2 | description2 | 25 | female | 18 | 25 |
| 4 | 46 | game3 | description3 | 89 | female | 26 | 35 |
This is table survey_header (survey_id and category_id is what I need)
| 1 | survey_id | date_created | date_updated | opening_date | closing_date | category_id | topic_id |
| 2 | 33 | Not important | Not important | NULL | NULL | 1 | NULL |
| 3 | 45 | Not important | Not important | NULL | NULL | 6 | NULL |
| 4 | 46 | Not important | Not important | NULL | NULL | 4 | NULL |
Try this query :
$my_query="SELECT s.survey_id, s.title,c.categoryname
FROM survey_header as sh
JOIN survey AS s ON sh.survey_id=s.survey_id
JOIN category AS c ON sh.category_id=c.category_id
WHERE $searchtype LIKE '%".$var."%'
ORDER BY title ASC";
$get_data= mysqli_query($con, $my_query) or die ("Couldn't execute query: ".mysqli_error());
if (mysqli_num_rows($get_data) > 0 )
{
/*create table*/
}
else
// do something else
I want the queqe id auto increase start from 1
I have an mysql table call t1
mysql table t1 Data as below:
+----------+------------------+-------------+
| ID | Name | Status |
+----------+------------------+-------------+
| 1 | ABBCCC | 1 |
| 2 | BASDASD | 1 |
| 3 | ABBCCC | 1 |
| 4 | ABBCCC | 2 |
+-------------------------------------------+
I loop data in php like this:
$quserCA = DB::query("SELECT * FROM ".DB::table('jnbook_book')." WHERE Name = 'ABBCCC' ORDER BY id DESC LIMIT 20");
$nqCA = mysql_num_rows($quserCA);
while($ruserCA = DB::fetch($quserCA)){
$CAlist[] = $ruserCA;
}
$x = 1;
while($x <= $nqCA) {
//echo "The number is: $x <br>";
$x++;
}
I loop this in my htm like this:
<table>
<tr>
<td>Queqe ID</td><td>ID</td><td>Status</td>
</tr>
<!--{loop $CAlist $value}-->
<tr>
<td>{$x}</td><td>{$value[id]}</td><td>{$value[status]}</td>
</tr>
<!--{/loop}-->
</table>
But after that my table output as below show
+---------------+-------------------+----------------+
| Queqe ID | ID | Status |
+---------------+-------------------+----------------+
| 1 | 1 | 1 |
| 1 | 3 | 1 |
| 1 | 4 | 2 |
+----------------------------------------------------+
Actually what I want the table output as below
(I want the queqe id auto increase start from 1):
+----------+-----------------+-----------------+
| Queqe ID | ID | Status |
+----------+-----------------+-----------------+
| 1 | 1 | 1 |
| 2 | 3 | 1 |
| 3 | 4 | 2 |
+----------------------------------------------+
Thank you.
This should be done something like:
$x = 1;
while($ruserCA = DB::fetch($quserCA)){
// add a field, say `x` with number of a record:
$ruserCA['x'] = $x++;
$CAlist[] = $ruserCA;
}
In a template:
<td>{$value[x]}</td><td>{$value[id]}</td><td>{$value[status]}</td>
My database design : http://i.stack.imgur.com/3Rm2e.png
I am trying to use below code :
$myselect = mysql_query("
SELECT
reason.ReasonName,
tblexam.EXAMDATE,
tblexam.EXAMTIME,
prefix.PREFIXABB,
studentmaster.STUDENTNAME,
studentmaster.STUDENTSURNAME,
studentmaster.STUDENTCODE,
program.PROGRAMNAME,
course.COURSENAMEENG
FROM
reason,
formlate,
tblexam,
studentmaster,
prefix,
program,
course
WHERE
reason.ReasonID = formlate.ReasonID AND
formlate.CLASSID = tblexam.CLASSID AND
formlate.COURSEID = formlate.COURSEID AND
formlate.ROOMID = formlate.ROOMID AND
prefix.PREFIXID = studentmaster.PREFIXID AND
formlate.STUDENTID = studentmaster.STUDENTID AND
program.PROGRAMID = studentmaster.PROGRAMID AND
course.COURSEID = tblexam.COURSEID
GROUP BY
reason.ReasonID,studentmaster.STUDENTID
ORDER BY
reason.ReasonID ASC");
<body>
<?php $i =1;
while($rowfet = mysql_fetch_array($myselect)){
?>
<h2><?php echo $rowfet['ReasonName']; ?></h2>
<h3><?php echo DateThai($rowfet['EXAMDATE']) . ' ' . MorningEvening($rowfet['EXAMTIME']); ?></h3>
<table width="100%" border="1">
<thead>
<tr>
<th>No.</th>
<th>studentid </th>
<th>Fullname </th>
<th>Program </th>
<th>Coursename </th>
</tr>
</thead>
<tbody>
<tr>
<td align="center"><?php echo $i; ?></td>
<td><?php echo $rowfet['STUDENTCODE']; ?></td>
<td><?php echo $rowfet['PREFIXABB'] . $rowfet['STUDENTNAME'] . ' '.$rowfet['STUDENTSURNAME']; ?></td>
<td><?php echo $rowfet['PROGRAMNAME']; ?></td>
<td><?php echo $rowfet['COURSENAMEENG']; ?></td>
</tr>
</tbody>
</table>
<?php $i++; } ?>
but it shows the result like :
Reason1
10 Jan 2015 (Morning)
| No | studentid | Fullname | Program | Coursename |
-------------------------------------------------------
| 1 | 111111 | John | ProgA | Math |
---------------------------------------------------------
Reason1
10 Jan 2015 (Morning)
| No | studentid | Fullname | Program | Coursename |
-------------------------------------------------------
| 2 | 2222222 | Jane | ProgB | Math |
-----------------------------------
Reason2
10 Jan 2015 (Evening)
| No | studentid | Fullname | Program | Coursename |
-------------------------------------------------------
| 3 | 3333333 | Hawk | ProgB | Math |
-----------------------------------
I want the result to show like :
Reason1
10 Jan 2015 (Morning)
| No | studentid | Fullname | Program | Coursename |
-------------------------------------------------------
| 1 | 111111 | John | ProgA | Math |
---------------------------------------------------------
| 2 | 2222222 | Jane | ProgB | Math |
-----------------------------------
Reason2
10 Jan 2015 (Evening)
| No | studentid | Fullname | Program | Coursename |
-------------------------------------------------------
| 1 | 3333333 | Hawk | ProgB | Math |
---------------------------------------------------------
I want to group table by
Reasonname
Examdate
Examtime
Please help, I'm a newbie.
I've create database in SQLFiddle Here this please try and help:
SQLFiddle
considered that you have a sql result that looks like this, ordered by reason, date and time :
reason.reasonName | examDate | examTime | prefix | studentName | studentSurname | studentCode | ProgramName | courseName
with a few records, like :
1 | someDate1 | someTime1 | A | James | Jim | 123 | PA | PACourse
1 | someDate1 | someTime1 | A | Tommy | Tim | 222 | PC | PCCourse
1 | someDate2 | someTime2 | B | James | Jim | 123 | PB | PBCourse
2 | someDate1 | someTime1 | C | Jamie | Jam | 444 | PD | PDCourse
2 | someDate1 | someTime1 | E | Willy | Wil | 555 | PE | PECourse
2 | someDate2 | someTime2 | F | Cathy | Cat | 777 | PF | PFCourse
2 | someDate2 | someTime2 | G | David | Dav | 844 | PL | PLCourse
2 | someDate3 | someTime3 | L | James | Jim | 123 | PM | PMCourse
3 | someDate4 | someTime4 | W | Ibear | Ibe | 999 | PX | PXCourse
Here, you get this kind of data, you have,
for reason 1 :
2 students with exam at the same day/time (time1)
1 student with axam at another time (time2)
for reason 2 :
2 students with exam at the same day/time (time1)
2 students with exam at the same day/time (time2)
1 student with exam at another time (time3)
for reason 3 :
1 student with exam at time 4
SO, you want to display :
2 tables for reason 1 (time1 / time2)
3 tables for reason 2 (time1 / time2 / time3)
1 table for reason 3
CONSIDERED that the table, when you get it from your SQL is REALLY oredered like what I wrote above, you have to make your checks on fetch :
<?php
//YOU WILL HAVE TO CHECK THESE VARS TO MAKE IT CLEAR, IF SAME TABLE OR NOT
$i = false;
//I use $i as a table detector, false at start, once a table has been opened it is turned as true
$reason = "";
//I will keep the latest reasonName in this
$examDate = "";
//I will keep the latest examDate in this
$examTime = "";
//I will keep the latest examTime in this
$yourDisplay = "";
//In yourDisplay, i keep the text i have to display (tables, data...)
$closeTable = "</tbody></table>";
//This is the code you need to close a table.
while($rowfet = mysql_fetch_array($myselect)){
if(($rowfet['ReasonName']!=$reason)||(DateThai($rowfet['EXAMDATE'])!=$examDate)||(MorningEvening($rowfet['EXAMTIME'])!=$examTime)){
//YOU GET HERE IF YOU HAVE DIFFERENT REASON/DATE/TIME, SO YOU HAVE TO MAKE A NEW TABLE
$reason = $rowfet['ReasonName'];
$examDate = DateThai($rowfet['EXAMDATE']);
$examTime = MorningEvening($rowfet['EXAMTIME']);
//NOW CHECK IF YOU ALREADY HAVE WRITTEN A TABLE BEFORE
if($i == true){
//You already have written a table before
$yourDisplay.=$closeTable;
}else{
//This is the first table you write
$i=true;
}
$yourDisplay.=" <h2>".$reason."</h2>
<h3>".DateThai($examDate)." ".MorningEvening($examTime)."</h3>
<table width='100%' border='1'>
<thead>
<tr>
<th>No.</th>
<th>studentid</th>
<th>fullname</th>
<th>Program</th>
<th>Coursename</th>
</tr>
</thead>
<tbody>
<tr>
<td>".$rowfet['STUDENTCODE']."</td>
<td>".$rowfet['PREFIXABB']." ".$rowfet['STUDENTNAME']." ".$rowfet['STUDENTSURNAME']."</td>
<td>".$rowfet['PROGRAMNAME']."</td>
<td>".$rowfet['COURSENAMEENG']."</td>
</tr>";
}else{
//YOU ARE ALREADY IN A TABLE! WITH SAME REASON/TIME/DATE
//YOUR RECORD JUST NEEDS TO BE ADDED IN A NEW LINE
$yourDisplay.="<tr>
<td>".$rowfet['STUDENTCODE']."</td>
<td>".$rowfet['PREFIXABB']." ".$rowfet['STUDENTNAME']." ".$rowfet['STUDENTSURNAME']."</td>
<td>".$rowfet['PROGRAMNAME']."</td>
<td>".$rowfet['COURSENAMEENG']."</td>
</tr>";
}
}
//ONCE OUT, CLOSE THE LAST TABLE AND DISPLAY YOUR DATA
if($i==1){
$yourDisplay.=$closeTable;
}
echo $yourDisplay;
?>
In My Opinion, this kind of approach should solve your problem IF THE SQL QUERY returns the rows as explained above. (seems like it is the case)
EDIT :
You asked to "not display" the reasonname if it was the same, so just look at the line you see this :
$yourDisplay.=" <h2>".$reason."</h2>
<h3>".DateThai($examDate)." ................ code continue
and replace it with this :
if($rowfet['ReasonName']!=$reason){
$yourDisplay.="<h2>".$reason."</h2>";
}
$yourDisplay.="<h3>".DateThai($examDate)." ".MorningEvening($examTime)."</h3>
<table width='100%' border='1'>
//.... continue the code
You're looping through all your rows and creating a table for each of the rows.
What you want to do is to create a new table only if a sequence of a new ReasonName starts.
$myselect = mysql_query("SELECT reason.ReasonName,tblexam.EXAMDATE,tblexam.EXAMTIME,prefix.PREFIXABB,studentmaster.STUDENTNAME,studentmaster.STUDENTSURNAME,studentmaster.STUDENTCODE,program.PROGRAMNAME,course.COURSENAMEENG FROM reason, formlate, tblexam, studentmaster,prefix, program, course WHERE reason.ReasonID = formlate.ReasonID AND formlate.CLASSID = tblexam.CLASSID AND formlate.COURSEID = formlate.COURSEID AND formlate.ROOMID = formlate.ROOMID AND prefix.PREFIXID = studentmaster.PREFIXID AND formlate.STUDENTID = studentmaster.STUDENTID AND program.PROGRAMID = studentmaster.PROGRAMID AND course.COURSEID = tblexam.COURSEID GROUP BY reason.ReasonID,studentmaster.STUDENTID ORDER BY reason.ReasonID ASC");
<body>
<?php $i =1;
while($rowfet = mysql_fetch_array($myselect)){
?>
<h2><?php echo $rowfet['ReasonName']; ?></h2>
<h3><?php echo DateThai($rowfet['EXAMDATE']) . ' ' . MorningEvening($rowfet['EXAMTIME']); ?></h3>
<table width="100%" border="1">
<thead>
<tr>
<th>No.</th>
<th>studentid </th>
<th>Fullname </th>
<th>Program </th>
<th>Coursename </th>
</tr>
</thead>
<tbody>
<?php echo "You should create a loop here to loop through your ReasonNames"; ?>
<tr>
<td align="center"><?php echo $i; ?></td>
<td><?php echo $rowfet['STUDENTCODE']; ?></td>
<td><?php echo $rowfet['PREFIXABB'] . $rowfet['STUDENTNAME'] . ' '.$rowfet['STUDENTSURNAME']; ?></td>
<td><?php echo $rowfet['PROGRAMNAME']; ?></td>
<td><?php echo $rowfet['COURSENAMEENG']; ?></td>
</tr>
<?php echo "Your loop should end here"; ?>
</tbody>
</table>
<?php $i++; } ?>
I may not use the proper subject of the problem. But here's the detail. I've got 3 tables of data 2 of them are set name and group name. The rest is data - user db. Here's the db.
set_name
+--------+-----------+
| set_id | set_title |
+--------+-----------+
| 1 | Set A |
+--------+-----------+
| 2 | Set B |
+--------+-----------+
group_db
+--------+-----------+--------+
| grp_id | grp_title | set_id |
+--------+-----------+--------+
| 1 | Grp. A | 1 |
+--------+-----------+--------+
| 2 | Grp. B | 1 |
+--------+-----------+--------+
| 3 | Grp. C | 1 |
+--------+-----------+--------+
| 4 | Grp. D | 1 |
+--------+-----------+--------+
| 5 | Grp. E | 1 |
+--------+-----------+--------+
| 6 | Grp. F | 2 |
+--------+-----------+--------+
user_db
+--------+-----------+
| usr_id | grp_id |
+--------+-----------+
| 1 | 1 |
+--------+-----------+
| 2 | 1 |
+--------+-----------+
| 3 | 2 |
+--------+-----------+
| 4 | 1 |
+--------+-----------+
| 5 | 3 |
+--------+-----------+
| 6 | 4 |
+--------+-----------+
| 7 | 5 |
+--------+-----------+
| 8 | 5 |
+--------+-----------+
| 9 | 5 |
+--------+-----------+
| 10 | 6 |
+--------+-----------+
According to the information provided above. I expect a summary table in which count all user and categorize by group and set. For example:
+-----+--------------------------------------------+--------+
| SET | Set A. | Set B. |
+-----+--------------------------------------------+--------+
|GROUP| Grp. A | Grp. B | Grp. C | Grp. D | Grp. E | Grp. F |
+-----+--------------------------------------------+--------+
| NUM | 3 | 1 | 1 | 1 | 3 | 1 |
+-----+--------------------------------------------+--------+
|TOTAL| 9 | 1 |
+-----+--------------------------------------------+--------+
And this is how I do.
<table>
<tr>
<?
$sql_set=mysqli_query($con,"SELECT *,count(group_db.grp_id) AS nGrp\n"
. "FROM set_name\n"
. "INNER JOIN group_db ON set_name.set_id=group_db.set_id\n"
. "GROUP BY set_name.set_id\n"
. "ORDER BY set_name.set_id asc");
echo "<td>SET</td>";
while($rec_set=mysqli_fetch_array($sql_set)){
echo "<td colspan=\"$rec_set[nGrp]\">$rec_set[set_title]</td>";
}
?>
</tr>
<tr>
<?
$sql_sGrp=mysqli_query($con,"SELECT * from group_db\n"
. "WHERE set_id='$rec_set[set_id]'\n"
. "ORDER BY grp_title asc");
echo "<td>GROUP</td>";
while($rec_sGrp=mysqli_fetch_array($sql_sGrp)){
echo "<td>$rec_sGrp[grp_title]</td>";
}
?>
</tr>
</table>
That's it. I don't know how to go further. Please be advice.
Ps. Should I make them all in multilevel array to make it easier?
I would do something like:
SELECT
*
FROM
user_db u
JOIN
group_db g ON u.grp_id = g.grp_id
JOIN
set_name s ON g.set_id = s.set_id
(EDIT: changed qry to this ^ which can be seen here: http://sqlfiddle.com/#!2/e749f/4)
And then in PHP:
$newArray = array();
while($rec_set=mysqli_fetch_array($sql_set)){
$newArray[$rec_set['set_title']][$rec_set['grp_title']] += 1;
}
which should give you a nice multidimensional array of the results that you can parse through however you want
And to give a table that looks like:
+-----+--------------------------------------------+--------+
| SET | Set A. | Set B. |
+-----+--------------------------------------------+--------+
|GROUP| Grp. A | Grp. B | Grp. C | Grp. D | Grp. E | Grp. F |
+-----+--------------------------------------------+--------+
| NUM | 3 | 1 | 1 | 1 | 3 | 1 |
+-----+--------------------------------------------+--------+
|TOTAL| 9 | 1 |
+-----+--------------------------------------------+--------+
I would use:
<tr>
<td>SET</td>
<?php foreach($newArray as $set => $group): ?>
<td colspan="<?=count($newArray[$set])?>"><?=$set?></td>
<?php endforeach; ?>
</tr>
<tr>
<td>GROUP</td>
<?php foreach($newArray as $set => $group): ?>
<?php foreach($group as $group_name => $amount): ?>
<td><?=$group_name?></td>
<?php endforeach; ?>
<?php endforeach; ?>
</tr>
<tr>
<td>NUMBER</td>
<?php foreach($newArray as $set => $group): ?>
<?php foreach($group as $group_name => $amount): ?>
<td><?=$amount?></td>
<?php $totals[$set] += $amount;?>
<?php endforeach; ?>
<?php endforeach; ?>
</tr>
<tr>
<td>TOTAL</td>
<?php foreach($newArray as $set => $group): ?>
<td colspan="<?=count($newArray[$set])?>"><?=$totals[$set]?></td>
<?php endforeach; ?>
</tr>
However, now that I look at how you would actually display it, if you really wanted a table that looked like you put, then a multidimensional array would probably not be the best way to loop through your data since all these loops are UGLY! (And it does not scale too well horizontally as you add more and more sets and groups). I did not check it for accuracy.
echo '<table>';
$rows = array('SET', 'GROUP', 'NUM', 'TOTAL');
$setids = array();
$grp_usercounts = array();
$set_usertotals = array();
foreach($rows as $key => $row){
echo "<tr> $rows </td>";
switch ($key){
case 0: //SET
$sql = "SELECT s.set_id, set_title, count(g.grp_id) nGrp
FROM set_name s
JOIN group_db g ON s.set_id = g.set_id
group by set_id";
$sql_set = mysqli_query($con, $sql);
while($rec_set=mysqli_fetch_array($sql_set)){
echo '<td colspan="'.$rec_set['nGrp'].'">'. rec_set['set_title'].'</td>';
$setids[$rec_set['set_id']] = $rec_set['nGrp'];
}
break;
case 1://GROUP
foreach($setids as $setid => $val){
$sql = "SELECT g.grp_id, grp_title, count(usr_id) nUsr
FROM group_db g
JOIN user_db u ON u.grp_id = g.grp_id
where set_id = $setid
group by g.grp_id order by grp_title";
$sql_set = mysqli_query($con, $sql);
$total = 0;
while($rec_set=mysqli_fetch_array($sql_set)){
echo '<td>'. $rec_set['grp_title'].'</td>';
$grp_usercounts[$rec_set['grp_id']] = $rec_set['nUsr'];
$total += $rec_set['nUsr'];
}
$set_usertotals[$setid] = $total;
}
break;
case 2://NUM
foreach($grp_usercounts as $key => $grp_usercount){
echo '<td>'. $grp_usercount .'</td>';
}
break;
case 3: //TOTAL
foreach($set_usertotals as $setid => $set_usertotal){
echo '<td colspan="'.$setids[$setid].'">'. $set_usertotal .'</td>';
}
break;
}
}
unset($setids);
unset($grp_usercounts);
unset($set_usertotals);
echo '</table>';
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: http://i.stack.imgur.com/yvBF0.jpg (table name is bill_agents )
Table Structure 2: http://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> </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>
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.