Sort Table via Date Picker Range - php

I'm trying to sort the data table via date picker range (server side) but can't get the date sorting part to work properly. Surely there are missing codes and other problems.
What am I missing?
<html lang="en">
<head>
<title>List</title>
<script src="jquery-3.3.1.min.js"></script>
</head>
<body>
<h1 align="center">List</h1>
</br>
</br>
</br>
<center> <p class="search_input">
<form method="post" action="#">
<input type="date" name="dateFrom"> <input type="date" name="dateTo">
<input type="submit" name="range" id="range" class="btn-info" />
</form>
</center>
<table align="center" cellspacing="0" cellpadding="0">
<thead class="fixedthead">
<th width="120px" style="text-align: center; color: navy">Name</th>
<th width="120px" style="text-align: center; color: navy">Description</th>
<th width="120px" style="text-align: center; color: navy">Date</th>
<th width="120px" style="text-align: center; color: navy">Open</th>
</thead>
<?php
//retrieve content via data picker range
$dateFrom = $_POST['dateFrom'];
$dateTo = $_POST['dateTo'];
$conn = mysqli_connect("localhost", "root", "", "order");
// get results from database
$result = mysqli_query($conn, "SELECT * FROM order.item WHERE date BETWEEN '$dateFrom' AND '$dateTo' ", MYSQLI_USE_RESULT)
or die(mysqli_error($conn));
while($row = mysqli_fetch_array( $result )) {
?>
<tbody>
<tr>
<td width="120px" style="text-align: center"><?php echo $row['name']; ?></td>
<td width="120px" style="text-align: center"><?php echo $row['description']; ?></td>
<td width="120px" style="text-align: center"><?php echo $row['date']; ?></td>
<td width="120px"><a href = "download.php?id=<?php echo $row['id']; ?>" style='text-decoration:none;'><button>View</button></a></td>
</tr>
</tbody>
</table><br><br><br>
<?php
}
?>
</body>
</html>
Any help is appreciated. Thanks guys!

