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
?>
Related
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++; } } ?>
public function get_orderProduct($loginuserID) {
$this->db->select('client_order.*,product.image_gallery,product.name');
$this->db->from('client_order');
$this->db->join('product','client_order.productID = product.productID','LEFT');
$this->db->where('client_order.clientID',$loginuserID);
$this->db->where('client_order.status',1);
$this->db->order_by('client_order.id','desc');
$sql = $this->db->get();
$result = $sql->result();
return $result;
}
<?php
if(count($result) > 0){
$i=1;
foreach($result as $row){
$img = explode(",", $row->image_gallery);
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $row->orderID; ?></td>
<td><?php echo $row->name; ?></td>
</tr>
<?php
$i++;
}
}else{
?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
No order available!
</div>
<?php
}
?>
I have two product with same orderID and I want to show same orderID product in single row. Now, Here it show in different different row. So, How can I show in single row? Please help me.
Thank You
To solve this, you have to create an empty array to store orderids.Before you print the orderid you have to search the array using in_array method, if orderid exists ignore printing it, otherwise print and push the orderID to the array using array_push method.
<?php
$ids = array();
$i=1;
foreach($result as $row){
$img = explode(",", $row->image_gallery);
?>
<tr>
<td><?php echo $i; ?></td>
<td>
<?php
if(!in_array($row->orderID,$ids))
{
echo $row->orderID;
array_push($ids,$row->orderID);
}
?>
</td>
<td><?php echo $row->name; ?></td>
</tr>
<?php
$i++;
}
?>
I am having problem to display the numbers of each rows. I am displaying a table with LIMIT of up to 20 rows. I can display the numbers in the while loop but when user click for next rows it will display the number back to 1. How can I display it as a continue number example, 1-20, 21-40, etc? Below are my codes:
<?php
//SQL AND LIMIT codes here
$x = '1';
if(mysqli_num_rows($result) > 0){
while($row=mysqli_fetch_assoc($result)){
$pr_id=$row['pr_id'];
?>
<tr>
<td align="center"><?php echo $x; //display the number ?></td>
<td><?php echo html_entity_decode($row['title']); ?></td>
<td align="center"><?php echo date("d/m/y",strtotime($row['submit_date'])); ?></td>
<td><?php echo $row['purchase_type']; ?></td>
</tr>
<?php
$x++;
}
}
mysqli_free_result($result);
mysqli_close($conn);
echo 'Next';
?>
As you mentioned $next is page number:
One solution is you can do:
$x = 1 + (($next -1 ) * 20);
So...
When $next = 1 (first page) $x = 1 + (0*20) = 1
When $next = 2 (second page) $x = 1 + (1*20) = 21
When $next = 3 (third page) $x = 1 + (2*20) = 41
So on...
Can you please use limit in your sql query and you use below code for getting limit.
$start = 1 + ((page number)*20);
$end = $start + 19;
Now you can set it in your sql query
$sql = "select * from table name where limit".$start.", ".$end;
try like this
<?php
if(isset($_REQUEST['pn'])){
$pn = $_REQUEST['pn'];
} else {
$pn = '1';
}
$x = $pn * 20; //place your no. of rows instead of 20
if(mysqli_num_rows($result) > 0){
while($row=mysqli_fetch_assoc($result)){
$pr_id=$row['pr_id'];
?>
<tr>
<td align="center"><?php echo $x; //display the number ?></td>
<td><?php echo html_entity_decode($row['title']); ?></td>
<td align="center"><?php echo date("d/m/y",strtotime($row['submit_date'])); ?></td>
<td><?php echo $row['purchase_type']; ?></td>
</tr>
<?php
$x++;
}
}
mysqli_free_result($result);
mysqli_close($conn);
echo 'Next';
?>
Here's my code sir:
<?php
session_start();
include 'include/db_config.php';
$result = $dbs->prepare("SELECT * FROM service_info ORDER BY id DESC");
/*$result->bindParam(':id', $_SESSION['id']);*/
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
$result2 = $dbs->prepare("SELECT *FROM customer_info ORDER BY id DESC");
$result2->execute();
for($j=0; $row2 = $result2->fetch();$j++){
?>
<tr class="record">
<td><?php echo $row2['firstname'];?></td>
<td><?php echo $row['no_guest']; ?></td>
<td><?php echo $row['type_service']; ?></td>
<td><?php echo $row['datepicker']; ?></td>
<td><?php echo $row['t_time']; ?></td>
<td> delete </td>
</tr>
<?php
}
}
?>
i didnt use a LEFT JOIN for displaying data's from 2 tables. i just want to do it in my own way . But my problem is it duplicates my data . before i inserted my second query its just 2 data's and now its 4 already. i just cant figure it out where is the duplication occurs.
Someone help me out please . Thanks in Advance.
You display data in second for, which is inside first for. So you get result count of customer_info table length*service_info length results. You should save info in arrays and then use only one for cycle. Eg.:
<?php
session_start();
include 'include/db_config.php';
$services = array();
$customers = array();
$result = $dbs->prepare("SELECT * FROM service_info ORDER BY id DESC");
/*$result->bindParam(':id', $_SESSION['id']);*/
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
$services[] = $row;
}
$result2 = $dbs->prepare("SELECT *FROM customer_info ORDER BY id DESC");
$result2->execute();
for($j=0; $row2 = $result2->fetch();$j++){
$customers[] = $row2;
}
foreach($services as $key => $service) {
?>
<tr class="record">
<td><?php echo $customers[$key]['firstname'];?></td>
<td><?php echo $service['no_guest']; ?></td>
<td><?php echo $service['type_service']; ?></td>
<td><?php echo $service['datepicker']; ?></td>
<td><?php echo $service['t_time']; ?></td>
<td> delete </td>
</tr>
<?php
}
?>
Btw, i suggest you use different approach, like joins
Hi i have table in mysql like this:
to show this by php like this:
now first i get and print all gradeid on top by this
$qry = "select * from grademaster";
$result= mysql_query($qry,$link);
$nro=mysql_num_rows($result);
while($row=mysql_fetch_array($result))
{
$gradeid = $row['gradeid'];
echo "<th> $gradeid </th>";
$grdid[] = $gradeid; ////////takes all grades in array
}
and then getting all details from the below table
and but i can't able to show records like on top how can i show this plz help
Just copy and paste it instead of your code, then run script in browser.
You'll see the table. Prettify it via CSS and you are done.
I've tested it on my local server and it seems to work.
$qry = "select * from grademaster";
$result = mysql_query($qry, $link);
$nro = mysql_num_rows($result);
$table = array();
$rowNum = 0;
while ($row = mysql_fetch_array($result)) {
$table[$rowNum]['ordid'] = $row['ordid'];
$table[$rowNum]['orddate'] = date('m/d/Y', strtotime($row['orddate']));
$table[$rowNum]['1001'] = intval($row['gradeid']) == 1001 ? $row['ordqty'] : '';
$table[$rowNum]['1002'] = intval($row['gradeid']) == 1002 ? $row['ordqty'] : '';
$table[$rowNum]['1003'] = intval($row['gradeid']) == 1003 ? $row['ordqty'] : '';
$table[$rowNum]['1004'] = intval($row['gradeid']) == 1004 ? $row['ordqty'] : '';
$table[$rowNum]['1005'] = intval($row['gradeid']) == 1005 ? $row['ordqty'] : '';
$rowNum++;
}
?>
<table>
<tr>
<th>ordid</th>
<th>orddate</th>
<th>1001</th>
<th>1002</th>
<th>1003</th>
<th>1004</th>
<th>1005</th>
</tr>
<?php foreach ($table as $row): ?>
<tr>
<td><?php echo $row['ordid']; ?></td>
<td><?php echo $row['orddate']; ?></td>
<td><?php echo $row['1001']; ?></td>
<td><?php echo $row['1002']; ?></td>
<td><?php echo $row['1003']; ?></td>
<td><?php echo $row['1004']; ?></td>
<td><?php echo $row['1005']; ?></td>
</tr>
<?php endforeach; ?>
</table>