PHP update MySQL and export to csv - php

I have a PHP page that shows pending claims which can have their status changed when a checkbox is selected and the array updated.
<?php include("partials/header.php"); ?>
<p>Manage expenses here!</p>
<body>
<style type="text/css">
#dis{
display:none;
}
</style>
<div id="dis">
<!-- here message will be displayed -->
</div>
<p>Current Claims</p>
<div class="container">
<form method="post" action="export.php" enctype="multipart/form-data" >
<table cellspacing="0" width="100%" id="example" class="table table-striped table-hover table-responsive">
<thead>
<tr>
<th>ID</th>
<th>Employee ID</th>
<th>Ref</th>
<th>Date</th>
<th>Filed Date</th>
<th>Type</th>
<th>Client</th>
<th>Project</th>
<th>Narrative</th>
<th>Net</th>
<th>VAT</th>
<th>Status</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
//connection details
require_once 'dbconfig.php';
//end connection details
$query = "SELECT * FROM claim WHERE status='pending'";
$result = mysql_query($query);
if($result === false) {
die(mysql_error());
}
while($row = mysql_fetch_array($result)){
$row_id = $row['c_id'];
$employee_id = $row['employee_id'];
$claim_ref = $row['claim_ref'];
$transaction_date = $row['transaction_date'];
$filed_date = $row['filed_date'];
$expense_type_id = $row['expense_type_id'];
$client_code_id = $row['client_code_id'];
$project_code_id = $row['project_code_id'];
$narrative = $row['narrative'];
$net = $row['net'];
$vat = $row['vat'];
$status = $row['status'];
echo
"<tr>
<td>" . $row_id . "</td>
<td>" . $employee_id . "</td>
<td>" . $claim_ref . "</td>
<td>" . $transaction_date . "</td>
<td>" . $filed_date . "</td>
<td>" . $expense_type_id . "</td>
<td>" . $client_code_id . "</td>
<td>" . $project_code_id . "</td>
<td>" . $narrative . "</td>
<td>" . $net . "</td>
<td>" . $vat . "</td>
<td>" . $status . "</td>
<td><input type='checkbox' name='c_id[]' value='" . $row_id . "'/></td>
</tr>";
}
echo "</tbody></table><input type='submit' value='accept'></form>"; //end form
/***** Update Status *****/
/*print_r($_POST);*/
if(gettype($_POST['c_id'])=="array"){
foreach($_POST['c_id'] as $val){
$id_c = $val;
$query2 = "UPDATE claim SET status = 'accepted' where c_id='".$id_c."'";
$result2 = mysql_query($query2);
if($result2 === false) {
die(mysql_error());
}
echo "Status " .$id_c. " is updated. <br>";
}
}
mysql_close();
?>
</tbody>
</table>
</div>
</div>
<br />
</script>
</body>
</html>
Q1. I was wondering whether the rows which are having the status column changed to accepted, can somehow also be exported to a csv file.
Q2. Is there a way to have two buttons, one for accepting and the other one for rejecting as each claim has three possible statuses: pending, accepted, rejected
Update: the claim table is as follows: http://sqlfiddle.com/#!9/5ce91
create table claim (
c_id INT(5) zerofill primary key auto_increment,
claim_ref VARCHAR(7),
employee_id integer NOT NULL REFERENCES employee(emp_id),
transaction_date DATE,
filed_date DATE,
expense_type_id VARCHAR(20) REFERENCES expense_type(expense_type),
client_code_id VARCHAR(7) REFERENCES client(client_code),
project_code_id VARCHAR(10) REFERENCES project(project_code),
narrative VARCHAR(255),
net decimal(6,2),
vat decimal(6,2),
status ENUM('pending', 'rejected', 'accepted'))
ENGINE = InnoDB;
Another bit is that the project ends in about a week so I'd rather not get into updating to PDO or MySQLi although if anyone has a complete solution in one of these it would be just as good, thanks.

Related

how to list data from 2 mysql datatable into html table

