<?php require_once('../includes/header.php');?>
<?php require_once('../includes/connection.php');?>
<?php include('../includes/get_username.php');?>
<?php
include('sanitise.php');
$month = sanitise($_GET['month']);
?>
<table id="contentbox">
<tr>
<td>
<?php
$color="1";
//view record
$qry = mysql_query("SELECT * FROM tbl_dv WHERE monthname(date_added) = '$month' ORDER BY dv_id DESC");
echo "<table class='dvr_table' id='alternatecolor' width='100%'>
<tr>
<th>DATE ADDED</th>
<th>CT</th>
<th>PAYEE</th>
<th>PARTICULAR</th>
<th>PM</th>
<th>VOUCHER NO.</th>
<th>NET</th>
<th>OBR NO.</th>
<th>";
//RESPO/Office
$sql = "SELECT respo FROM tbl_dv WHERE monthname(date_added) = '$month' GROUP BY respo";
$result = mysql_query($sql);
echo "<select name='respo'>
<option value=' ' disabled='disabled' selected='selected'>Select a RESPO/Office</option>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['respo'] . "'>" . $row['respo'] . "</option>";
}
echo "</th>
<th>ACCOUNT CODE</th>
<th>FPP CODE</th>
<th>DEDUCTION</th>
</tr>";
while ($row = mysql_fetch_array($qry)) {
$dv_id = $row['dv_id'];
if($color==1){
echo "<tr bgcolor='#ffffff'>
<td style='text-align:center;'>" .date_format(date_create($row['date_added']), 'm/d/y')." </td>
<td style='text-align:center;'>" .$row['cashtype']."</td>
etc.......
$color="2";
} else {
echo "<tr bgcolor='#ebeaea'>
<td style='text-align:center;'>" .date_format(date_create($row['date_added']), 'm/d/y')." </td>
<td style='text-align:center;'>" .$row['cashtype']."</td>
etc......
$color="1";
}
}
echo "</tr>";
?>
I Have all it work. Only 1 PROBLEM.. I CANNOT DISPLAY the selected RESPO/OFFICE. As I select the RESPO/OFFICE it will sort out the selected RESPO/OFFICE... What should I resvise my code or add code.. Please Help!
A few suggestions first:
mysql is depreciated and you should move to PDO prepared statements
or mysqli. You can consult the php manual for this.
comment your code, you are not able to follow along unless you read
from top to bottom.
Now onto your code:
<?php
// Start document with includes needed, header, connection, functions.
require_once('../includes/header.php');
require_once('../includes/connection.php');
include('../includes/get_username.php');
include('sanitise.php');
// Start with the month variable posted from $_GET, this will begin the initial display.
$month = sanitise($_GET['month']);
?>
<!-- Begin Table Display and Layout -->
<table id="contentbox">
<tr>
<td>
<?php // Open PHP Tag to get variables to display in the table
$color="1";
// Initial Query to get the data for the month passed by GET, ordering by dv_id.
$qry = mysql_query("SELECT * FROM tbl_dv WHERE monthname(date_added) = '$month' ORDER BY dv_id DESC");
// Format the table rows for display
echo "<table class='dvr_table' id='alternatecolor' width='100%'>
<tr>
<th>DATE ADDED</th>
<th>CT</th>
<th>PAYEE</th>
<th>PARTICULAR</th>
<th>PM</th>
<th>VOUCHER NO.</th>
<th>NET</th>
<th>OBR NO.</th>
<th>";
// Fill the SELECT option with all the RESPO/Office data
$sql = "SELECT respo FROM tbl_dv WHERE monthname(date_added) = '$month' GROUP BY respo";
$result = mysql_query($sql);
// Display the select with the newly populated content
echo "<select name='respo'>
<option value=' ' disabled='disabled' selected='selected'>Select a RESPO/Office</option>";
// While the result contains data we will display respo as a select option.
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['respo'] . "'>" . $row['respo'] . "</option>";
}
// Finish the table header
echo "</th>
<th>ACCOUNT CODE</th>
<th>FPP CODE</th>
<th>DEDUCTION</th>
</tr>";
// While we have data from the original query we will display it
while ($row = mysql_fetch_array($qry)) {
$dv_id = $row['dv_id']; // grab the dv_id
if($color==1){ // Setup color scheme
echo "<tr bgcolor='#ffffff'> // Color for the row
// Let's begin filling the table with data
<td style='text-align:center;'>" .date_format(date_create($row['date_added']), 'm/d/y')." </td>
<td style='text-align:center;'>" .$row['cashtype']."</td>
etc.......
$color="2";
} else {
echo "<tr bgcolor='#ebeaea'>
<td style='text-align:center;'>" .date_format(date_create($row['date_added']), 'm/d/y')." </td>
<td style='text-align:center;'>" .$row['cashtype']."</td>
etc......
$color="1";
}
}
echo "</tr>";
?>
After reviewing the above code it is clear that you haven't setup a query to filter the data that you are displaying based on the selected option.
Your first query:
// Initial Query to get the data for the month passed by GET, ordering by dv_id.
$qry = mysql_query("SELECT * FROM tbl_dv WHERE monthname(date_added) = '$month' ORDER BY dv_id DESC");
You could append a new variable for the selected option, expand your WHERE statement. WHERE monthname(date_added) = '$month' && respo = '$respo' and move this query after the select option so it has access to the value chosen by the select option. Lastly, unless you are going to use jQuery to deal with the select box changing values, you would need a submit button to show the state change for the select option.
Pseudo Code:
Check for Submit
Change value of the department in the query WHERE clause
Display the requested data
This should get you in the direction you need to go in order to achieve the desired result.
EDIT:
In the 3rd answer you posted:
$viewrecord = "SELECT * FROM tbl_dv WHERE respo='".mysql_real_escape_string($respo)."' AND year(date_added)='$year' GROUP BY date_added ";
You said it is not filtering by the month being passed in, you are not adding it into your query, specifically the WHERE clause. So it should be:
$viewrecord = "SELECT * FROM tbl_dv WHERE respo='".mysql_real_escape_string($respo)."' AND month(month_added)='$month' AND year(date_added)='$year' GROUP BY date_added ";
FINAL EDIT:
Your first issue stems from not sending the $_GET['date_added'] value to the view page, so you will never display anything. Properly format the select box and add the data for the RESP and Date data from the database:
echo "<option value='". $row['respo'] . "+". $row['date_added'] . "'>" . $row['respo'] . " (".$row['count(*)'].")</option>";
Now that we are passing the respo and the date string we can work with the data on the next page:
/* UNCOMMENT NEXT LINE FOR ERROR DETECTION */
//error_reporting( E_ALL );
/* GET THE ENTIRE STRING PASSED FROM THE SELECT OPTION */
$respo = $_GET['respo'];
/* EXPLODE THE DATA BY + SYMBOL TO GET THE DATA */
$data = explode("+", $respo);
/* FORMAT THE DATETIME FROM THE STRING TO A FRIENDLY FORMAT */
$month = date("m", strtotime($data[1])) . "<br />";
$year = date("Y", strtotime($data[1])) . "<br />";
This is how I choose to work with the data and get what I needed, so now the query would be:
$viewrecord = "SELECT * FROM tbl_dv WHERE respo='".mysql_real_escape_string($data[0])."' && year(date_added)='$year' && month(date_added)='$month' ";
This will only display the records for the RESPO and the date by year and month passed into the page via $_GET.
<?php require_once('../includes/header.php');?>
<?php
$respo = $_GET['respo'];
$month = (int) (!empty($_GET['date_added']) ? $_GET['date_added'] : date('m'));
$year = (int) (!empty($_GET['date_added']) ? $_GET['date_added'] : date('Y'));
//database call here
$viewrecord = "SELECT * FROM tbl_dv WHERE respo='".mysql_real_escape_string($respo)."' AND year(date_added)='$year' GROUP BY date_added ";
$result = mysql_query($viewrecord, $db) or die (mysql_error());
$num_result = mysql_num_rows($result);
{
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>
etc...... (header here)
while ($row = mysql_fetch_row($result)) {
echo '<tr>';
echo "<tr ><td align='center'>" .date_format(date_create($row[17]), "m/d/y")."</td>
etc...
}
mysql_close($db);
?>
This is the code i come up. This is just an alternative solution. But the problem with this solution is that. I won't display the record in MONTH as call. THe respo is working as I select the RESPO it will display the all RESPO I ask, but the problem is that it include all the previous months with the RESPO I select. When I just select the specific month only..
//RESPO/Office
**echo '<form action="view.php" method="post">';**
$byrespo = mysql_query("SELECT DISTINCT count(*), respo FROM tbl_dv WHERE monthname(date_added)='$month' GROUP BY respo");
echo "<select name='respo' onchange='this.form.submit()'>
<option value=' ' disabled='disabled' selected='selected'>Select a RESPO/Office</option>";
while ($row = mysql_fetch_array($byrespo)) {
echo "<option value='" . $row['respo'] . "'>" . $row['respo'] . " (".$row['count(*)'].")</option>";
}
echo "</select></form>";
I just rushing to came up with solution.. So I add a and I create another view.php
Related
I am trying to make a while loop which retrieves all the relevant data from Mysql database but it doesn't work for more than one variable, the problem I think is with the while loop because I have echoed the sql statement and it retrieved the values of the variables right, the code is:
$wherein = implode(',', $_SESSION['cart']);
$sql = "select ID, Name, Price from lamps WHERE ID = '$wherein'";
$result = mysqli_query($conn, $sql);
echo "<table style='width:100%' border='1' >";
echo "<tr>";
echo "<th> Product Name</th>";
echo "<th>Product Price </th>" ;
echo "<th>Quantity </th>" ;
echo "</tr>";
while ( $row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td> $". $row['Price'] . "</td>" ;
echo "<td> <select>
<option value= '1'>1</option>
<option value= '2'>2</option>
<option value= '3'>3</option>
</select>
</td>";
echo "</tr>";
}
echo "</table>";
I have tried many things with the code but the problem still exists, I would really appreciate your help, Thank you.
The </table> statement is inside the while loop. Hence once the table is closed after the first loop the rest of the data might not be showing on the browser (it will still be in your html source code). Try it with moving the </table> statement to the last line.
assuming that by imploding the variable you will have multiple IDs then you should use the IN statement in the sql.
$wherein = implode("','", $_SESSION['cart']);
$sql = "select ID, Name, Price from lamps WHERE ID IN ( '$wherein' )";
The rendered output can be simplified slightly and the table should be closed after the loop! Not knowing the contents of $wherein makes it tricky to answer but I would think the IN statement seems to fit the nature of a comma separated value being used in the query better than a direct equals =
$wherein = implode("','", $_SESSION['cart']);
$sql = "select ID, Name, Price from lamps WHERE ID IN ( '$wherein' )";
$result= mysqli_query( $conn, $sql );
echo "
<table style='width:100%' border='1' >
<tr>
<th> Product Name</th>
<th>Product Price </th>
<th>Quantity </th>
</tr>";
while ( $row = mysqli_fetch_object( $result ) ){
echo "
<tr>
<td>{$row->Name}</td>
<td>${$row->Price}</td>
<td>
<select>
<option value= '1'>1
<option value= '2'>2
<option value= '3'>3
</select>
</td>
</tr>";
}
echo "
</table>";
/* Example of using an array of IDs and imploding to generate where conditions for IN clause */
$cart=array('BGL1','BJL');
/* session - cart */
$wherein=implode( "','", $cart );
/* correctly add quotes around each string */
$sql="select ID, Name, Price from lamps WHERE ID IN ('$wherein');";
/* use the new $wherein string within quotes */
echo $sql;
>> select ID, Name, Price from lamps WHERE ID IN ('BGL1','BJL');
Hi in the below i want to show Consultation Charges values for that i took the td.But in that td not displying anything even td also not showing.
After executing this query i want to find the no. of rows based on the rows i want to display the data.
My expected output:
Bill Particular Bill Sub Particular Doctor Date Dis. Amt.
Consultation Charges:
all the values based on no of rows.
php
<table width="100%">
<th>Bill Particular</th>
<th>Bill Sub Particular</th>
<th>Doctor</th>
<th>Date</th>
<th>Dis. Amt.</th>
<th>Charge</th>
<th>No. of Times</th>
<th>Amount</th>
</table>
<tr><th colspan=2>Consultation Charges:</th>
<?php
$div_options = array();
$sql = "SELECT ibp.ipd_bp_id, ibp.bp_id, bp.bp_name, ibp.bsp_id, bsp.bsp_name, ibp.doctor_id, ab.employee_name doctor, ibp.date date, ibp.amount charge, ibp.discount_amount discount, ibp.no_of_time, (ibp.no_of_time * ibp.amount) total_amount
FROM bill_particular_master bp
INNER JOIN ipd_bill_particular ibp ON ibp.bp_id = bp.bp_id
LEFT OUTER JOIN bill_sub_particular bsp ON bsp.bsp_id = ibp.bsp_id
LEFT OUTER JOIN address_book ab ON ab.employee_id = ibp.doctor_id
WHERE ibp.ipd_reg_no = '$ipd_no'
AND bsp.consultant =1
AND bsp.package = 0
AND bsp.admission = 0
AND bp.bp_name != 'Scan Charges'
AND bp.bp_name !='Procedure'";
$sth = $dbh->query($sql);
//$row=$dbh->fetch();
$i=1;
while($row=$sth->fetch(PDO::FETCH_ASSOC)){
$sub_arr['bp_name'] = $row['bp_name'];
$sub_arr['bsp_name'] = $row['bsp_name'];
echo "<tr>
<td>Here is the text - " . $sub_arr['bp_name'] . "</td>
<td>The ID of the text is - " .$sub_arr['bsp_name'] . "</td>";
if($i !== 0) {
echo "<td>The ID of the previous entry is - " .$sub_arr['bp_name'] . "</td>";
}
else {
echo "<td> </td>";
}
echo "</tr>";
$i = $row['bp_name'];
}
?>
</tr>
echo "<td>The ID of the previous entry is - " . $row['bp_name'] . "</td>";
You have there variable thats not even defined in your code.
I am trying to get all values shown in a table from mysql but getting one .
I want to get the rows of the mysql table at in the last table mentioned in the code
////////Here is a desc of no use --- blah for just posting this question / as i am getting an error msg for giving more details information about this question /////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Here is the code:
$sql = 'SELECT
item_added
FROM
products_added
ORDER BY id';
$results = mysqli_query($conn, $sql);
if(mysqli_num_rows($results) < 1){
echo "No items";
}else{
$new_sql = 'SELECT
item_added,
quantity,
amount,
sum(amount) as items_total
FROM
products_added
where `username` = "'.mysqli_real_escape_string($conn, $_SERVER["REMOTE_ADDR"]).'"
ORDER BY id';
$resu = mysqli_query($conn, $new_sql);
}
?>
<table>
<thead>
<tr>
<td>Item</td>
<td>Qyt</td>
<td>Price</td>
<td>Action</td>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_assoc($resu)){
echo "<tr>";
echo "<td>" . $row['item_added'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "<td>" . $row['amount'] . "</td>";
echo "<td><a class=\"remove-from-cart\" href=\"\"><i class=\"fa fa-times\"></i></a></td>";
echo "</tr>";
}
?>
</tbody>
</table>
It looks like its because you're using an aggregate function SUM() without a GROUP BY. In the $new_sql query, try adding "GROUP BY item_added" right before "ORDER BY id".
I want to count all user records and display them in tables, I am trying this code code, It displays the record for one user only, I want to display records from all users.
$u=$_POST['userid'];
$result1 = mysqli_query($con,"SELECT COUNT(user_id) as total FROM table-name where user_id=$u");
echo "<table border='1'>
</tr>
<tr>
<th>User ID</th>
<th>count</th>
</tr>";
while($row = mysqli_fetch_array($result1))
{
echo "<tr>";
echo "<td>" . $u . "</td>";
echo "<td>" . $row['total'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
Try the following SQL Query:
SELECT `user_id`, COUNT(`user_id`) as `total` FROM `table-name` GROUP BY `user_id`;
Refer to the documentation of the GROUP BY clause.
Use below:
$result1 = mysqli_query($con,"SELECT COUNT(user_id) as total FROM table-name");
where clause use for filter the data.
refer http://www.w3schools.com/sql/sql_where.asp
Im working on a table where data from users will be picked from database.
The problem is that im not being able to put two statements on my while condition because im getting data from different tables and rows.
#CODE
<?php
$con = mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database_name") or die(mysql_error());
$result = mysql_query("select * FROM users");
$space_used = mysql_query("SELECT SUM(fileSize) FROM file WHERE file.userId=users.id AND file.statusId=1");
$total_files = mysql_query("SELECT COUNT(id) FROM file WHERE file.userId=users.id AND file.statusId=1");
echo "<table class='table table-striped table-hover table-bordered' id='sample_editable_1'>
<tr>
<th>Username</th>
<th>Type</th>
<th>Email</th>
<th>Last Login</th>
<th>Last IP</th>
<th>space_used</th>
<th>total_files</th>
<th>status</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['level'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['lastlogindate'] . "</td>";
echo "<td>" . $row['lastloginip'] . "</td>";
echo "<td>" . $space_used['space_used'] . "</td>";
echo "<td>" . $total_files['total_files'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
What im getting is all the values of username, level, email, lastlogindate, lastloginip & status. The two that are not showing on my table are the ones of the second condition i want to implement: space_used and total_files.
What am i doing wrong? Im not an expert in php and the fact that i had a mix of tutorials and a script im chaning to get to this result had complicated a little bit.
Thanks
You need to rework your query, you can resume your 3 queries into a single query using JOIN:
SELECT u.*,
SUM(f.fileSize) AS space_used,
COUNT(f.id) AS total_files
FROM users u
JOIN file f
ON f.userId = u.id AND f.statusId = 1
GROUP BY u.id
Then you can read it like this:
<?php
// your database info
$db_host = 'your MySQL server host';
$db_user = 'your username';
$db_pass = 'your password';
$db_name = 'your database name';
$con = new mysqli($db_host, $db_user, $db_pass, $db_name);
if($con->connect_error)
{
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
}
if (!$result = $con->query("SELECT u.*,
SUM(f.fileSize) AS space_used,
COUNT(f.id) AS total_files
FROM users u
JOIN file f
ON f.userId = u.id AND f.statusId = 1
GROUP BY u.id"))
{
die('Select query error: ' . $con->error);
}
?>
<table class='table table-striped table-hover table-bordered' id='sample_editable_1'>
<tr>
<th>Username</th>
<th>Type</th>
<th>Email</th>
<th>Last Login</th>
<th>Last IP</th>
<th>space_used</th>
<th>total_files</th>
<th>status</th>
</tr>
<?php
while ($row = $result->fetch_array())
{
?>
<tr>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['level']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['lastlogindate']; ?></td>
<td><?php echo $row['lastloginip']; ?></td>
<td><?php echo $row['space_used']; ?></td>
<td><?php echo $row['total_files']; ?></td>
<td><?php echo $row['status']; ?></td>
</tr>
<?php
}
$con->close();
?>
</table>
You want to aggregate per userId.
Pull these two query's into the while loop. Use the retrieved User's id to Make the aggregate query.
while($row = mysql_fetch_array($result))
{
$space_used = mysql_query("SELECT SUM(fileSize) FROM file WHERE file.userId={$row['id']} AND file.statusId=1");
$total_files = mysql_query("SELECT COUNT(id) FROM file WHERE file.userId={$row['id']} AND file.statusId=1");
...
}
Combine them into one query.
$agragateDataResponce = mysql_query("SELECT SUM(fileSize) as size,
COUNT(id) as count
FROM file
WHERE file.userId={$row['id']} AND
file.statusId=1");
$agragateData = mysql_fetch_array($agragateDataResponce);
Access by $agragateData['size'] and $agragateData['count'];
Then look up how to combine the remaining two SQL into one join :) . SO Question on topic
First you query the database and save the results into variables.
$result = mysql_query("select * FROM users");
$space_used = mysql_query("SELECT SUM(fileSize) FROM file WHERE file.userId=users.id AND file.statusId=1");
$total_files = mysql_query("SELECT COUNT(id) FROM file WHERE file.userId=users.id AND file.statusId=1");
Then you iterate through the first of your results with
while($row = mysql_fetch_array($result))
Notice your call to mysql_fetch_array. You never call mysql_fetch_array on the other two results.
See the PHP documentation on mysql_query for more details. Specifically, the return type is a resource.
The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.
However,
SELECT SUM(fileSize) FROM file WHERE file.userId=users.id AND file.statusId=1
is not even what you want. You check for file.userId=users.id but only read from the file table. You are missing the users source table.
Try to use
$space_used = mysql_query("SELECT SUM(fileSize)
FROM file
WHERE file.userId=users.id AND
file.statusId=1");
$total_files = mysql_query("SELECT COUNT(id)
FROM file
WHERE file.userId=users.id AND
file.statusId=1");
the above lines inside the While and verify with the user.id
I am new to stackoverflow so don't mine. I don't know how to make a code look like every one do