Table pagination, content is not showing in php - php

enter image description hereThe problem is the content of my table is not showing. It is counting and show the the equal row from my database but no content is showing. Can you please help me?
index.php
<?php
include('db.php');
$limit = 2;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $limit;
$sql = "SELECT * FROM sales_order ORDER BY `so_id` ASC LIMIT $start_from, $limit";
$rs_result = mysqli_query($conn, $sql);
?>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>title</th>
<th>body</th>
</tr>
<thead>
<tbody>
<?php
while ($row = mysqli_fetch_assoc($rs_result)) {
?>
<tr>
<td><? echo $row["so_customer"]; ?></td>
<td><? echo $row["so_address"]; ?></td>
</tr>
<?php
};
?>
</tbody>
</table>
<?php
$sql = "SELECT COUNT('so_id') FROM sales_order";
$rs_result = mysqli_query($conn, $sql);
$row = mysqli_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / $limit);
$pagLink = "<div class='pagination'>";
for ($i=1; $i<=$total_pages; $i++) {
$pagLink .= "<a href='index.php?page=".$i."'>".$i."</a>";
};
echo $pagLink . "</div>"; ?>

There are few things wrong left over from your edited question earlier, most particularly this one where you changed the single quotes from ORDER BY 'so_id' to using backticks:
ORDER BY `so_id`
More on what are called "Identifier Qualifiers" can be seen in the following reference:
https://dev.mysql.com/doc/refman/8.0/en/identifier-qualifiers.html
Then about <? echo. Those are what are called short tags and if they're unabled, you need to change them to <?php echo or <?= which also does the same.
Then your quotes in COUNT('so_id'). Either remove them or replace them with backticks.
COUNT(`so_id`)
Finally; you're going to receive undefined index notices for these as soon as the page is loaded:
<td><? echo $row["so_customer"]; ?></td>
<td><? echo $row["so_address"]; ?></td>
Therefore and to avoid / fix those errors, replace them with the following:
<td><?php if(isset($row['so_customer'])){ echo $row["so_customer"]; } ?></td>
<td><?php if(!empty($row['so_address'])){ echo $row["so_address"]; }?></td>
Enabling error reporting for the PHP and error handling for the MySQL, would have been of help during development/debugging.
http://php.net/manual/en/function.error-reporting.php
http://php.net/manual/en/mysqli.error.php

Related

Displaying query rows dependent on another query's result

I have a function in PHP that prints a list of inventories with their IDs.
Another function lists the products in the inventory with the corresponding ID from the inventory list.
But I don't know how to get the inventory ID from the list correctly into the product list, and it doesn't work for me. I don't know exactly where I'm doing wrong.
When I list the products with a specific ID and refresh the page, I want the list to stay the same.
A function to get a list
<?
php function fetchSeznamInventur() {
global $userCon;
global $rowcount;
global $query;
global $inventuraId;
global $row;
$query = mysqli_query($userCon, "SELECT * FROM seznamInventur");
//$inventuraId = $row["id"];
$rowcount = mysqli_num_rows($query);
}
?>
List listing to HTML
<?php
<table border="1" class="invTable">
<thead>
<th>Datum</th>
<th>ID</th>
<th>Název</th>
</thead>
<?php
for ($inv = 1; $inv <= $rowcount; $inv++) {
$row = mysqli_fetch_array($query);
?>
<tr>
<td><?php echo $row["createdate"]?></td>
<td name="id"><?php echo $row["id"] ?></td>
<td><?php echo $row["name"]?></td>
<td><input type="submit" name="submit" value="Detail"></td>
</tr>
<?php
}
?>
</table>
?>
Obtaining products by inventory ID
<?php
function fetchInventura() {
global $userCon;
global $inventuraId;
global $rowcount;
global $query;
$query = mysqli_query($userCon, "SELECT * FROM inv WHERE inventuraId='$inventuraId'");
$rowcount = mysqli_num_rows($query);
echo $inventuraId;
}
?>
List of products in html
<?php
for ($products = 1; $products <= $rowcount; $products++) {
$row = mysqli_fetch_array($query);
?>
<tr>
<td><?php echo $row["id"]?></td>
<td><?php echo $row["inventuraId"] ?></td>
<td style="display: none;"><?php echo $row["name"]?></td>
<td><?php echo $row["ean"]?></td>
<td style="display: none;"><?php echo $row["plu"]?></td>
<td style="display: none;"><?php echo $row["externalId"]?></td>
<td style="display: none;"><?php echo $row["productId"]?></td>
<td><?php echo $row["quantity"]?></td>
<td><?php echo $row["versiondate"]?></td>
</tr>
<?php
}
?>
</table>
<input type="submit" name="submit" value="Detail">
won't do much, as it's not inside a form.
You probably need a hyperlink which goes to a "products" page and passes the inventory ID as a query parameter, e.g.
Display
And then on the products page, you'd use $_GET["inventoryID"] to get the ID, and pass that into your SQL query (but please use a parameter, don't inject variables directly into the SQL, it's a security risk).