in my mysql database there is 2 table named tblapps and tblapps2 . i have stored some of the data of an application in tblapps and rest of the data in tblapps2.
in backend of my application, currenly all the data of tblapps is getting displayed but suddenly i have a requirement that i want one more column to be shown here from tblapps2 with column name "country" and mysqli table column name "precountry" . and this is completely new for me, i am not getting anyway of doing this.
if any help it will be appreciated.
<div class="portlet-body flip-scroll">
<?php
$sql = "SELECT * FROM tblapps order by id desc";
$result = $connect->query($sql);
if ($result->num_rows > 0) {
echo "<table class='display' id='example'>
<thead class='flip-content'>
<tr>
<th>ID</th>
<th>App ID</th>
<th>Full Name</th>
<th>Journey Date</th>
<th>App type</th>
<th>Visa Type</th>
<th>Fee</th>
<th>Nationality</th>
<th>Country</th>
<th>Submit Date</th>
<th>payment</th>
<th>status</th>
</tr>
</thead>";
while($row = $result->fetch_assoc()) {
echo"<tr>
<td>" . $row["id"]. "</td>
<td>". $row["app_id"]. "</td>
<td>" . $row["firstname"]. " " . $row["lastname"]."</td>
<td>" . $row["journeydate"]. "</td>
<td>" . $row["appl_type"]. "</td>
<td>" . $row["visatype"]. "</td>
<td>" . $row["visa_fee"]. "</td>
<td>" . $row["nationality"]. "</td>
<td>" . $row["precountry"]. "</td>
<td>" . $row["sdate"]. "</td>
<td>" . $row["pay_status"]. "</td>
<td>" . $row["appstatus"]. "</td>
</tr>"; }
echo "</table>"; } else { echo "0 results";}$connect->close();?>
</div>
you can use SQL JOIN for matching values in both tables.
the implementation is depending on your tables structure

How to return average data through the loop in mysqli and php

I am a beginner programmer, and i want to fetch AVG() data in every rows created by the while loop,
this is the code,
<?php
$sql = "SELECT sites_id, sites_nama, sites_alamat, sites_kota_kabupaten, perpanjangan_pagu, sites_tanggal_start, sites_tanggal_finish, perpanjangan_invoice, AVG(perpanjangan_pagu) FROM site";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>
<tr>
<th>Site ID</th>
<th>Site Name</th>
<th>Alamat</th>
<th>Kab.Kota</th>
<th>Pagu</th>
<th>Harga Rata Rata</th>
<th>Awal Kontrak</th>
<th>Akhir Kontrak</th>
<th>Invoice</th>
</tr>";
// output data of each row
$rows = array();
while ($row = $result->fetch_assoc()) {
$rows = $row["AVG(perpanjangan_pagu)"];
echo "
<tr>
<td>" . $row["sites_id"] . "</td>
<td>" . $row["sites_nama"] . "</td>
<td>" . $row["sites_alamat"] . "</td>
<td>" . $row["sites_kota_kabupaten"] . "</td>
<td>" . $row["perpanjangan_pagu"] . "</td>
<td>" . $rows . "</td>
<td>" . $row["sites_tanggal_start"] . "</td>
<td>" . $row["sites_tanggal_finish"] . "</td>
<td>" . $row["perpanjangan_invoice"] . "</td>
</tr>";
}
echo "</table>";
}
else {
//echo "0 results";
}
$conn->close();
?>
and this is the image, you can see that it only return one row.
and this is the image when I deleted the avg function.
please help me to fetch the average data in every row in the table, not just only one table,
Thanks.
can you try this I have modified user code & added the AVG() column.
<?php
$sql = "SELECT sites_id, sites_nama, sites_alamat, sites_kota_kabupaten, perpanjangan_pagu, sites_tanggal_start, sites_tanggal_finish, perpanjangan_invoice, (SELECT AVG(perpanjangan_pagu) FROM site) AS 'AVG_pagu' FROM site";
$result = $conn->query($sql);
// AVG_pagu has the AVG value of all columns of `perpanjangan_pagu` in table `site`
if ($result->num_rows > 0) {
echo "<table>
<tr>
<th>Site ID</th>
<th>Site Name</th>
<th>Alamat</th>
<th>Kab.Kota</th>
<th>Pagu</th>
<th>Harga Rata Rata</th>
<th>Awal Kontrak</th>
<th>Akhir Kontrak</th>
<th>Invoice</th>
</tr>";
// output data of each row
// $rows = array(); // This is not actually required
while ($row = $result->fetch_assoc()) {
//$rows[] = $row["AVG_pagu"]; // This is not actually required
echo "
<tr>
<td>" . $row["sites_id"] . "</td>
<td>" . $row["sites_nama"] . "</td>
<td>" . $row["sites_alamat"] . "</td>
<td>" . $row["sites_kota_kabupaten"] . "</td>
<td>" . $row["perpanjangan_pagu"] . "</td>
<td>" . $row["AVG_pagu"] . "</td>
<td>" . $row["sites_tanggal_start"] . "</td>
<td>" . $row["sites_tanggal_finish"] . "</td>
<td>" . $row["perpanjangan_invoice"] . "</td>
</tr>";
}
echo "</table>";
}
else {
echo "No records found!";
}
$conn->close();
?>