Knowing nothing about your database...
$last_five_days = time() - 432000;
$sql = "SELECT * FROM order.item WHERE order.date > ".$last_five_days." ORDER BY order.date ASC";
If this answer doesn't help, then you need to provide us with more information about what you mean by "date picker range."
ORDER BY {column} ASC|DESC orders the rows by the indicated column. It defaults to ASC.
I'm using the WHERE clause to set up your last-five-days requirement. I'm assuming order.date (whatever you've actually named that) is storing a simple UNIX timestamp.

Related

Print html table in pdf , many pdf to be generated

I have a page that is generated using data from a database into a table format.
Each result of a student is displayed on a page so that when printed on a printer, each result is printed on a page. I need now to generate a pdf for each student. How can I add code to generate the pdf, using the name of the student?
Thanks in advance.
<body>
<?php
$grade=7;
require 'GetStudentNames.php';
while ($row1 = $result1->fetch_assoc()) { ?>
<P style="page-break-before: always">
<table margin-top="70px" width="100%">
<tr>
<td rowspan="2"><img src="logo.PNG" alt="logo"></td>
<td style="text-align:center" rowspan="1">
<h2>xyz School</h2>
</td>
<tr>
<td rowspan="1" style="text-align: center; ">
<h3 style="text-decoration: underline ;text-align:center">Student Report Sheet - First Term 2021</h3>
</td>
</tr>
</table>
<?php echo '<br><table width="100%"><th width="50%">Student Name: '.#$row1["name"].' '.#$row1["firstname"].'</th>';
echo '</td>';
require 'db.php';
$sql = "select subject from results where SN = '".#$row1["sn"]."' order by listorder";
$result = $conn->query($sql);
$count=1;
while ($row = $result->fetch_assoc()) {
echo '<tr class="oneSubj"><td class="oneSubj">'. $count.'. '.#$row["subject"].'</td>
</tr>';
$count++;
}?>
</table>
<br>
<table style="width: 100%">
<th style="width:25%">Absences..................</th>
<br>
<h5 style="text-decoration: underline ;text-align:center">Special Mention</h5>
</table>
<?php }?>
</body>

Passing value from html table with hyperlink to database query on next page

Below is my code for two pages. Dashboard.php has an html table filled with 7 columns populated by my database. All are populated without any problem. In an 8th column, I have a hyperlink that says 'View'. When clicked, I want that hyper link to use the value in the serialNumber column and then when it opens up the Display.php page, I need my html tables to be filled by database values based on that serial number.
My query in display.php is meant to get the stageID from my staging table that corresponds with the serialNumber in the row where the hyperlink was clicked.
i.e., if the user hits 'View' on the row with 988809 as the serialNumber, the query should match that serial number in my staging table and select everything with that stageID and then fill my tables.
The debug statements for $_GET do echo the correct serialNumber from the line/link chosen, but on display.PHP, I only get my echo statements, no tables or data. How can I change my hyperlink or query to fix this?
Dashboard.PHP
<?php
include 'connectionDB.php';
$query1 = "SELECT * FROM staging;";
$result1 = mysqli_query($connect,$query1);
?>
<div class="dashboardTable">
<table style="border: 1px solid black;">
<tr>
<th>Work Order Packet</th>
<th>Work Order Number</th>
<th>Date</th>
<th>Utility</th>
<th>Service Name</th>
<th>Address</th>
<th>Serial No.</th>
</tr>
<?php
while($row = mysqli_fetch_array($result1)){
?>
<tr>
<td><? echo $row['workOrderPacket'];?> </td>
<td><? echo $row['workOrderNum'];?> </td>
<td><? echo $row['date'];?> </td>
<td><? echo $row['utility'];?> </td>
<td><? echo $row['serviceName'];?> </td>
<td><? echo $row['address'];?> </td>
<td><? echo $row['serialNumber'];?> </td>
<td>view</td>
</tr>
<?}?>
</table>
</div>
Display.php
<?php
if(isset($_GET['serialNumber']))
{
$query1 = "SELECT * FROM staging WHERE stageID = ".$_GET['serialNumber'].";";
$result1 = mysqli_query($connect,$query1);
while($row = mysqli_fetch_array($result1)){
?>
<div class="container">
<!--Title Line-->
<DIV class="title">
<h3>REPORT TITLE</h3>
</DIV>
<div class="TitleContainer" style="width: 100%;">
<!--Column 1 for header info-->
<DIV class="headerCol1">
<table style=" float: left; border:none;
border-collapse:collapse;">
<tr style="border: none;">
<th style="border: none; text-align: left;">Account:</th>
<th style="border: none; text-align: right;"><? echo $row['accountNum'];?> </th>
</tr>
<tr style="border: none;">
<td style="border: none; text-align: left;">Date/Time:</td>
<td style="border: none; text-align: right;"><? echo $row['date'];?>, <?echo $row['timeTested'];?> </td>
</tr>
1), use the htmlspecialcharts() method to avoid any problems of infiltration of your database -> http://php.net/manual/en/function.htmlspecialchars.php
2), If I understands well it is the condition with the ISSET which does not work well? If that's it it is strange... Try maybe with
if(!empty($_GET['serialNumber']))
{
...

How to create a link through to database entry

I have a page that shows the responses from my form (in a webpage). At the moment, all the responses sit on one page, one after the other. I would like the page to show a list of all the dates submitted (date_submitted) and ID number and when that is clicked it takes us to a page with that particular response (i.e. the full response). How do I create a link that will take it to this record?
This is my code for that page:
<?php error_reporting(E_ALL); ?>
<head>
<link rel="stylesheet" type="text/css" href="surveystyle.css" media="all" />
</head>
<body>
<div id="container2">
<div class="logo-header"><img src="images/logo.jpg" alt="" width="205" height="119" /></div>
<h2> Guest Questionnaire Responses </h2>
<?php
try {
$handler = new PDO('mysql:host=localhost;dbname=***', '***', '***');
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo $e->getMessage();
die();
}
class guestquestionnaireEntry
{
public $id, $date_submitted, $choice, $expectations, $res, $res_information, $res_staff, $further_comments1,
$entry;
public function __construct()
{
$this->entry = "ID
<tr style='text-align: left; font:arial;'><td><h3>Date Submitted: {$this->date_submitted}</h3></td></tr>
<BR>
<table border='1' align='center'>
<tr style='background: #566890; font-size: 8pt; font-weight: bold; color: #fff;'>
<td colspan='3'>Prior to Arrival</td>
</tr>
<tr style='font-size: 8pt;'>
<td width='60%'>What made you choose us for your recent stay? </td>
<td width='40%' colspan='2'>{$this->choice}</td>
</tr>
<tr style='font-size: 8pt;'>
<td>Did our hotel meet your expectations as advertised? If no, please state why: </td>
<td width='40%' colspan='2'>{$this->expectations}</td>
</tr>
<tr style='background: #566890; font-size: 8pt; font-weight: bold; color: #fff;'>
<td colspan='3'>Making your Reservation</td>
</tr>
<BR>
<tr style='font-size: 8pt;'>
<td>Ease of making your reservation: </td>
<td width='40%'>$img</td>
<td width='5%'>{$this->res}</td>
</tr>
<BR>
<tr style='font-size: 8pt;'>
<td>Hotel information offered: </td>
<td width='40%'>$img2</td>
<td width='5%'>{$this->res_information}</td>
</tr>
<BR>
<tr style='font-size: 8pt;'>
<td>Warmth and friendliness of staff: </td>
<td width='40%'>$img3</td>
<td width='5%'>{$this->res_staff}</td>
</tr>
<BR>
<tr style='font-size: 8pt;'>
<td colspan='3'>Further Comments: </BR></BR>{$this->further_comments1}</td>
</tr>
<BR>
</table>";
}
}
// Checks if the submitted is a number. If so, isolates the ID and adds "where" clause
$id = (!empty($_GET['ID']) && is_numeric($_GET['ID']))? " where ID = '".$_GET['ID']."'" : "";
// Add the $id to the end of the string
// A single call would be SELECT * FROM guestquestionnaire where ID = '1'
$query = $handler->query("SELECT * FROM guestquestionnaire{$id}");
$query->setFetchMode(PDO::FETCH_CLASS, 'guestquestionnaireEntry');
while($r = $query->fetch()) {
echo $r->entry, '<br>';
}
?>
</div>
</body>
You need to do a where in your sql:
HTML Button:
<!-- Use whatever here to click on that triggers the ID reload -->
ID
SQL Query:
// Checks if the submitted is a number. If so, isolates the ID and adds "where" clause
$id = (!empty($_GET['ID']) && is_numeric($_GET['ID']))? " where ID = '".$_GET['ID']."'" : "";
// Add the $id to the end of the string
// A single call would be SELECT * FROM guestquestionnaire where ID = '1'
$query = $handler->query("SELECT * FROM guestquestionnaire{$id}");
$query->setFetchMode(PDO::FETCH_CLASS, 'guestquestionnaireEntry');
EDIT:
// Should be something like this
public function __construct()
{
$this->entry = "
ID...etc.

Displaying submitted form results as a list of URL links

First of all my PHP skills are kinda limited, Hence my question to you here.
I have built a fairly complex form with multiple inputs(text boxs and drop downs) which are stored in a MYsql DB. When the form is submitted it displays on a new page as completed reports. These completed reports display one under the next every time the form is submitted. My question is, How can i get the reports displayed to show as a list of links to the individual reports rather then a list of complete reports.
I hope i've explained the situation well enough.
Code snippit from viewpage.php
<html>
<head>
<body>
<?php
mysql_connect("localhost","user","passwrd");
mysql_select_db("dtbase");
$order = "SELECT * FROM jobrequest" ;
$result = mysql_query($order);
while ($row=mysql_fetch_array($result)){
?>
<link rel="stylesheet" href="css/style.css" type="text/css" />
</head>
<body>
<div style="padding:15px 0px 0px 100px;">
<table cellpadding="0" cellspacing="0" border="0" style="vertical-align:middle;width: 1139px; background-color:#213568; height:36px;">
<tr>
<td class="topbar">Client Request Form</td>
<td style="width:900px;"></td>
<td class="topbar"><a style="color:#ffffff;" href="logout.php?logout">Logout</a></td>
</tr>
</table>
<div class="main-wrap">
<div class="content">
<table cellpadding="0" cellspacing="0" border="0" style="width: 1137px; background-color:#ffffff; height:5px;">
<tr>
<td></td>
</tr>
</table>
<table cellpadding="0" cellspacing="0" border="0" >
<tr>
<td style="vertical-align:top; width:5px;"></td>
<td style="vertical-align:top;"><?php include("includes/clientchoices.php"); ?></td>
<td style="vertical-align:top; padding:0px 5px 15px 5px;">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="vertical-align:top; width:1002px;"> <h1> Dashboard</h1></td>
</tr>
<tr>
<td style="vertical-align:top; background-color:#f5f5f5;"><h2>Job Request Form</h2></td>
</tr>
<tr>
<td style="vertical-align:top; background-color:#ffffff; height:5px;"> </td>
</tr>
<tr>
<td>
<div class="form">
<table cellspacing="0" cellpadding="0" border="0" style="width:998px">
<tr>
<td style="width:1002px; border:solid 1px #000000; padding:10px 0px 10px 0px;"><center><img src="../../images/spectra_logotop.jpg" alt="Spectra" title="Spectra" width="735" height="120" style="padding:5px;"></center>
</td>
</tr>
<tr>
<td>
<div style="padding:10px 0px 10px 0px;">
<table cellpadding="0" cellspacing="0">
<tr>
<td class="headingsa">Project Leader:</td><td class="answersa"><div class= "typesectiona"><?php echo ($row['project_leader'] ); ?></div></td>
<td class="headingsb">Contact Number:</td><td class="answersb"><div class= "typesectionb"><?php echo ($row['contact_number'] ); ?></div></td>
<td class="headingsc">Company Details:</td><td class="answersc"><div class= "typesectionc"><?php echo ($row['company_details'] ); ?></div></td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td>
<table cellpadding="0" cellspacing="0">
<tr>
<td class="headings5">Contact Person On Site:</td><td class="answers5"><div class= "typesection5"><?php echo ($row['contactperson_onsite'] ); ?></div></td>
<td class="headings6">Contact Details:</td><td class="answers6"><div class= "typesection6"><?php echo ($row['contact_no'] ); ?></div></td>
<td class="headings7">Date:</td><td class="answers7"><div class= "typesection7"><?php echo ($row['date'] ); ?></div></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table cellpadding="0" cellspacing="0">
<tr>
<td class="headings1">Job/Order Number:</td><td class="answers1"><div class= "typesection1"><?php echo ($row['job_order_number'] ); ?></div></td>
<td class="headings2">Document Number:</td><td class="answers2"><div class= "typesection2"><?php echo ($row['doument_number'] ); ?></div></td>
<td class="headings3">QCP:</td><td class="answers3"><div class= "typesection3"><?php echo ($row['qcp'] ); ?></div></td>
<td class="headings4">Page No:</td><td class="answers4"><div class= "typesection4"><?php echo ($row['pageno'] ); ?></div></td>
</tr>
</table>
</td>
</tr>
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="15px"></td>
<td><a class="othersubmitsLink" href="actionpdf.php">Email to Spectra</a></td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</div>
</div>
<?php
}
?>
</body>
</html>
You will need a separate PHP script for displaying a report based on a supplied ID. This separate script would look something like this:
Using mysqli
<?php
$conn = new mysqli("localhost", "user", "passwrd", "dtbase");
$jrQry = $conn->prepare("SELECT * FROM jobrequest WHERE jobrequest_id = ?");
$jrQry->bind_param('i', $_GET['jobrequest_id']);
$jrQry->execute();
$jobrequestResult = $jrQry->get_result();
$jobrequest = $jobrequestResult->fetch_assoc();
// At this point, $jobrequest will contain the jobrequest record you want to display.
?>
<!-- HTML FOR REPORT GOES HERE, USING $jobrequest VARIABLE TO SHOW THE DATA -->
Note that I have used mysqli in this example, if this is unsuitable you can use the old-style mysql commands, but for many many reasons (security chief among them) I would strongly suggest against this.
Using mysql
<?php
mysql_connect("localhost","user","passwrd");
mysql_select_db("dtbase");
$order = "SELECT * FROM jobrequest WHERE jobrequest_id = " . (int)$_GET['jobrequest_id'];
$result = mysql_query($order);
$jobrequest = mysql_fetch_array($result);
// At this point, $jobrequest will contain the jobrequest record you want to display.
?>
<!-- HTML FOR REPORT GOES HERE, USING $jobrequest VARIABLE TO SHOW THE DATA -->
Save this page as "viewjobrequest.php" and you will be able to load a given job request's report by supplying the jobrequest ID as a parameter in the URL, like so:
http://address_of_site/viewjobrequest.php?jobrequest_id=X
Now you can automatically generate a list of links to these pages by looking up your complete set of jobrequests and iterating over them, but instead on outputting a full report, just output a link:
Using mysqli
<?php
$conn = new mysqli("localhost", "user", "passwrd", "dtbase");
$jrQry = $conn->prepare("SELECT * FROM jobrequest WHERE jobrequest_id = ?");
$jrQry->bind_param('i', $_GET['jobrequest_id']);
$jrQry->execute();
$jobrequestResult = $jrQry->get_result();
?>
<ul>
<?php
while ($jobrequest = $jobrequestResult->fetch_assoc())
{
?>
<li>
<a href="viewjobrequest.php?jobrequest_id=<?php echo $jobrequest['jobrequest_id']; ?>">
View job request #<?php echo $jobrequest['jobrequest_id']; ?>
</a>
</li>
<?php
}
?>
</ul>
Using mysql
<?php
mysql_connect("localhost","user","passwrd");
mysql_select_db("dtbase");
$order = "SELECT * FROM jobrequest WHERE jobrequest_id = " . (int)$_GET['jobrequest_id'];
$result = mysql_query($order);
?>
<ul>
<?php
while ($jobrequest = mysql_fetch_assoc($result))
{
?>
<li>
<a href="viewjobrequest.php?jobrequest_id=<?php echo $jobrequest['jobrequest_id']; ?>">
View job request #<?php echo $jobrequest['jobrequest_id']; ?>
</a>
</li>
<?php
}
?>
</ul>
Note: I have deliberately omitted large portions of your HTML, you can add as much extra HTML as you need, I've just offered the bare bones to get you started.
BY looking at your code, I can say, it's not problem with PHP, it's more like HOW you are showing the result after fetching from database,
you are running loop, started from here
<?php while ($row=mysql_fetch_array($result)){ ?>
and ends here
<?php}?>
So everything inside loop or in easy words inside these brackets {} repeating again and again with new row of result fetched from database, if you take view source of viewpage.php you will see that style css file <link rel="stylesheet" href="css/style.css" type="text/css" /> repeating, Imagine if you are fetching 10 rows of result, you are also loading css file 10 times.
So the answer (most probably the solution) of your question is;
Your mysql query
<?php
mysql_connect("localhost","user","passwrd");
mysql_select_db("dtbase");
$order = "SELECT * FROM jobrequest" ;
$result = mysql_query($order);
$totalrows = mysql_num_rows($result); //Check Total Number of Rows To Check if Data Exist or Not
?>
and then in your HTML, First check if there are any rows exist in database which match your WHERE clause in mysql query.
//Set an if else statement here so incase if no data exist.
<?php if($totalrows > 0) {
//If row(s) exist run your while loop here
<?php while ($row=mysql_fetch_array($result)){ ?>
//Show the result here from database
//Close your loop
<?php } ?>
//Close your if condition
<?php } else { ?>
//Display a message here if there is no data to show
//Close your else bracket
<?php } ?>
With above explaniation I adjusted your HTML to show the result in desired way;
<table cellspacing="0" cellpadding="0" border="0" style="width:998px">
<tr>
<td style="width:1002px; border:solid 1px #000000; padding:10px 0px 10px 0px;"><center><img src="../../images/spectra_logotop.jpg" alt="Spectra" title="Spectra" width="735" height="120" style="padding:5px;"></center></td>
</tr>
<tr>
<td>
<?php if($totalrows > 0) {
<div style="padding:10px 0px 10px 0px;">
<table cellpadding="0" cellspacing="0">
<tr>
<td class="headingsa">Link to Report</td>
</tr>
<?php while ($row=mysql_fetch_array($result)){ ?>
<tr>
//You have to replace `nameoffile.php` with file in which you want to display report and correct this (as i assumed it) if it's wrong `$row['id']`
<td>Open Report</td>
</tr>
<?php } ?>
</table>
</div>
<?php } else { ?>
<div style="padding:10px 0px 10px 0px;">
<table cellpadding="0" cellspacing="0">
<tr>
<td>There is No Result To Show</td>
</tr>
</table>
</div>
<?php } ?>
</td>
</tr>
</table>
Note: MySQL will soon be deprecated, consider start using MYSQLi or PDO

Unable to pass value of a row to another page in AJAX

I am having a table with 7 columns(Complaintid id,name,school name etc...) and also have button. When I click the button I need to pass the complaintid of that row to another page. pls help me find a way to pass the value.
<html>
<body>
<?php
include '../library/dbconnect.php';
$query="SELECT * FROM Complaint_register WHERE status=1 ORDER BY entrydate ";
$result=mysql_query($query) or die("Selection query of
Complaint_register is Error ".mysql_error());
$num = mysql_numrows($result);
$i=0;
$j=1;
?>
<form action="" name="frmcomplaint" id="frmcomplaint" method="post">
<table border="1" style="border-color: #FFFFFF;" >
<tr>
<th style="color: #FF0000">Sl. No</th>
<th style="color: #FF0000">Complaint Id</th>
<th style="color: #FF0000">Date</th>
<th style="color: #FF0000; width:200px;" >Name Of student</th>
<th style="color: #FF0000">District</th>
<th style="color: #FF0000">School Name</th>
<th style="color: #FF0000">Standard with </th>
<th style="color: #FF0000; width:200px;">Complaint</th>
</tr>
<?php
while($row=mysql_fetch_array($result))
{
$date1=explode('-', $row[$i+2]);
$entrydate=$date1[2]."-".$date1[1]."-".$date1[0];
$job_id=$row[$i+1];
?>
<tr>
<td style="color: #000000"><?php echo $j;?></td>
<td style="color: #000000"><?php echo $row[complain_Id]; ?> </td>
<td style="color: #000000"><?php echo $entrydate;?></td>
<td style="color: #000000" ><?php echo $row[studname];?></td>
<td style="color: #000000"><?php echo $row[District];?></td>
<td style="color: #000000"><?php echo $row[School_name] ;?></td>
<td style="color: #000000"><?php
echo $row[Standard]."-".$row[Division];?></td>
<td id="disp" ><?php echo $row[Complaint];?></td>
<td id="button" name="viewbutton" >
</td>
</tr>
<?php
$button++;
$j=$j+1;
}
?>
</table>
</form>
</body>
</html>
Instead of using
<td id="button" name="viewbutton" >
</td>
use,
<td>
<input type="button" onclick="functionname(<?php echo $row[complain_Id]; ?>)">
</td>
and add a javascript function
<script>
function functioname(param)
{
window.location="redirectionpage?id="+param;
}
</script>
This will redirect to the page redirectionpage with the id.
Few Loop holes in your current attempt.
Strictly use mysqli or PDO instead of mysql as it's been deprecated.
It's mysql_num_rows not mysql_numrows.
Now coming to your problem, you can solve this by using jQuery ajax.
$("table").on("click","#button",function(){
var cid = $("#disp").text();
$.ajax({
type: "GET",
url: "anotherpage.php",
data: { complainid: cid},
success: function(data) {
alert(data) //To check if response is success
}
});
});
anotherpage.php:
<?php
$cid = $_GET['complainid'];
?>
You can use a link with complain id like complaints.php?complainid=echo your value here
<td id="button" name="viewbutton" >
view complaint
</td>
and you can style that link like a button if needed.
In complaints.php page you can take the details of that complain with the id you passed.

Categories