How to fix between date to date error in php - php

When I try to Search Between two dates all are working display on reports-details.php but When I try to fresh reload on reports-details.php show this error Warning:
Invalid argument supplied for foreach() in C:\xampp\htdocs\parking
reservation\admin\reports-details.php on line 52
reports.php content is:
<form action="reports-details.php" method="POST">
<label>From Date</label>
<input type="date" name="start_date">
<label>To Date</label>
<input type="date" name="end_date">
<button name="search">Search</button>
</form>
back-end-reports.php
<?php
include 'config.php';
class report extends Connection{
public function managereport(){
if (isset($_POST['search'])) {
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];
$sqlselect = "SELECT * FROM tbl_customers WHERE test_date BETWEEN '$start_date' AND '$end_date' ORDER BY test_date";
$result = $this->conn()->query($sqlselect);
$result->execute();
return $result->fetchAll();
}
}
}
$new_vehicle = new report();
$new_vehicle->managereport();
?>
reports-details.php
<?php
include '../back-end/back-end-reports.php';
$result = new report();
$query = $result->managereport();
?>
<?php $id = 1; foreach($query as $row) { ?>
<tbody>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $row['serial']; ?></td>
<td><?php echo $row['fullname']; ?></td>
<td><?php echo $row['num_plate']; ?></td>
<td>View | <a class="text-dark" href="print.php">Print</a></td>
</tr>
</tbody>
<?php $id++; } ?>

Your issue here is the if statement in your function
if (isset($_POST['search'])) {
When you refresh your page there is no post data so the managereport returns nothing. The value of $query is therefore empty and so you cant iterate over it in the foreach loop.
My suggestion would be that your mangerreport() should return an empty array in the situation where there is no post data i.e
public function managereport(){
if (isset($_POST['search'])) {
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];
$sqlselect = "SELECT * FROM tbl_customers WHERE test_date BETWEEN '$start_date' AND '$end_date' ORDER BY test_date";
$result = $this->conn()->query($sqlselect);
$result->execute();
return $result->fetchAll();
} else {
return array();
}
}

<?php $id = 1; foreach($query as $row) { ?>
When using the foreach loop, you must first judge the array $query, otherwise you will be prompted to report an error like this Invalid argument supplied for foreach() , you can add judgment before the loop,
<?php $id = 1; if (is_array($query) && count($query)) {
foreach($query as $row) { ?>
<tbody>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $row['serial']; ?></td>
<td><?php echo $row['fullname']; ?></td>
<td><?php echo $row['num_plate']; ?></td>
<td>View | <a class="text-dark" href="print.php">Print</a></td>
</tr>
</tbody>
<?php $id++; } } ?>

Related

Calculate total amount of rows for specific date

I have this working.
$queryPR = "SELECT * FROM `raport` WHERE DATE(`timestamp`) = CURDATE() AND (lokacioni = 'PR')";
I need the total amount of rows calculated for specific date.
I need also to the SUM('pagesat') somehow but I have no idea how.
Find the whole code below.
I am trying to build something simple to show the income and outcome for 3 locations that we have.
<table class="responsive-table">
<thead>
<tr>
<th>Vetura</th>
<th>Targa</th>
<th>Dite</th>
<th>Pagesa</th>
<th>Cmimi Ditore</th>
<th>Vazhdim</th>
<th>Te Tjera</th>
<th>Shpenzime</th>
<th>Komente</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM `raport` WHERE DATE(`timestamp`) = CURDATE() AND (lokacioni = 'FR')";
$stmt = $DBcon->prepare( $query );
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
?>
<tr>
<td><?php echo $row['vetura']; ?></td>
<td><?php echo $row['targa']; ?></td>
<td><?php echo $row['dite']; ?></td>
<td><?php echo $row['pagesa']; ?>€</td>
<td><?php echo $row['cmimiditore']; ?>€</td>
<td><?php echo $row['vazhdim']; ?>€</td>
<td><?php echo $row['tetjera']; ?>€</td>
<td><?php echo $row['shpenzime']; ?>€</td>
<td><?php echo $row['koment']; ?></td>
</tr>
<?php
$pagesa = $row['pagesa'];
$tetjera = $row['tetjera'];
$vazhdime = $row['vazhdim'];
$shpenzime = $row['shpenzime'];
}?>
</tbody>
</table>
<?php
$totali = ($pagesa + $vazhdime + $tetjera);
$gtotal = ($totali - $shpenzime);
?>
<div class="card grey">
<hgroup class="totalDitorRaport">
<h2>Total</h2>
<h1><?php echo $gtotal; ?>€</h1>
</hgroup>
</div>
try this
get the sum of the row pagesa in the loop
$queryPR = "SELECT * FROM `raport` WHERE DATE(`timestamp`) = CURDATE() AND (lokacioni = 'PR')";
$stmt = $DBcon->prepare( $query );
$stmt->execute();
$sum = 0;
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
$sum += $row['pagesa'];
}
echo $sum;