Refresh table after seconds

I'm currently trying to refresh my table every 5 seconds, but I'm not being successful.
<table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Número</th>
<th>Nome do colaborador / secção</th>
</tr>
</thead>
<tbody>
<?php
$req = mysqli_query($con, 'select user_id, user_name from users');
while ($dnn = mysqli_fetch_array($req)) {
echo "<tr>";
echo " <td>" . $dnn['user_id'] . "</td>";
echo " <td>" . $dnn['user_name'] . "</td>";
echo "</tr>";
}
?>
</tbody>
</table>
What I want is to refresh the page every 5 seconds and of course if someone edits he will be able to see the changes that were made.
What can I do to complete this action?
You need to use $.ajax like
$(function(){
function getdata(){
$.ajax({
url:'getdata.php',
success:function(response){
$('#dataTables-example tbody').html(response);
}
});
}
setInterval(function(){getdata()},5000);
});
getdata.php
<?php
$req = mysqli_query($con, 'select user_id, user_name from users');
while ($dnn = mysqli_fetch_array($req)) {
echo "<tr>
<td>" . $dnn['user_id'] . "</td>
<td>" . $dnn['user_name'] . "</td>
</tr>";
}
?>

PHP if statement in html-table outputs error

I am new to HTML5 and PHP, I am trying to output a specific value in table data, If the database-retrieved-value is per condition.
My code:
<table class="scroll">
<thead style="background-color: #99E1D9; color: #705D56;">
<tr>
<th>ID</th>
<th>Name Client</th>
<th>Last Update</th>
<th style="padding-left: 30%;">Status</th>
</tr>
</thead>
<tbody id="hoverTable">
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('patientdb');
$query = "SELECT id, name, date FROM clients";
$result = mysql_query($query);
//status waarden uit
$status = "SELECT status FROM clients";
$status_ = mysql_query($status);
while($row = mysql_fetch_array($result)){ //Loop through results
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>" .
if ($status_ > 60){echo "red";
} elseif ($status_ > 50){echo "yellow";
} else{echo "green";}
. "</td>
</tr>";
}
mysql_close();
?>
</tbody>
</table>
Error output
Parse error: syntax error, unexpected T_IF in
/test/composition/login/portal/portal.php
on line 204
What is the right way to solve this?
EDIT
my current code:
<table class="scroll">
<thead style="background-color: #99E1D9; color: #705D56;">
<tr>
<th>Naam Client</th>
<th>Laatste Update</th>
<th style="margin-left: 40%; padding-left: 0%;">Status</th>
</tr>
</thead>
<tbody id="hoverTable" style="font-size: 11pt;">
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('patientdb');
$query = "SELECT id, naam, datum FROM clients";
$result = mysql_query($query);
$query2 = "SELECT status FROM clients";
$result2 = mysql_query($query2);
if (!empty ($result2)) {
while ($row2 = mysql_fetch_assoc($result2)) {
echo $row2['status'] . "<br />";
}
}
while($row = mysql_fetch_array($result)){ //Loop through results
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['naam'] . "</td>
<td>" . $row['datum'] . "</td>
<td style='padding-left: 30%;'>";
if ($results2 > 60 && $results2 < 70) {
echo "red";
} elseif ($results2 > 50 && $results2 < 60) {
echo "yellow";
} else {
echo "green";
}
echo "</td>
</tr>";
}
mysql_close();
?>
</tbody>
</table>
Output the right data. but partly outside and partly inside the table.
You will have to remove the if statement out of the echo to get rid of the error Try this:
<table class="scroll">
<thead style="background-color: #99E1D9; color: #705D56;">
<tr>
<th>ID</th>
<th>Name Client</th>
<th>Last Update</th>
<th style="padding-left: 30%;">Status</th>
</tr>
</thead>
<tbody id="hoverTable">
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('patientdb');
$query = "SELECT id, name, date FROM clients";
$result = mysql_query($query);
//status waarden uit
$status = "SELECT status FROM clients";
$status_ = mysql_query($status);
while($row = mysql_fetch_array($result)){ //Loop through results
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>";
if ($status_ > 60) {
echo "red";
} elseif ($status_ > 50) {
echo "yellow";
} else {
echo "green";
}
echo "</td>
</tr>";
}
mysql_close();
?>
</tbody>
</table>
You can't have an if statement (or any other statement, for that matter) in the middle of another statement like echo. If you want to concatenate different strings depending on a variable, you can use the conditional (AKA "ternary") operator.
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>" .
$status_ > 60 ? "red" : ($status_ > 50 ? "yellow" : "green" )
. "</td>
</tr>";
Try:
$status = "green";
if ($status > 50)
{
$status="yellow";
}
elseif($status>60)
{
$status="red";
}
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>" .$status. "</td>
</tr>";
You can't append to a string a conditional statement, assign to a variable first for example (like I posted)
This part isn't at the right place:
if ($status_ > 60){echo "red";
} elseif ($status_ > 50){echo "yellow";
} else{echo "green";}
should be:
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>";
if ($status_ > 60){
echo "red";
} elseif ($status_ > 50){
echo "yellow";
} else{
echo "green";
}
echo "</td></tr>";
Surely status_ would not come back with a number, but an array.
$status_ = mysql_query($status);
Without knowing what data is coming back, it is difficult to help.
mysql_query

