MySql/PHP Simple Report Query - php

I'm running a MySQL/PHP and i'm trying to display a simple report that tracks when a salesrep contacts a customer. I can't figure out what I'm doing wrong, as i'm a novice in this area. The simplest solution to me seems extremely convoluted (making a separate recordset for each figure). I figured there would have to be a simpler way.
I'm looking to display the number of contacts made during the current week/month/year in a simple table. see below. any help would be greatly appreciated.
|Current|Current| |
| Week | Month | YTD |
------|-------|-------|-------|
Brian | 7 | 14 | 37 |
------|-------|-------|-------|
Chad | 0 | 15 | 27 |
------|-------|-------|-------|
David | 11 | 26 | 52 |
------|-------|-------|-------|
Current recordsets
mysql_select_db($database_Sales, $Sales);
$query_rsCurWeek = "SELECT Sales.rep, COUNT(*) FROM Sales WHERE YEARWEEK(`date`, 1) = YEARWEEK(CURDATE(), 1) GROUP BY Sales.rep";
$rsCurWeek = mysql_query($query_rsCurWeek, $Sales) or die(mysql_error());
$row_rsCurWeek = mysql_fetch_assoc($rsCurWeek);
$totalRows_rsCurWeek = mysql_num_rows($rsCurWeek);
mysql_select_db($database_Sales, $Sales);
$query_rsCurMonth = "SELECT Sales.rep, COUNT(*) FROM Sales WHERE MONTH(date) = MONTH(CURDATE()) GROUP BY Sales.rep";
$rsCurMonth = mysql_query($query_rsCurMonth, $Sales) or die(mysql_error());
$row_rsCurMonth = mysql_fetch_assoc($rsCurMonth);
$totalRows_rsCurMonth = mysql_num_rows($rsCurMonth);
mysql_select_db($database_Sales, $Sales);
$query_rsCurYear = "SELECT Sales.rep, COUNT(*) FROM Sales WHERE YEAR(date) =
YEAR(CURDATE()) GROUP BY Sales.rep";
$rsCurYear = mysql_query($query_rsCurYear, $Sales) or die(mysql_error());
$row_rsCurYear = mysql_fetch_assoc($rsCurYear);
$totalRows_rsCurYear = mysql_num_rows($rsCurYear);
Current Output Table
<table width="400" border="1" cellspacing="0" cellpadding="0">
<tr>
<th width="175" align="center"></th>
<th width="75" align="center">Current<br />Week</th>
<th width="75" align="center">Current<br />Month</th>
<th width="75" align="center">YTD</th>
</tr>
<?php do { ?>
<tr>
<th align="left"><?php echo $row_rsCurYear['rep']; ?></th>
<td align="center"><?php echo $row_rsCurWeek['COUNT(*)']; ?></td>
<td align="center"><?php echo $row_rsCurMonth['COUNT(*)']; ?></td>
<td align="center"><?php echo $row_rsCurYear['COUNT(*)']; ?></td>
</tr>
<?php } while ($row_rsCurWeek = mysql_fetch_assoc($rsCurWeek)); ?>

try{
$sql = "SELECT * FROM contacts";
$result = $pdo->query($sql);
if($result->rowCount() > 0){
echo "<table>";
echo "<tr>";
echo "<th>username</th>";
echo "<th>current_week</th>";
echo "<th>current_month</th>";
echo "<th>ytd</th>";
echo "</tr>";
while($row = $result->fetch()){
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['current_week'] . "</td>";
echo "<td>" . $row['current_month'] . "</td>";
echo "<td>" . $row['ytd'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
unset($result);
} else{
echo "No records matching your query were found.";
}
} catch(PDOException $e){
die("ERROR: Could not able to execute $sql. " . $e->getMessage());
}

Related

Eliminating duplicate specific rows?

Please Help!! I just want to display the Barangay which is talojongon with no other duplicate values
Let's say my current output is:
| Barangay | Disease |
__________________________________
| Talojongon | Cancer |
| Talojongon | Cancer |
| Talojongon | Cancer |
and i want to turn this to:
| Barangay | Disease |
__________________________________
| Talojongon | Cancer |
| | Cancer |
| | Cancer |
The receive in the database is for disease and status in the database is the barangay...thank you...Any suggestions is appreciated ..:)
This is my current part of my code
<br>
<table id="keywords" class="table table-striped table-bordered">
<thead>
<tr>
<th><center>Barangay</th></center>
<th><center>Diseases</th></center>
</tr>
</thead>
<tbody>
<?php
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM patients WHERE receive="Cancer"';
foreach ($pdo->query($sql) as $row){
echo '<tr>';
echo '<td><center>'. $row['status'] . '</center></td>';
echo '<td><center>'. $row['receive'] . '</center></td>';
}
Database::disconnect();
?>
</tr>
</tbody>
How about this?
<?php
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM patients WHERE receive="Cancer" order by status';
$last = '';
foreach ($pdo->query($sql) as $row){
echo '<tr>';
if($last == $row['status'])
{
echo '<td></td>';
}
else
{
$last = $row['status'];
echo '<td><center>'. $row['status'] . '</center></td>';
}
echo '<td><center>'. $row['receive'] . '</center></td>';
}
Database::disconnect();
?>
<?php
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM patients WHERE receive="Cancer"';
$counter=0;
foreach ($pdo->query($sql) as $row){
$counter++;
echo '<tr>';
if($counter==1){
echo '<td><center>Cancer</center></td>';
}else{
echo '<td><center></center></td>';
}
echo '<td><center>'. $row['receive'] . '</center></td>';
}
Database::disconnect();
?>
Simply echo the status just in the first row...
Save the last value of $row['status'] in a variable. If it's the same leave it blank in the output.
$last_status = null;
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
$status = $row['status'] == $last_status ? '' : $row['status'];
echo '<td style="text-align: center;">' . $status . '</td>';
echo '<td style="text-align: center;">' . $row['receive'] . '</td>';
echo '</tr>';
$last_status = $row['status'];
}
Also, you shouldn't use the obsolete <center> tag, use CSS. And </tr> should be inside the loop, not at the end.
You probably should also use ORDER BY status in the query, so that all the rows with the same status are together.