inserting array using button php

hey guys i've been trying to insert an bunch of data from array and variable into database using button , but it can't be inserted and the database still empty , is there anyone who know where is my mistake ?
here is my code
<?php
include 'koneksi.php';
$arr_nf = array(1,0.334,-0.334,-1);
$arr_ef = array(0,0.333,0.667,1);
$arr_lf = array(1,0.667,0.333,0);
$kelas = "80 keatas" ;
$iduser = "8" ;
$query = mysql_query(" select id_alternatif from tb_alternatif where kelas = '".$kelas."' order by nama "); // run query
$idalter = array();
while($row = mysql_fetch_assoc($query)){
$idalter[] = $row;
}
////////////////////////////////////////// RANGKING LF
foreach ($arr_lf as $val){ // net flow kali 1000
$arr_lfx[]= $val * 1000 ;
}
$int_lfx = array_map( // rubah jd integer
function($value) { return (int)$value; },
$arr_lfx );
$lfx_copy = $int_lfx;
rsort($lfx_copy);
$lfx_copy = array_flip($lfx_copy);
foreach($int_lfx as $val){
$rangkinglf[] = ($lfx_copy[$val]+1);
}
//////////////////////////////////////////
$jumlah_atlet = count($arr_nf);
if (isset($_POST['submit'])){
for($x=0; $x<$jumlah_atlet ;$x++){
mysql_query(" INSERT INTO tb_histori(id_histori, id_user, id_alternatif, kelas, nilai, rangking, waktu) values(NULL, '$iduser', '".$idalter[$x]['id_alternatif']."', '$kelas', '$arr_nf[$x]', '$rangkingnf[$x]', now());") or die(mysql_error()); };
};
print_r($jumlah_atlet);
?>
<html>
<head>
<title> insert data array using only button </title>
</head>
<body>
<form action="" method="post">
<input name="submit" type="button" value="Print & Save" onClick="" />
</form>
</body>
<?php
$data = mysql_query("select * from tb_histori");
$no = 1;
while($d = mysql_fetch_array($data)){
?>
<tr>
<td><?php echo $no++; ?></td>
<td><?php echo $d['id_user']; ?></td>
<td><?php echo $d['id_alternatif']; ?></td>
<td><?php echo $d['kelas']; ?></td>
<td><?php echo $d['nilai']; ?></td>
<td><?php echo $d['rangking']; ?></td>
<td><?php echo $d['waktu']; ?></td>
</tr>
<br>
<?php } ?>
<br>
</html>

HTML Select Option and submit to fetch data from PHP PDO MySql