Delete Row Button for MySQL and PHP

I have an admin panel where administrators can view all the data in a table, however I would like them to have a small delete button next to each row. Each row can be defined by an ID, however I am not sure about the code behind it.
<!-- Requests -->
<div class="panel panel-<?php echo $PColor; ?>">
<table class="table table-striped table-requests">
<thead class="table-requests">
<tr>
<th>Req ID</th>
<th><?php echo "Song Name"; ?></th>
<th><?php echo "Requested By"; ?></th>
<th><?php echo "Comments (If any)"; ?></th>
<th><?php echo "Time"; ?></th>
</tr>
</thead>
<tbody>
<?php
// Select Requests
$SelectRequests = $db->query("SELECT * FROM `requests`");
// Print Output
foreach($SelectRequests as $PrintRequests)
{
echo
"
<tr>
<td><b>" . $PrintRequests['ID'] . "</b></td>
<td>" . $PrintRequests['song'] . "</td>
<td>" . $PrintRequests['name'] . "</td>
<td>" . $PrintRequests['dedicated'] . "</td>
<td>" . $PrintRequests['time'] . "</td>
";
}
?>
</tbody>
</table>
</div>
</div>
Is there any way to have the delete button to the right of each displayed row, and make it functional?
Thank you in advance!
<!-- Lets call this file index.php -->
<!-- Requests -->
<div class="panel panel-<?php echo $PColor; ?>">
<form action='deleteRecord.php' method='GET'>
<table class="table table-striped table-requests">
<thead class="table-requests">
<tr>
<th>Req ID</th>
<th><?php echo "Song Name"; ?></th>
<th><?php echo "Requested By"; ?></th>
<th><?php echo "Comments (If any)"; ?></th>
<th><?php echo "Time"; ?></th>
<!-- Add this for the table header -->
<th><?php echo "Delete"; ?></th>
</tr>
</thead>
<tbody>
<?php
// Select Requests
$SelectRequests = $db->query("SELECT * FROM `requests`");
// Print Output
foreach($SelectRequests as $PrintRequests){
echo
"<tr>
<td><b>" . $PrintRequests['ID'] . "</b></td>
<td>" . $PrintRequests['song'] . "</td>
<td>" . $PrintRequests['name'] . "</td>
<td>" . $PrintRequests['dedicated'] . "</td>
<td>" . $PrintRequests['time'] . "</td>
<td><input type='checkbox' value ='" . $PrintRequests['ID'] . "' name='removal[]'></td>
</tr>";
}
?>
</tbody>
</table>
<input type="submit" value="Submit">
</form>
</div>
deleteRecord.php
<?php
//Assume you can connect to DB
$removals = $_GET['removal'];
if(!empty($removals)){
if(count($removals) > 1){
$r = implode(', ', $removals);
$query="DELETE from requests where ID IN (". $r . ")";
} else {
$query="DELETE from requests where ID = ". $removals[0];
}
//Commit to db
} else {
echo "Nothing Selected";
//Handle an error if needed
}
//redirect
?>
try something like that :
<?php
// Select Requests
$SelectRequests = $db->query("SELECT * FROM `requests`");
// Print Output
foreach($SelectRequests as $PrintRequests)
{
echo
"
<tr>
<td><b>" . $PrintRequests['ID'] . "</b></td>
<td>" . $PrintRequests['song'] . "</td>
<td>" . $PrintRequests['name'] . "</td>
<td>" . $PrintRequests['dedicated'] . "</td>
<td>" . $PrintRequests['time'] . "</td>
<td> <a href='delete.php?pid=" . $PrintRequests['ID'] . "'> Delete</a> </td>;
";
}
?>
In delete.php
<?php
// Connexion to database..
$Query="delete from requests where id=".$_REQUEST['pid'];
if(!mysqli_query($Conection,$Query)) {echo "<i>Error !</i>";}
echo '<script language="javascript"> document.location="Your_previous_page.php";</script>';
?>

Categories