Increment database by 1, using UPDATE (mysql). What's wrong with my code?

I try to write code that receive a student ID as an input in student_list.php, and pass this ID to score.php.
At score.php, use that ID to match and pull out student's name from database, and display it here.
Then, below the name, there is an input field, for adding 1 score to database for this student.
But I got this message, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1".
Any help would be appreciated. Thank you.
student_list.php
<?php
include_once 'DBconnect.php';
?>
<html>
<head>Student List</head>
<body>
<form method="post" action="score.php">
<?php
$result = mysql_query("SELECT * FROM term3")
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Student ID</th> <th>First Name</th> <th> Button </th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['student_id'] . ' </td> ';
echo '<td>' . $row['student_fname'] . ' </td>';
echo '<td> <button type="submit" name="btn_student_id" value=" ' . $row['student_id'] . ' " >Select</button> </td>';
echo '</tr>';
}
echo "</table>";
?>
</form>
</body>
</html>
score.php
<?php
include_once 'database_connect.php';
?>
<html>
<head>Add Score</head>
<body>
<?php
$student_id = $_POST["btn_student_id"];
$result = mysql_query("SELECT * FROM term3 WHERE student_id=".$_POST['btn_student_id'])
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Student ID</th> <th>First Name</th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['student_id'] . ' </td> ';
echo '<td>' . $row['student_fname'] . ' </td>';
echo '</tr>';
}
echo "</table>";
if(isset($_POST['btn_add_score'])) {
$score = $_POST['score'];
mysql_query ("UPDATE term3 SET score = score + 1 WHERE student_id = ' ".$_POST['btn_student_id']. " ' ");
}
?>
<form method="post">
<table>
<tr>
<td>Score</td>
<td> <input type="number" name="score" size="8">
<button type="submit" name="btn_add_score" >Add</button>
</td>
</tr>
</table>
</form>
</body>
</html>
----------------------------------------
Student ID | Name | Select |
----------------------------------------
10001 | Pat | Button (submit) |
----------------------------------------
10002 | Jess | Button (submit) |
----------------------------------------
Try to remove the quotation marks around the student id in the update query. It is redundant
UPDATE term3 SET score = score + 1 WHERE student_id = ".$_POST['btn_student_id']);
Update
$result = mysql_query("SELECT * FROM term3 WHERE student_id=".$_POST['btn_student_id'])
to
$result = mysql_query("SELECT * FROM term3 WHERE student_id='".$_POST['btn_student_id']."'")

Save content of table to a variable