Row number is not displaying first and last row number in paginated table in php

I am implementing a website in which admin has access to maintain stock. I am displaying stock from the database in html table. i have used pagination to display 10 records per page. I want that when i am on page 1 it displays "Showing 1-10 of total record" and when i am on page 2 it displays "Showing 11-20 of total record" and vice versa.
CODE:
<?php
$link = mysql_connect("localhost", "root", "");
mysql_select_db("login", $link);
$result = mysql_query("SELECT COUNT(*) FROM add_stock");
$row = mysql_fetch_row($result);
$num = $row[0];
?>
<table align="center" border="0" id="myTable" class="table table-striped table-bordered table-list">
<tr>
<th>Sr.No</th>
<th>Product Code</th>
<th>Brand</th>
<th>Price</th>
<th>Gender</th>
<th>Category</th>
<th>Material</th>
<th>Size</th>
<th>Description</th>
<th>Quantity</th>
<th><b>Image</b></th>
</tr>
$num_rec_per_page=10;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $num_rec_per_page;
$sql = "SELECT * FROM add_stock ORDER BY id DESC LIMIT $start_from, $num_rec_per_page";
$rs_result = mysql_query ($sql); //run the query
$total_records = mysql_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
$next_page = $page + 1;
$prev_page = $page - 1;
$i= $start_from;
$start=1;
while ($result=mysql_fetch_array($rs_result) )
{
?>
<tr>
<td><?php echo $i+$start; ?></td>
<td><?php echo $result['id']; ?></td>
<td><?php echo $result['brand_name']; ?></td>
<td><?php echo $result['price']; ?></td>
<td><?php echo $result['gender_name']; ?></td>
<td><?php echo $result['category_name']; ?></td>
<td><?php echo $result['material_name']; ?></td>
<td><?php echo $result['size_name']; ?></td>
<td><?php echo $result['dress_description']; ?></td>
<td><?php echo $result['dress_quantity']; ?></td>
<td><a href="javascript:window.open('<?php echo $result['image'] ?>','mypopuptitle', '_parent')" >View Image</a></td>
</tr>
<?php
$i++;
} ?>
</table>
<?php
$sql = "SELECT * FROM add_stock";
$rs_result = mysql_query($sql); //run the query
$total_records = mysql_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
?>
<div style="margin-left: 5px;">
<div class="pagination">
<?php
echo "<a href='viewstock.php?page=1'><b>«</b></a>";
if ($page==1) {
$page=1;
}
else{
echo "<a href='viewstock.php?page=$prev_page'><b>Prev</b></a>"; }
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='viewstock.php?page=".$i."'><b>".$i."</b></a>";
}
if ($page==$total_pages) {
$page=$total_pages;
}
else{
echo "<a href='viewstock.php?page=$next_page'><b>Next</b></a>"; }
echo "<a href='viewstock.php?page=$total_pages'><b>»</b></a> ";
?>
</div>
</div>
<p>Displaying <?php echo $start ?> - <?php echo $total_records ?> of Records: <?php echo $num ?></p>
This will work:
<p>Displaying <?php echo $start_from+1 ?> - <?php echo $start_from + $total_records ?> of Records: <?php echo $num ?></p>
as long as you:
1) Move the output below the code where the numbers you need are calculated
2) Don't re-use the variable $total_records after your SELECT * FROM add_stock. Instead give this instance of the variable a new name so it can't be confused with the earlier version, which means something different, and is needed for your output.
Please note also my first comment regarding your SQL Injection vulnerability, which you should take steps to fix as soon as possible.

PHP Pagination - only 1 page works

I have been messing around, and been trying to make this script work (for later implenting it in my own projects). It works fine, beside that I when I click on a new page, the results doesnt' change..
here is the script:
<?php
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 5;
$sql = "SELECT * FROM employee ORDER BY emp_id ASC LIMIT $start_from, 20";
$rs_result = mysql_query ($sql);
?>
<table>
<tr><td>Name</td><td>Phone</td><td>Salary</td></tr>
<?php
while ($row = mysql_fetch_assoc($rs_result)) {
?>
<tr>
<td><?php echo $row['emp_id']; ?></td>
<td><?php echo $row['emp_name']; ?></td>
<td><?php echo $row['emp_salary']; ?></td>
</tr>
<?php
};
?>
</table>
<?php
$sql = "SELECT COUNT(emp_name) FROM employee";
$rs_result = mysql_query($sql);
$row = mysql_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / 5);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='pagination?page=".$i."'>".$i."</a> ";
};
?>
Actually there seems nothing fundamentally wrong. Did you try a hard reload in your browser ?
Use this in the unshown head part of your page to get rid of some caching issues:
<html>
<head>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">
<meta http-equiv="cache-control" content="no-cache">
</head>
...
</html>
I changed some things anyway:
$perpage contains the number of entries per page nice in one place
never use unsanitized values in sql so I (int)ed the $_GET
--- need this line for the code to show ---
<?php
$perpage = 5;
if (isset($_GET["page"])) { $page = abs ((int)$_GET["page"]); } else { $page=1; };
$start_from = ($page-1) * $perpage;
$sql = "SELECT * FROM employee ORDER BY emp_id ASC LIMIT $start_from, $perpage";
$rs_result = mysql_query ($sql);
?>
<table>
<tr><td>Name</td><td>Phone</td><td>Salary</td></tr>
<? php
while ($row = mysql_fetch_assoc($rs_result)) {
?>
<tr>
<td><?php echo $row['emp_id']; ?></td>
<td><?php echo $row['emp_name']; ?></td>
<td><?php echo $row['emp_salary']; ?></td>
</tr>
<?php
};
?>
</table>
<?php
$sql = "SELECT COUNT(emp_name) FROM employee";
$rs_result = mysql_query($sql);
$row = mysql_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / 5);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='pagination?page=".$i."'>".$i."</a> ";
};
?>

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
?>

