This is pagination code which is converted in PDO form and this is not giving any output which is connected to table , here is code of pdo pagination code
<?php
$total_num_page=2;
if (isset($_GET['page']))
{
$page_id = $_GET['page'];
}
else
{
$page_id = 1;
}
$search = '' ;
if( isset($_POST['search']) )
{
$search = urldecode($_POST['search']);
}
else if( isset($_GET['search']) )
{
$search = urldecode($_GET['search']);
}
if ($search)
{
$all_stmt=$db->prepare("select * from files where recieved_by like :needle or processed_by like :needle or purpose like :needle or file_name like :needle order by date desc");
$needle = '%' . $search . '%';
$all_stmt->bindValue(':needle', $needle, PDO::PARAM_STR);
$all_stmt->execute();
$all_post=$all_stmt->rowCount();
$total_page = ceil($all_post / $total_num_page);
$page_start_from = ($page_id - 1) * $total_num_page;
}
else
{
$all_post_query = "select * from files order by date desc";
$all_stmt=$db->prepare($all_post_query);
$all_stmt->execute();
$all_post=$all_stmt->rowCount();
$total_page = ceil($all_post / $total_num_page);
$page_start_from = ($page_id - 1) * $total_num_page;
}
?>
and here is part where i am fetching data from table database , and i don't know what is problem coming ,
<?php
if ($search)
{
$stmt=$con->prepare("select * from files where recieved_by like :needle or processed_by like :needle or purpose like :needle or file_name like :needle order by date desc limit $page_start_from, $total_num_page"); $needle = '%$search%';
$stmt->bindValue(':needle', $needle, PDO::PARAM_STR);
}
else
{
$stmt=$con->prepare("select * from files order by date desc limit $page_start_from, $total_num_page");
}
$stmt->execute();
$stmt->fetchAll();
if(count($stmt)>0){
while($row=$stmt->fetch())
{
$c_id=$row['id'];
$file=$row['file_name'];
$purpose=$row['purpose'];
$recieve=$row['recieved_by'];
$processed=$row['processed_by'];
$address=$row['address'];
$contact=$row['contact_no'];
$date=$row['date'];
?>
<tr>
<td><?php echo $c_id;?></td>
<td><?php echo $file;?></td>
<td><?php echo $purpose;?></td>
<td><?php echo $recieve;?></td>
<td><?php echo $processed;?></td>
<td><?php echo $address;?></td>
<td><?php echo $contact;?></td>
<td><?php echo $date;?></td>
</tr>
<?php
}
}
else
{
echo "No Related File Found Here ";
}
?>
</tbody>
</table>
<nav id="pagination">
<ul class="pagination">
<?php
$search_str = '';
if ($search) {
$search_str = "&search=" . urlencode($search);
}
for ($i = 1; $i <= $total_page; $i++)
echo "<li class='" . ($page_id == $i ? 'active' : '') . "'><a href='index.php?page=" . $i . $search_str . "'>$i</a></li>";
?>
and this is code of my database
<?php
$servername = "localhost";
$username = "root";
$password = "";
$con = new PDO("mysql:host=$servername;dbname=fileprogramsysteeem", $username, $password);
?>
and i also wanted to check ,if condition in page when there is no related page found in database it should show else output , Now what is i am doing wrong can anybody help me please
No Related File Found Here
Related
I just started to learn PHP this week, and I'd search for possible solution on this but I can make it right. While I sucessfully created a pagination that sorts queries into alphabetical order, I wasn't successful in creating a numerical pagination, in which it will limit the number of queries to be displayed. Also, I wasn't able to combined these two types of pagination with two different queries, one for A-Z sorting, and one for the number of queries to be displayed in a single page.
Here's my code
`<?php
include 'includes/connection.php';
$character = '';
$characters = '';
if (isset($_GET['character']))
{
$character = $_GET['character'];
$character = preg_replace('#[^a-z]#i', '', $character);
$sql = "SELECT * FROM drivers_info WHERE last_name LIKE '$character%' ORDER BY last_name";
}
else{
$sql = "SELECT * FROM drivers_info ORDER BY rfid ";
$pageno = 1
}
$result = mysqli_query($db, $sql);
<body>
<br /><br />
<br /> <br />
Here's the code to create an alphabetical pagination
<div class="table-responsive">
<div align="center">
<?php
$character = range('A', 'Z');
echo '<ul class="pagination">';
foreach($character as $alphabet)
{
echo '<li>'.$alphabet.'</li>';
}
echo '</ul>';
?>
</div>
<?php
Here's the code for displaying the queries alphabetically
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result)){
?>
<tr>
<td><?php echo $row["rfid"]; ?></td>
<td><?php echo $row["last_name"]; ?></td>
<td><?php echo $row["first_name"]; ?></td>
<td><?php echo $row["middle_name"]; ?></td>
<td><?php echo $row["gender"]; ?></td>
<td><?php echo $row["age"]; ?></td>
<td><?php echo $row["height"]; ?></td>
<td><?php echo $row["weight"]; ?></td>
<td><?php echo $row["address"]; ?></td>
<td><?php echo $row["contact_no"]; ?></td>
<td><?php echo $row["license_type"]; ?></td>
<td><?php echo $row["license_no"]; ?></td>
<td><?php echo $row["expiration"]; ?></td>
<td><?php echo $row["nationality"]; ?></td>
<td><?php echo $row["eyes_color"]; ?></td>
<td><?php echo $row["blood_type"]; ?></td>
</tr>
<?php
}
}
</body>
If you had any code or idea on it, would you mind sharing it with me? Thank you so much, I needed this to accomplish my school project.
`
Here is best reference code for understanding pagination logic
<?php
$host = "localhost";
$user = "root";
$pass = "New";
$db = "booksgood";
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// how many rows to show per page
$rowsPerPage = 10;
// by default we show first page
$page_num = 1;
// if $_GET['page'] defined, use it as page number, $_GET gets the page number out of the url
//set by the $page_pagination below
if(isset($_GET['page'])){$page_num = $_GET['page'];}
//the point to start for the limit query
$offset = $page_num;
// Zero is an incorrect page, so switch the zero with 1, mainly because it will cause an error with the SQL
if($page_num == 0) {$page_num = 1;}
// counting the offset
$sql = "SELECT * FROM bookdata where titles like 's%' order by titles LIMIT $offset, $rowsPerPage ";
$res = mysql_query($sql) or die(mysql_error());
// how many rows we have in database
$sql2 = "SELECT COUNT(id) AS numrows FROM bookdata where titles like 's%'";
$res2 = mysql_query($sql2) or die(mysql_error());
$row2 = mysql_fetch_array($res2);
$numrows = $row2['numrows'];
// print the random numbers
while($row = mysql_fetch_array($res))
{
//Echo out your table contents here.
echo $row[1].'<BR>';
echo $row[2].'<BR>';
echo '<BR>';
}
// how many pages we have when using paging?
$numofpages = ceil($numrows/$rowsPerPage);
// print the link to access each page
$self = "/paging3.php?";
if ($numofpages > '1' ) {
$range =15; //set this to what ever range you want to show in the pagination link
$range_min = ($range % 2 == 0) ? ($range / 2) - 1 : ($range - 1) / 2;
$range_max = ($range % 2 == 0) ? $range_min + 1 : $range_min;
$page_min = $page_num- $range_min;
$page_max = $page_num+ $range_max;
$page_min = ($page_min < 1) ? 1 : $page_min;
$page_max = ($page_max < ($page_min + $range - 1)) ? $page_min + $range - 1 : $page_max;
if ($page_max > $numofpages) {
$page_min = ($page_min > 1) ? $numofpages - $range + 1 : 1;
$page_max = $numofpages;
}
$page_min = ($page_min < 1) ? 1 : $page_min;
//$page_content .= '<p class="menuPage">';
if ( ($page_num > ($range - $range_min)) && ($numofpages > $range) ) {
$page_pagination .= '<a class="num" title="First" href="'.$self.'page=1"><</a> ';
}
if ($page_num != 1) {
$page_pagination .= '<a class="num" href="'.$self.'page='.($page_num-1). '">Previous</a> ';
}
for ($i = $page_min;$i <= $page_max;$i++) {
if ($i == $page_num)
$page_pagination .= '<span class="num"><strong>' . $i . '</strong></span> ';
else
$page_pagination.= '<a class="num" href="'.$self.'page='.$i. '">'.$i.'</a> ';
}
if ($page_num < $numofpages) {
$page_pagination.= ' <a class="num" href="'.$self.'page='.($page_num + 1) . '">Next</a>';
}
if (($page_num< ($numofpages - $range_max)) && ($numofpages > $range)) {
$page_pagination .= ' <a class="num" title="Last" href="'.$self.'page='.$numofpages. '">></a> ';
}
//$page['PAGINATION'] ='<p id="pagination">'.$page_pagination.'</p>';
}//end if more than 1 page
echo $page_pagination.'<BR><BR>';
echo 'Number of results - '.$numrows ;
echo ' and Number of pages - '.$numofpages.'<BR><BR>';
// Free resultset
mysql_free_result($res);
// and close the database connection
mysql_close($con);
?>
here is reference code link
I want to display five record per page through pagination (mysql,php,html,css) until all the records are displayed, navigation to pages must be like, Page: 1 2 3 4 5 6 7 7 8... Last.
HERE IS MY CODE TO VIEW ALL THE RECORDS FROM emp_master table.
I am new to PHP so please write an easily understandable code for pagination. I have seen few examples but they are not working.
<?php
$con=mysqli_connect("localhost","user","password","dataplus");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM emp_master");
echo "<table border='1'>";
$i = 0;
while($row = $result->fetch_assoc())
{
if ($i == 0) {
$i++;
echo "<tr>";
foreach ($row as $key => $value) {
echo "<th>" . $key . "</th>";
}
echo "</tr>";
}
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
I want to display five record per page through pagination untill all the records are displayed, navigation to pages must be like, Page: 1 2 3 4 5 6 7 7 8... Last.
This code below is not working:
$dbhost="localhost";
$dbuser="10053";
$dbpass="n6867242";
$database="0368";
$rec_limit = 10;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('1005368');
/* Get total number of records */
$sql = "SELECT count(emp_id) FROM emp_master ";
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];
if( isset($_GET{'page'} ) ) {
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}else {
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT emp_id, emp_name, e_mail ".
"FROM emp_master ".
"LIMIT $offset, $rec_limit";
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "EMP ID :{$row['emp_id']} <br> ".
"EMP NAME : {$row['emp_name']} <br> ".
"EMP MAIL : {$row['e_mail']} <br> ".
"--------------------------------<br>";
}
if( $page > 0 ) {
$last = $page - 2;
echo "Last 10 Records |";
echo "Next 10 Records";
}else if( $page == 0 ) {
echo "Next 10 Records";
}else if( $left_rec < $rec_limit ) {
$last = $page - 2;
echo "Last 10 Records";
}
mysql_close($conn);
If you want just PHP MySQL code, I usually use something like the following.
$page=max(intval($_GET['page']),1); // assuming there is a parameter 'page'
$itemsperpage = 5;
$total=100; // total results if you know it already otherwise use another query
$totalpages = max(ceil($total/$itemsperpage),1);
$query = "SELECT * FROM emp_master LIMIT ".(($page-1)*$itemsperpage).",".$itemsperpage; // this will return 5 items based on the page
You can use a library called dataTables that will auto paginate the data and you can easily customize it to suit your needs with the look and how many records to show per page. It also has a search field which enables users to search your data without any extra code.
All you have to do is include a couple of cdn's and the following code and everything works fine
$(document).ready(function(){
$('#myTable').DataTable();
});
Some examples here
you just should remove spaces in href and also make $page = $page + 1 when page is 0
if( $page > 0 ) {
$last = $page - 2;
echo "Last 10 Records |";
echo "Next 10 Records";
}else if( $page == 0 ) {
$page = $page + 1;
echo "Next 10 Records";
}else if( $left_rec < $rec_limit ) {
$last = $page - 2;
echo "Last 10 Records";
}
Link is here for demo, click here to experience the result
Here is the code working for me, just change your db name, username and password
and get pagination done. You can change $rec_limit value to your desired no. of records per page.
<?php
$host="localhost";
$username="68";
$password="67242";
$database="68";
$rec_limit = 5;
$conn = mysql_connect($localhost,$username,$password);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('10');
/* Get total number of records */
$sql = "SELECT count(emp_id) FROM emp_master ";
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];
if( isset($_GET{'page'} ) ) {
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}else {
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT eid,ename, email, quali, gender, contactno, birthdate, joiningdate,CURDATE(), TIMESTAMPDIFF( YEAR, birthdate, CURDATE( ) ) AS age ".
"FROM emp_master ".
"LIMIT $offset, $rec_limit";
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
echo "<table border='1'>";
$i = 0;
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
if ($i == 0) {
$i++;
echo "<tr>";
foreach ($row as $key => $value) {
echo "<th>" . $key . "</th>";
}
echo "</tr>";
}
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
echo "</table>";
if( $page > 0 ) {
$last = $page - 2;
echo "Last 5 Records | ";
echo "Next 5 Records";
}else if( $page == 0 ) {
$page = $page + 0;
echo "Next 5 Records";
}else if( $left_rec < $rec_limit ) {
$last = $page - 2;
echo "Last 5 Records";
}
mysql_close($conn);
php?>
IF your are using mysqli the code is below
$conn=mysqli_connect("localhost","root","","ui");
$start=0;
$limit=5;
$t=mysqli_query($conn,"select * from form_table");
$total=mysqli_num_rows($t);
if(isset($_GET['id']))
{
$id=$_GET['id'] ;
$start=($id-1)*$limit;
}
else
{
$id=1;
}
$page=ceil($total/$limit);
$query=mysqli_query($conn,"select * from form_table limit $start, $limit");
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script s src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"> </script>
</head>
<body>
<div class="container">
<h2>Table</h2>
<table class="table table-bordered">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Gender</th>
<th>Hobbies</th>
<th>Course</th>
</tr>
</thead>
<tbody>
<?php
while($ft=mysqli_fetch_array($query))
{?>
<tr>
<td><?= $ft['0']?></td>
<td><?= $ft['1']?></td>
<td><?= $ft['2']?></td>
<td><?= $ft['3']?></td>
<td><?= $ft['4']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<ul class="pagination">
<?php if($id > 1) {?> <li>Previous</li><?php }?>
<?php
for($i=1;$i <= $page;$i++){
?>
<li><?php echo $i;?></li>
<?php
}
?>
<?php if($id!=$page)
{?>
i Give you an example of my project of pagination. Just you have to put your values in the code
$sql="SELECT * FROM tblname LIMIT $next,5";
$results = mysqli_query($conn,$sql);
}
else if (isset($_POST['prev']))
{
$prev1=$_POST['prev'];
$next=$_POST['prev'];
$prev=$prev1;
$sql="SELECT * FROM tablename LIMIT $prev,5";
$prev=$prev1;
$results = mysqli_query($conn,"SELECT * FROM tablename LIMIT $prev,10");
if($prev==0)
{
$prev = 0;
}
else
{
$prev=$next-10;
}
}
else
{
$next=0;
$prev=0;
$results = mysqli_query($conn,"SELECT * FROM tablename LIMIT $next,10");
}
When I click on Add to cart hyperlink, cart.php page is not responding (it's not echoing the add variable. Hyperlink looks fine. However, there is something wrong in cart.php. Any response is appreciated. Thanks in advance.
<html>
<head>
</head>
<body>
<table>
<tr>
<td><?php echo $row['ISBN']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['year']; ?></td>
<td><?php echo $row['price']; ?></td>
<td><?php echo $row['publisher']; ?></td>
<td> Add to cart</td>
<td><?php echo $row['ISBN']; ?></td>
</tr>
</table>
</body>
</html>
cart.php page:
<?php
//
session_start();
$page = 'search.php';
$lpage = 'cart.php';
$db = new mysqli('localhost', 'root', '', 'cheapbook') or die('Error connecting to MySQL server.');
mysqli_set_charset($db, 'utf8');
if (isset($_GET['add'])) {
echo $_GET['add'];
$pieces = explode(":", $_GET['add']);
$quantity = mysqli_query('SELECT ISBN, title from book WHERE ISBN=$pieces[0]');
$result = mysqli_query($db, $quantity);
while ($quantity_row = mysqli_fetch_array($result)) {
if ($quantity_row['quantity'] != $_SESSION['cart_' . $_GET['add']]) {
$_SESSION['cart_' . $_GET['add']] += 1;
}
}
if ($pieces[1] == 'SearchByBookTitle') {
header('location:' . $page . 'SearchByBookTitle=' . $pieces[2]);
}
if ($pieces[1] == 'SearchByAuthor') {
header('location:' . $page . 'SearchByAuthor=' . $pieces[2]);
echo $pieces[1];
} else {
header('location:' . $lpage);
}
}
if (isset($_GET['remove'])) {
$_SESSION['cart_' . $_GET['remove']]--;
header('location:' . $page);
}
if (isset($_GET['delete'])) {
$_SESSION['cart_' . $_GET['remove']]--;
header('location:' . $page);
}
if (isset($_GET['cart'])) {
cart();
}
function cart()
{
foreach ($_SESSION as $name => $value) {
if ($value > 0) {
if (substr($name, 0, 5) == 'curt_') {
$total = 0;
$id = substr($name, 5, (strlen($name) - 5));
$get = mysql_query("SELECT ISBN, title, price FROM book where id='.$id.'");
$result = mysqli_query($db, $get);
while ($get_row = mysqli_fetch_array($result)) {
$sub = $get_row['price'] * $value;
echo $get_row['title'] . 'X' . $value . '#Dollar' . $get_row['price'] . '=' . $sub . '[-][+][Delete]';
}
$total += $sub;
}
}
if ($total == 0) {
echo "Your cart is empty";
} else {
echo "Paypal button";
}
}
}
?>
This query coded like this of course will not work as expected
$quantity = mysqli_query('SELECT ISBN, title
from book
WHERE ISBN=$pieces[0]');
You need a double quoted string to use variable expansion, it does not work in a single quoted string.
You also need to add single quotes around the text varibale parameter value
$quantity = mysqli_query("SELECT ISBN, title
from book
WHERE ISBN='$pieces[0]'");
In future you would be well advised to add some error checking code after you attempt to execute a query and also use prepared and parameterised queries to avoid SQL Injection
$sql = "SELECT ISBN, title from book WHERE ISBN=?";
$stmt = mysqli_prepare($sql);
if ( ! $stmt ) {
echo mysqli_error();
exit;
}
$stmt->bind_param('s', $pieces[0] );
$stmt->execute();
ok si im not very fimiliar with PDO im just starting off, could someone help me here why my results will not display? the pages are working but no data would show ..
<?php
require_once 'db/_db.php';
$stmt = $db->prepare("SELECT * FROM article");
$stmt->execute();
$result = $stmt->fetchAll();
$total_rows = $stmt->rowCount();
//$allRecords = mysql_query('select * from article');
//$total_rows = mysql_num_rows($allRecords);
//$base_url = 'https://localhost/pagi/'; //
global $per_page;
$num_links = 4;
$total_rows = $total_rows;
$cur_page = 1;
if(isset($_GET['page']))
{
$cur_page = $_GET['page'];
$cur_page = ($cur_page < 1)? 1 : $cur_page; //if page no. in url is less then 1 or -ve
}
$offset = ($cur_page-1)*$per_page; //setting offset
$pages = ceil($total_rows/$per_page);
$start = (($cur_page - $num_links) > 0) ? ($cur_page - ($num_links - 1)) : 1;
$end = (($cur_page + $num_links) < $pages) ? ($cur_page + $num_links) : $pages;
//$res = mysql_query("SELECT * FROM article ORDER BY id DESC LIMIT ".$per_page." OFFSET ".$offset);
$stm = $db->prepare("SELECT * FROM article ORDER BY id DESC LIMIT ".$per_page." OFFSET ".$offset);
$stm->execute();
$res = $stm->fetchAll();
$rows = $stm->rowCount();
if($rows < 1){
header("Location: news-events.php");
exit;
}
if(is_resource($result))
{
foreach( $res as $row ) {
$desc = $row["ne_article"];
$img = $row['ne_image'];
if(!empty($img)){
$img = '<br/><img src="uploads/images/'.$img.'" alt="" class="responsive-shrink">';
}else{
$img = '';
}
$youtube = $row["ne_youtube"];
if(!empty($youtube)){
$youtube = '<br/><div class="video-container"><iframe src="http://www.youtube.com/embed/'.$youtube.'"></iframe></div>';
}else{
$youtube = '';
}
//shortenString($row['ne_article']);
?>
<h4><?php echo $row['ne_title']; ?></h4>
<h5><b>Views:</b> <?php echo $row['views']; ?>, <b>Posted on:</b> <?php echo format_date($row['created']); ?></h5>
<?php echo $img; ?>
<?php echo $youtube; ?>
<p>
<br/><?php echo bbcode(nl2br(shortenString($desc))); ?>
</p>
<div id="pagelink">
Read more...</button>
</div>
<?php
}
}
?>
<div id="pagination">
<div id="pagiCount">
<br/><br/>
<?php
if(isset($pages))
{
if($pages > 1)
{ if($cur_page > $num_links) // for taking to page 1 //
{ $dir = "First";
echo '<span id="prev"> '.$dir.' </span>';
}
if($cur_page > 1)
{
$dir = "Prev";
echo '<span id="prev"> '.$dir.' </span>';
}
for($x=$start ; $x<=$end ;$x++)
{
echo ($x == $cur_page) ? '<strong>'.$x.'</strong> ':''.$x.' ';
}
if($cur_page < $pages )
{ $dir = "Next";
echo '<span id="next"> '.$dir.' </span>';
}
if($cur_page < ($pages-$num_links) )
{ $dir = "Last";
echo ''.$dir.' ';
}
}
}
?>
would really appreciate your time and help
You are doing nearly everything wrong. And you did with old mysql as well
To get the number of records in database you should never select all the rows, nor fetch them. You have to select the count already.
$total_rows = $pdo->query("SELECT count(1) FROM article")->fetchColumn();
is how it have to be done with PDO.
Besides, for the other query you should use prepared statements.
Finally, you have to take out all the useless verifications and start with foreach right away:
$stm = $db->prepare("SELECT * FROM article ORDER BY id DESC LIMIT ?,?");
$stm->execute([$offset,$per_page]);
$res = $stm->fetchAll();
foreach( $res as $row ) {
And read up on setting the error mode properly here.
check if you have problem with the quotes... when you echo html code inside ' ' if you have " " and again inside '' like this example echo ' " '' " ' you will have problem. you should use \' in order to avoid these problem here echo ' '.$dir.' ';
I got it to order by but now the paginaging doesn't work. NOTHING prints into my error_logs now.
User Red Acid helpedme out with the the missing Whitespace and use a string variable instead of PHP_SELF.
Once I press next record I get:
Not Found
The requested URL /surf/$index.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to
use an ErrorDocument to handle the request.
My code:
<?php
$dbHost = "localhost";
$dbUser = "";
$dbPass = "";
$dbName = "";
$tbl_name="";
$rec_limit = 20;
$order = "ORDER BY";
$scriptname = 'index.php';
mysql_connect("$dbHost", "$dbUser", "$dbPass")or die("cannot connect");
mysql_select_db("$dbName")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name $order ".$_GET["orderby"]." ".$_GET["desc"];
$result=mysql_query($sql);
$sql = "SELECT count(steamid) FROM $tbl_name";
$result= mysql_query($sql);
if(! $result)
{
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($result, MYSQL_NUM );
$rec_count = $row[0];
if( isset($_GET{'page'} ) )
{
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}
else
{
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT * ".
"FROM $tbl_name ".
"$order points DESC ".
"LIMIT $offset, $rec_limit";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $rows['name']; ?></td>
<td><?php echo $rows['points']; ?></td>
<td><?php echo $rows['winratio']; ?></td>
<td><?php echo $rows['pointsratio']; ?></td>
<td><?php echo $rows['finishedmaps']; ?></td>
<td><?php echo $rows['lastseen']; ?></td>
</tr>
</tr>
<?php
}
if( $page > 0 )
{
$last = $page - 2;
echo "Last 10 Records |";
echo "Next 10 Records";
}
else if( $page == 0 )
{
echo "Next 10 Records";
}
else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo "Last 10 Records";
}
mysql_close();
?><?php
$dbHost = "localhost";
$dbUser = "c1226125_ms";
$dbPass = "wR(#ucrb5oG.";
$dbName = "c1226125_timer";
$tbl_name="ck_playerrank";
$rec_limit = 20;
$order = "ORDER BY";
$scriptname = 'index.php';
mysql_connect("$dbHost", "$dbUser", "$dbPass")or die("cannot connect");
mysql_select_db("$dbName")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name $order ".$_GET["orderby"]." ".$_GET["desc"];
$result=mysql_query($sql);
$sql = "SELECT count(steamid) FROM $tbl_name";
$result= mysql_query($sql);
if(! $result)
{
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($result, MYSQL_NUM );
$rec_count = $row[0];
if( isset($_GET{'page'} ) )
{
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}
else
{
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT * ".
"FROM $tbl_name ".
"$order points DESC ".
"LIMIT $offset, $rec_limit";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $rows['name']; ?></td>
<td><?php echo $rows['points']; ?></td>
<td><?php echo $rows['winratio']; ?></td>
<td><?php echo $rows['pointsratio']; ?></td>
<td><?php echo $rows['finishedmaps']; ?></td>
<td><?php echo $rows['lastseen']; ?></td>
</tr>
</tr>
<?php
}
if( $page > 0 )
{
$last = $page - 2;
echo "Last 10 Records |";
echo "Next 10 Records";
}
else if( $page == 0 )
{
echo "Next 10 Records";
}
else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo "Last 10 Records";
}
mysql_close();
?>
problem that you dont have white space between ORDER BY and DESC:
$sql="SELECT * FROM $tbl_name ORDER BY ".$_GET["orderby"].$_GET["desc"];
should be:
$sql="SELECT * FROM $tbl_name ORDER BY ".$_GET["orderby"]." ".$_GET["desc"];
use string variable instead of PHP_SELF:
$scriptname = 'myscript.php'; // OR http://domain.com/myscript.php
echo "Last 10 Records";