I have a problem,
I want to create a variable wich is save the content of a table,
here is the code:
<?php
include 'koneksi.php';
$query = "SELECT transaksi.id as id, transaksi.deskripsi as deskripsi,
sum(case when shift='1' then transaksi.jumlah end) as shift1,
sum(case when shift='2' then transaksi.jumlah end) as shift2,
sum(transaksi.jumlah) as total
from transaksi where tanggal='2014-11-15' group by id, deskripsi";
$data1 = mysql_query($query) or die('Invalid query: ' .mysql_error());
$no=1;
while($row = mysql_fetch_object($data1)){
$output="
<table>
<tr>
<td>".$no++."</td>
<td>".$row->id."</td>
<td>".$row->deskripsi."</td>
<td>".$row->shift1."</td>
<td>".$row->shift2."</td>
<td>".$row->total."</td>
</tr>
</table>";}
echo $output;
?>
The code above show the result:
3 7100-04000 FINISH GOOD 32100-KVY-7000 20 11 31
The result show only last record of the query,
It should be like this:
1 7100-00000 FINISH GOOD 32100-KZRM-B200 10 9 19
2 7100-03000 FINISH GOOD 32100-KVB-N700 7 8 15
3 7100-04000 FINISH GOOD 32100-KVY-7000 20 11 31
Please help, because I want to use $output as $body in php mailer.
Thank you.
try this:
echo "<table>";
while($row = mysql_fetch_object($data1)){
echo "<tr>
<td>".$no++."</td>
<td>".$row->id."</td>
<td>".$row->deskripsi."</td>
<td>".$row->shift1."</td>
<td>".$row->shift2."</td>
<td>".$row->total."</td>
</tr>";}
echo "</table>";
or if you want to use a variable
$output = "<table>";
while($row = mysql_fetch_object($data1)){
$output .= "<tr>
<td>".$no++."</td>
<td>".$row->id."</td>
<td>".$row->deskripsi."</td>
<td>".$row->shift1."</td>
<td>".$row->shift2."</td>
<td>".$row->total."</td>
</tr>";}
$output .= "</table>";
echo $output;
Try this -
$output = '<table>';
while($row = mysql_fetch_object($data1)){
$output .= "
<tr>
<td>".$no++."</td>
<td>".$row->id."</td>
<td>".$row->deskripsi."</td>
<td>".$row->shift1."</td>
<td>".$row->shift2."</td>
<td>".$row->total."</td>
</tr>";
}
$output .= "</table>";
echo $output;
echo "<table>";
while($row = mysql_fetch_object($data1)){
$output=?>
<tr>
<td><?php echo $no++;?></td>
<td><?php echo $row->id;?></td>
<td><?php echo $row->deskripsi;?></td>
<td><?php echo $row->shift1;?></td>
<td><?php echo $row->shift2;?></td>
<td><?php echo $row->total;?></td>
</tr>
<?php
}
echo $output;
?>
echo "</table>";

SELECT *, SUM(NET) AS TOTAL FROM TABLE1 WHERE CLAUSE

<?php require_once('../includes/connection.php');?>
<?php require_once('../includes/header.php');?>
<?php
$color="1";
$respo = $_GET['respo'];
$data = explode("+", $respo);
$month = date("m", strtotime($data[1])) . "<br />";
$year = date("Y", strtotime($data[1])) . "<br />";
**$viewrecord = "SELECT *, (pr.roll1 + pr.roll2 + pr.roll3 + pr.roll4 + pr.roll5 + pr.roll6) AS rolls FROM tbl_payroll dv join tbl_payroll pr on pr.dv_id = dv.dv_id WHERE dv.respo='".mysql_real_escape_string($data[0])."' && year(dv.date_added)='$year' && month(dv.date_added)='$month'";**
$run_viewrecord = mysql_query($viewrecord) or die(mysql_error());
{
echo "<table border='1' width='100%' style='border:1px solid silver' cellpadding='5px' cellspacing='0px'>
<tr bgcolor='#666666' style='color:#FFFFFF'>
<th>Date Encoded</th>
------------HEADER--------- etc....
-------THERE SHOULD BE A IF STATEMATE HERE-----------------
(where if no records match "dv.dv_id=pr.pr.dv_id". It would still display records from tbl_dv..)
while ($row = mysql_fetch_row($run_viewrecord)) {
if($color==1){
echo "<tr bgcolor='#ffffff'>";
echo "<td align='center'>" .date_format(date_create($row[17]), "m/d/y")."</td>
**--------- I WANT TO DISPLAY THE ROLLS HERE --------------------**
echo "</td></tr>";
$color="2";
} else {
echo "<tr bgcolor='#ebeaea'>";
echo "<td align='center'>" .date_format(date_create($row[17]), "m/d/y")."</td>
**--------- I WANT TO DISPLAY THE ROLLS HERE --------------------**
echo "</td></tr>";
$color="1";
}
}
echo '</table>';
echo '<td><tr><table><br /><br />';
}
?>
I was hoping to add an IF statement before WHILE. Which will still display records even there is no match dv_id on both table2. It should still display records.. The COLUMN ROLLS IF No match it will display a 0.00 value. LINK>> http://i599.photobucket.com/albums/tt79/emcevo/viewphpdisplay_zpsfc6a8174.jpg
//Total NET
<?php
$qry2 = "SELECT *, SUM(net) as sum_net FROM tbl_dv";
$run2 = mysql_query($qry2) or die(mysql_error());
while ($row = mysql_fetch_array($run2)) {
?>
<tr>
<td colspan="5" style="text-align:right;" /><b>TOTAL NET</b></td>
<td colspan="6" style="text-align:left;font-size: 14px;" /><b><?php echo number_format($row['sum_net'],2); ?></b></td>
</tr><?php }?>
</td></tr></table>
How can I display the TOTAL NET below code:
$qry2 = "SELECT *, SUM(net) as sum_net FROM tbl_dv";.
I top codes are all working.. The bold Section BUTTOM code is the Problem.
I guess you get downvotes because your whole php code-block is unnecessary as far I can see. Your query "$qry2" is incorrect; you can't sum without a group by (unless you only do a sum). eg:
select dv_id, sum(net) as sum_net from tbl_dv group by dv_id

Read the latest number from loop

How can I read the latest number from the following code
<table width="100" border="1" >
<tr align="center" bgcolor="#999999" >
<td >NO</td>
<td >Name</td>
<td >PBSID</td>
</tr>
$query ="select * from stock ";
$hasil = mysql_query($query);
$no=0;
while ($row = mysql_fetch_array($hasil))
{
$no++;
if($x != $row[pbsid] )
{
$no=1;
echo "<tr bgcolor=#CCCCCC>
<td colspan=4 ><b> GROUP $row[pbsid]</b></td>
</tr>";
}
$mo=count($no);
echo "<tr><td>$no </td><td> $row[bnama]</td><td>$row[pbsid] </td></tr>";
$x = $row["pbsid"];
}
The result:
===========
NO | NAME | GROUP
---------------------------------------
===========
GROUP 1
---------------------------------------
===========
1 | A | 1
2 | B | 1
3 | C | 1
4 | D | 1
---------------------------------------
===========
GROUP 2
---------------------------------------
===========
1 | A | 2
2 | B | 2
3 | C | 2
I want to show the latest GROUP number, Group 1=4 and Group 2=3, can anyone help me with this?
Try this query:
SELECT s.*, c.count
FROM stock AS s
LEFT JOIN ( SELECT pbsid, COUNT(1) AS `count` FROM stock GROUP BY pbsid ) AS c
ON s.pbsid = c.pbsid
Now you will have the count for each group, which is what it seems you really want.
If you'd prefer to do all the work in PHP for some reason, you could do it like this:
<table width="100" border="1" >
<tr align="center" bgcolor="#999999" >
<td >NO</td>
<td >Name</td>
<td >PBSID</td>
</tr>
$query ="select * from stock ";
$hasil = mysql_query($query);
$no=0;
$counts = array(); // make an array of counts for each group, keyed by pbsid
$rows = array(); // put all the data from SQL into an array
while ($row = mysql_fetch_array($hasil))
{
$rows[] = $row;
if (!isset($counts[$row[pbsid]]))
{
$counts[$row[pbsid]] = 0; // initialize it to 0
}
$counts[$row[pbsid]]++; // increment for each occurrence of a pbsid
}
foreach ($rows as $row)
{
$no++;
if($x != $row[pbsid] )
{
$no=1;
echo "<tr bgcolor=#CCCCCC>
<td colspan=4 ><b> GROUP $row[pbsid]</b> Count = " . $counts[$row[pbsid]] . " </td>
</tr>";
}
$mo=count($no);
echo "<tr><td>$no </td><td> $row[bnama]</td><td>$row[pbsid] </td></tr>";
$x = $row["pbsid"];
}
Although, I think it's much simpler to just have this information calculated in your query.
This would work
$query ="select * from stock ";
$hasil = mysql_query($query);
$no=0;
$latest = array();
while ($row = mysql_fetch_array($hasil))
{
$no++;
if($x != $row[pbsid] ){
$no=1;
echo "<tr bgcolor=#CCCCCC>
<td colspan=4 ><b> GROUP $row[pbsid]</b></td>
</tr>";
}
$mo=count($no);
echo "<tr><td>$no </td><td> $row[bnama]</td><td>$row[pbsid] </td></tr>";
$x = $row["pbsid"];
$key = 'Group'.$row['pbsid'];
$latest[$key] = $no;
}
print_r($latest);
<table width="100" border="1" >
<tr align="center" bgcolor="#999999" >
<td >NO</td>
<td >Name</td>
<td >PBSID</td>
</tr>
$last_no = array();
$query ="select * from stock ";
$hasil = mysql_query($query);
$no=0;
while ($row = mysql_fetch_array($hasil))
{
$no++;
if($x != $row[pbsid] ){
$no=1;
echo "<tr bgcolor=#CCCCCC>
<td colspan=4 ><b> GROUP $row[pbsid]</b></td>
</tr>";
}
$mo=count($no);
echo "<tr><td>$no </td><td> $row[bnama]</td><td>$row[pbsid] </td></tr>";
$x = $row["pbsid"];
$last_no[] = $no;
}
print_r($last_no);
Change your query $query ="select MAX(no),bnama, pbsid from yourtablename GROUP BY pbsid ";

Categories