I like to fetch data from MySQL by Select Month and year in HTML Select Option, When i submit view, Table will be filtered. Else all data will be showing. But my code showing error.
Notice: Undefined variable: user in C:\xampp\htdocs\testSelect.php on line 85
Fatal error: Call to a member function getAllUser() on null in C:\xampp\htdocs\testSelect.php on line 85
How can solve this error. Please help me.
here my code:
<?php
class DatabaseConnection{
public function __construct(){
global $pdo;
try{
$pdo=new PDO('mysql:host=localhost;dbname=test','root','');
}catch(PDOException $e){
exit('Database Error');
}
}
}
?>
<?php echo "Select :   " ; ?>
<?php
$monthArray = range(1, 12);
?>
<select name="month">
<option value="">Select Month</option>
<?php
foreach ($monthArray as $month) {
// padding the month with extra zero
$monthPadding = str_pad($month, 2, "0", STR_PAD_LEFT);
// you can use whatever year you want
// you can use 'M' or 'F' as per your month formatting preference
$fdate = date("F", strtotime("2015-$monthPadding-01"));
echo '<option value="'.$monthPadding.'">'.$fdate.'</option>';
}
?>
</select>
  
<?php
// set start and end year range
$yearArray = range(2000, 2050);
?>
<!-- displaying the dropdown list -->
<select name="year">
<option value="s">Select Year</option>
<?php
foreach ($yearArray as $year) {
// if you want to select a particular year
$selected = ($year == 's') ? 'selected' : '';
echo '<option '.$selected.' value="'.$year.'">'.$year.'</option>';
}
?>
</select>
<form method="post">
<input type="submit" name="view" value="View" />
</form>
<?php
function getAllUserReport($username){
global $pdo;
$query=$pdo->prepare("SELECT * FROM user name= $username ");
$query->execute(array($username));
return $query->fetchALL(PDO::FETCH_ASSOC);
}
function filterTable(){
global $pdo;
$query=$pdo->prepare("Select from user where year(date) = $year and month(date) = $month ");
$query->execute();
return $query->fetchALL(PDO::FETCH_ASSOC);
}
?>
<table class="tbl_one">
<tr>
<th>Serial</th>
<th>Name</th>
<th>Age</th>
<th>BirthDate</th>
</tr>
<?php
$i=0;
$alluser = $user->getAllUser($username);
if(isset($_POST['view'])){
$alluser = $user->filterTable();
foreach($alluser as $user){
$i++;
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $user['name']; ?></td>
<td><?php echo $user['age']; ?></td>
<td><?php echo $user['birthdate']; ?></td>
</tr>
<?php } ?>
<?php
}else{
foreach($alluser as $user){
$i++;
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $user['name']; ?></td>
<td><?php echo $user['age']; ?></td>
<td><?php echo $user['birthdate']; ?></td>
</tr>
<?php } } ?>
</table>
Notice: Undefined variable: user in C:\xampp\htdocs\testSelect.php on line 85
Fatal error: Call to a member function getAllUser() on null in C:\xampp\htdocs\testSelect.php on line 85
The error says, that your $user variable is not defined. So you cant call a method on it.

Query works in phpMyAdmin but not PHP

In phpMyAdmin I have a simple query:
SELECT * FROM `recent` WHERE datediff(now(), `timestamp`) > 1
BUT when I try to do this in my clear_recent.php:
<?php $result = $conn->query("SELECT * FROM `recent` WHERE datediff(now(), `timestamp`) > 1"); ?>
<?php foreach ($result->fetch_assoc() as $row): ?>
<?php while($row = $result->fetch_assoc()) { ?>
<tr>
<td><?php echo($title = $row["id"]); ?></td>
<td><?php echo($title = $row["pid"]); ?></td>
<td><?php echo($title = $row["user_id"]); ?></td>
<td><?php echo($title = $row["timestamp"]); ?></td>
</tr>
<?php } ?>
<?php endforeach; ?>
I get an error:
Invalid argument supplied for foreach() in /database/chron/clear_recent.php
Cannot for the life of my figure out what's wrong!!!! Please help!
You should check if the query returns any row. try the following code.
<?php
$result = $conn->query("SELECT * FROM `recent` WHERE datediff(now(), `timestamp`) > 1");
$rows = $result->fetch_assoc();
//check if some rows available.
if (count($rows) > 0) {
foreach ($rows as $row) {
while ($row = $rows) {
?>
<tr>
<td><?php echo($title = $row["id"]); ?></td>
<td><?php echo($title = $row["pid"]); ?></td>
<td><?php echo($title = $row["user_id"]); ?></td>
<td><?php echo($title = $row["timestamp"]); ?></td>
</tr>
<?php
}
}
}
?>
additional question: why you use while loop under the foreach loop? is it right?
You are getting this error because no rows return from this query.