Displaying number of records in continuous fashion in php paging

I have a page which displays number of records on each page. I am displaying 5 records on each page and then next 5 on the next page and so on. Paging is working fine but the problem is on first page I'm displaying number serial wise next to each record i.e. from 1 to 5. Then on next page it should display numbers from 6 to 10 and on next page 11 to 15 and so on. But on every page numbers start from 1 to 5.
My code is below. I have tried different strategies but nothing worked. Please check code and tell me where to make changes so that it works properly. Thanks a ton in advance.
<div class="grid_12">
<div class="box first round fullpage mh500 grid">
<h2><?php echo $resource->_pageHead; ?></h2>
<?php $resource->displayMessage(); ?>
<?php
if($resource->_recordCount > 0)
{
?>
<div class="block">
<table class="listing" >
<thead>
<tr>
<th width="50" class="bdr-left">Sr. No.</th>
<th width="60">Name</th>
<th width="60">Email</th>
<th width="60">Address</th>
<th width="60">City</th>
<th width="60">State</th>
<th width="60">Phone Number</th>
<th width="60">Country</th>
<th width="60">Comment</th>
<th width="60">Inquiry Date</th>
<th width="60" class="bdr-right">Action</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$_SESSION['number'] = $i;
$perpage = 5;
$q = mysql_query("SELECT * FROM $resource->_table");
$total_record = mysql_num_rows($q);
$pages = ceil($total_record/$perpage);
$page = (isset($_GET['page']))?$_GET['page']:1;
$start = ($page-1) * $perpage;
$result = mysql_query("SELECT * FROM $resource->_table LIMIT $start, $perpage");
while($res = mysql_fetch_array($result))
{
?>
<tr class="odd gradeX">
<td><?php echo $i; ?></td>
<td><?php echo $res['name']; ?></td>
<td><?php echo $res['email'];?></td>
<td><?php echo $res['address'];?></td>
<td><?php echo $res['city'];?></td>
<td><?php echo $res['state'];?></td>
<td><?php echo $res['p_code']."-".$res['p_num'];?></td>
<td><?php echo $res['country'];?></td>
<td><?php echo substr($res['comments'], 0, 100);echo "...";?></td>
<td><?php echo $res['inquiry_date'];?></td>
<td align="center">
<a href="<?php echo $_SERVER['PHP_SELF'].'?action=delete&id='.$res['id'];?>" onclick="return confirm('Do you want to delete this record?');">
<img src="img/cross.png" alt="Delete" title="Delete"/>
</a>
</td>
</tr>
<?php
$i++;
}
}
?>
</tbody>
</table>
</div>
<div id="paging" style="padding-left:500px;">
<?php
$prev=$page-1;
$next=$page+1;
if($prev > 0)
{
echo "<a href='?page=$prev'>Prev</a>";
}
echo " ";
if($pages >= 1 AND $page <= $pages)
{
for($x=1;$x<=$pages;$x++)
{
echo " ";
echo ($x==$page) ?"$x":'<a href="?page='.$x.'" >'.$x.'</a>';
}
echo "&nbsp&nbsp";
if($page<$pages)
{
echo "<a href='?page=$next'>Next</a>";
}
}
?>
</div>
</div>
<div class="clear"></div>
</div>
I hope the following logic would be helpful to you, initialize a variable $i = 0;
Calculate the starting number of a record by the following logic,
$page_num = (int) (!isset($_GET['page']) ? 1 : $_GET['page']);
$start_num =((($page_num*$num_records_per_page)-$num_records_per_page)+1);
initialize a variable $i = 0;
Then inside the loop calculate the serial number like,
$slNo = $i+$start_num;
Then echo $slNo;
instead of echo $i; try this
$j= (($page-1) * $perpage) + i; echo $j;
You can go to this website and there is a simple example of pagination
http://snipplr.com/view/55519/
hope this works for you.

Categories