PHP Pagination for user?

I've tried to make a pagination with PHP, but it seems to not work. I want to limit my data which showing in each page, I have this code and it uses PHP code:
showUsers.php
<?php
$query = mysql_query("select * from users");
while ($data = mysql_fetch_array($query)) {
?>
<td><?php echo $data['no_peg']; ?></td>
<td>
<?php
echo $data['username'];
//previlage admin
if ($_SESSION['role'] == 'admin') {
?>
<div class="row-actions">
Edit
<?php if ($data['role'] != 'admin') {?>
| Delete
<?php } ?>
</div>
<?php } ?>
</td>
<td><?php echo $data['fullname']; ?></td>
<td><?php echo $data['telephone']; ?></td>
<td><?php echo $data['email']; ?></td>
I want to show just 10 names per page, to avoid long scrolling, but how can it work?
Hi,if you want to make a pagination, you should add in a LIMIT clause into your queries, like this:
SELECT* FROM users LIMIT 0, 10
With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return.
So, what's the next?
You need to add a parameter to your query url specifies the number of page when you list the users.
Such as:
showUsers.php?page=1
Then, in your program, you can get parameter by this:
$page = isset($_GET['page']) ? $_GET['page'] : 1;
I hope it will help you, I'm new here.
<?php
$sqlCount = "select count(id_user) from users";
$rsCount = mysql_fetch_array(mysql_query($sqlCount));
$totalData = $rsCount[0];
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$limit = 10;
$start_from = $limit * ($page - 1);
$sql_limit = "SELECT * FROM users limit $start_from, $limit";
$result = mysql_query($sql_limit);
while ($data = mysql_fetch_array($result)) {
?>
<td><?php echo $data['no_peg']; ?></td>
<td>
<?php
echo $data['username'];
//previlage admin
if ($_SESSION['role'] == 'admin') {
?>
<div class="row-actions">
Edit
<?php if ($data['role'] != 'admin') {?>
| Delete
<?php } ?>
</div>
<?php } ?>
</td>
<td><?php echo $data['fullname']; ?></td>
<td><?php echo $data['telephone']; ?></td>
<td><?php echo $data['email']; ?></td>
</tr>
<?php
}
?>
<?php
$totalPage = ceil($totalData / $limit);
echo 'Page : ';
for($i = 1; $i <= $totalPage; $i++){
if($page != $i){
echo '['.$i.'] ';
}else{
echo "[$i] ";
}
}
?>
Where is your pagination code? Your code just shows this query:
$query = mysql_query("select * from users");
But for basic pagination you need to set a LIMIT like so:
$query = mysql_query("select * from users LIMIT 0,10");
So that would only grab the first 10 items. And then—let’s say, on page 2 you could do this:
$query = mysql_query("select * from users LIMIT 11,10");
That would grab the next 10 items starting from item 11.
That’s the basic concept. But you have to code the logic for passing along pagination values & such.
<?php
if(is_int($_GET('pageNo'))) // getting the page number from the URL i.e script.php?pageNo=2
{
$query = mysql_query("select * from users limit ".$_GET['pageNo']." ,10");
while ($data = mysql_fetch_array($query)) {
?>
<td><?php echo $data['no_peg']; ?></td>
<td>
<?php
echo $data['username'];
//previlage admin
if ($_SESSION['role'] == 'admin') {
?>
<div class="row-actions">
Edit
<?php if ($data['role'] != 'admin') {?>
| Delete
<?php } ?>
</div>
<?php } ?>
</td>
<td><?php echo $data['fullname']; ?></td>
<td><?php echo $data['telephone']; ?></td>
<td><?php echo $data['email']; ?></td>
</tr>
<?php
}
} // not sure where the closing if should be you figure it out :P
?>

Categories