Here's a screenshot of the page that I want to put a pagination Below is my code and I want to create a simple pagination. I tried some examples available in this site but unfortunately it doesn't work for me or I might have missed something in the code.
<?php
session_start();
$server = 'localhost';
$user = 'root';
$pass = 'root';
$connect = mysql_connect($server,$user,$pass)
or die(mysql_error());
$selectdb = mysql_select_db('des')
or die(mysql_error());
?>
<form method="post" name="action_form" action="admin2.php">
<div id="gallery1" class="lbGallery">
<table class="table" width="100%" cellpadding="10">
<tbody>
<?php
$allRecords = mysql_query('select * from cms ORDER BY id DESC limit 4');
if(is_resource($allRecords))
{
while($row = mysql_fetch_assoc($allRecords))
{
?>
<tr><ul>
<td width="30%"><li style="list-style:none"><a href="uploads/<?php echo $row['image'];?>"/><img src="uploads/<?php echo $row['image'];?>"/></li></td>
<td style="float:left; font-size:16px"><?php echo $row['name']; ?></td>
</ul>
</tr>
<?php
}
}
?>
</tbody>
</table>
</div>
</form>
Try this. What is does is:
create a contant $amount en a variable $offset
Create a link (next) with which sends the value of $offset back to the script
Catch the value in $_GET['offset'];
Add the value of $amount to $offset to create a new offset.
Run MySQL statement again with the new values for LIMIT
I didn't actualy test this code for typo's, but you'll get the general idea. Hope this is of any help.
(Best to use the new mysqli statement by te way).
<?php
$amount = 4;
if (!empty($_GET['offset']))
{
$offset = $_GET['offset'] + $amount;
}
else
{
$offset = 1;
}
$allRecords = mysql_query('select * from cms ORDER BY id DESC limit $amount,$offset');
if(is_resource($allRecords))
{
while($row = mysql_fetch_assoc($allRecords))
{
?>
<tr><ul>
<td width="30%"><li style="list-style:none"><a href="uploads/<?php echo $row['image'];?>"/><img src="uploads/<?php echo $row['image'];?>"/></li></td>
<td style="float:left; font-size:16px"><?php echo $row['name']; ?></td>
</ul>
</tr>
<tr>
<td colspan="2">
Next
</td>
</tr>
<?php
}
}
?>
Related
Im just New in Wordpress and in PHP .. Now Ive just uploaded a theme in Wordpress. The Problem is The Custom Pagination is not working in Wordpress but works well in USBWebserver. It Only Just show the Same Content
<table id="maincon2-Article">
<?php
$page2 = $_GET["page2"];
if($page2 == "" || $page2 == "1"){
$pages = 0;
}
else{
$pages = ($page2*3)-3;
}
$Mobartsql = "Select * from tbl_article ORDER BY `Article_ID` DESC limit 1,3";
$result4 = $con->query($Mobartsql);
while($row4 = $result4->fetch_assoc()){
echo '<tr>
<td style="width:50%">
<img src = "https://www.stlouisreviewcenter.com/wp-content/uploads/slrc/'.$row4["Art_pic"].'" id="artpiclink2"/>
</td>
<td style="padding-left:2%" >
<h1 id="art-head2" style="color:purple">'.$row4["Art_title"].' </h5>
</td>
</tr>';
$results1 = $con->query("Select * from tbl_article ORDER BY `Article_ID` DESC");
$rows2 = $result1->num_rows/3;
$rows2 = ceil($rows2);
}
echo '<tr>
<td><br></td>
</tr>
<tr>
<td colspan="2">';
for($i=1; $i<=$rows;$i++){
?> <button> <a href="/article/?page=<?php echo $i;?>" style="text-decoration:none;"/><?php echo $i; ?></a> </button>
<?php
}
?>
</td>
</tr>
<tr>
<td><br/></td>
</tr>
</table>
Here is the Sample .. Plz Need Help ..
I am developing a page where a user can enter the limit of the table in pagination. While I am doing this the data I enter is taken and query is performed according to that. But as I click on another page of that table the value is reset to default which I set to 5 here.
<?php session_start(); ?>
Submit New record<br><br>
<form method="post">
<input type="text" name="dlimit">
<input type="submit" name="submit">
</form>
<?php
$database = 'test';
require 'connection.php';
if(!isset($_POST['submit']))
{
$limit = 5;
}
else
{
$dlimit = $_POST['dlimit'];
$limit = $dlimit;
}
#$id = $_GET['id'];
if($id==""||$id==null)
{
$page=0;
}
else
{
$page = ($id*$limit)-$limit;
}
$qq ="select * from record limit $page,$limit";
$result = $link -> query($qq);
?>
<table border="1"><th>ID</th>
<th>Name</th>
<th>qualification</th>
<th>address</th>
</tr>
<?php
while ($row = mysqli_fetch_object($result))
{
?>
<tr>
<td><?php echo $row->id ?></td>
<td><?php echo $row->user_name ?></td>
<td><?php echo $row->qualification ?></td>
<td><?php echo $row->address ?></td>
</tr>
<?php
}
?>
</table>
<?php
$query = "SELECT * FROM record";
$result = $link -> query($query);
$rows = mysqli_num_rows($result);
$rr = $rows/$limit;
$rr = ceil($rr);
for ($i=1; $i<=$rr ; $i++) {
?>
<?php echo #$i;?>
<?php
}
mysqli_close($link)
?>
Run the above code and check. If my words are not clear to you.
I think that the $_POST data is missing. You click on the link, so the new page will open without POST infos.
You can change this, if you switch to GET instead of POST. You can add the GET parameter to your <a href=""> Tag.
For example
<a href="pagination.php?page=5&dlimit=100
Also try to avoid the #error suppression and don't pass the $_POST/$_GET Vars directly to your sql string. Bad people could use it for SQL Injections
So I got the answer for my question. I am posting it here if anyone need it on later basis.
Submit New record<br><br>
<form method="get">
<input type="text" name="dlimit">
<input type="submit" name="submit">
</form>
<?php
$database = 'test';
require 'connection.php';
if(empty($_GET['dlimit']) && !isset($_GET['submit']) && empty($_GET['n']))
{
$limit = 5;
global $limit;
}
else
{
if (isset($_GET['dlimit']))
{
$limit = $_GET['dlimit'];
}
else
{
#$limit = $_GET['n'];
}
global $limit;
}
if(!isset($_GET['submit'])&& empty($_GET['n']))
{
$n=5;
global $n;
}
else
{
if(empty($_GET['dlimit']))
{
$n=$_GET['n'];
}
else
{
$n=$_GET['dlimit'];
}
global $n;
}
#$id = $_GET['id'];
if($id==""||$id==null)
{
$page=0;
}
else
{
$page = ($id*$limit)-$limit;
}
$qq ="select * from record limit $page,$limit";
$result = $link -> query($qq);
?>
<table border="1"><th>ID</th>
<th>Name</th>
<th>qualification</th>
<th>address</th>
</tr>
<?php
while ($row = mysqli_fetch_object($result))
{
?>
<tr>
<td><?php echo $row->id ?></td>
<td><?php echo $row->user_name ?></td>
<td><?php echo $row->qualification ?></td>
<td><?php echo $row->address ?></td>
</tr>
<?php
}
?>
</table>
<?php
if (!isset($_GET['submit']) && empty($_GET['n'])) {
$n = 5;
global $n;
}
else
{
if (empty($_GET['dlimit'])) {
$n = $_GET['n'];
}
else
{
$n = $_GET['dlimit'];
}
global $n;
}
$query = "SELECT * FROM record";
$result = $link -> query($query);
$rows = mysqli_num_rows($result);
$rr = $rows/$limit;
$rr = ceil($rr);
for ($i=1; $i<=$rr ; $i++) {
?>
<?php echo #$i;?>
<?php
}
mysqli_close($link)
?>
Again I am mentioning here that files which I included here are just connection where I filled my connection details and other is my record entry file where data is entered by the user.
how to display blank if there is no image in a record.As i have inserted a record in database without an image but while fetching an record it is displaying an blank image in front end.It should not show any image if there is no image.Here is my code. If there is no image it should show only description.
Blogimage.php
<tbody>
<?php include "blogs.php" ;
while($row = mysql_fetch_array($result))
{?>
<tr>
<td><img src="admin/upload/<?php echo $row['image'];?>" height="100" style="width:60%;height:50%;"/></td>
</tr>
<tr>
<td><?php echo "<p style='width:60%;'>" .$row['blog_description']."</p>"; ?></td>
</tr>
<?php }?>
</tbody>
Blogs.php
$id=$_GET['title'];
$res = "SELECT * FROM blogs
WHERE blog_title='$id'";
$result=mysql_query($res);
try changing this
<img src="admin/upload/<?php echo $row['image'];?>" height="100" style="width:60%;height:50%;"/>
to this
<?php if($row['image']) echo "<img src='admin/upload/".$row['image']."' height='100' style='width:60%;height:50%;' />"; ?>
A couple of general points before we go into this
1) The MySql library you are using is old and busted - you should be using something newer - read this - the options are Mysql PDO and MySqli - I promise you, you will be glad you did, i use IDIORM as an interface and find it very good (Also helps prevent stuff in point 2 below!)
2) Your code can be SQL injected - more about that here - this is very bad :)
Below is an example, I have fixed the SQL injection problem, and put in some logic that would test to see if there is an image, if not, it will dump out a 'noimage.jpg' src value.
I have removed the include, and just simply put the code for blogs.php inline.
<tbody>
<?php
//START CODE FOR BLOGS.PHP
$query = sprintf("SELECT * FROM blogs WHERE blog_title='%s'",
mysql_real_escape_string($_GET['title']));
$result = mysql_query($query);
//END CODE FOR BLOGS.PHP
while($row = mysql_fetch_array($result))
{
if(!empty($row['image']) && strlen($row['image']) > 4)
{
$iamgeSrc = 'admin/upload/' . $row['image'];
}
else
{
$imageSrc = 'admin/upload/noimage.jpg';
}
?>
<tr>
<td>
<img src="<?php echo $imageSrc; ?>" height="100" style="width:60%;height:50%;"/>
</td>
</tr>
<tr>
<td>
<?php echo "<p style='width:60%;'>" .$row['blog_description']."</p>"; ?>
</td>
</tr>
<?php
}
?>
</tbody>
If you want no image to show at all, then this would work
<tbody>
<?php
//START CODE FOR BLOGS.PHP
$query = sprintf("SELECT * FROM blogs WHERE blog_title='%s'",
mysql_real_escape_string($_GET['title']));
$result = mysql_query($query);
//END CODE FOR BLOGS.PHP
while($row = mysql_fetch_array($result))
{
$image = '';
if(!empty($row['image']) && strlen($row['image']) > 4)
{
$image = '<img src="admin/upload/'.$row['image'].'" height="100" style="width:60%;height:50%;"/>';
}
?>
<tr>
<td>
<?php echo $image; ?>
</td>
</tr>
<tr>
<td>
<?php echo "<p style='width:60%;'>" .$row['blog_description']."</p>"; ?>
</td>
</tr>
<?php
}
?>
</tbody>
I have an infinite scroll scipt that is pulling data and displaying it just fine. However, I am finding that when you scroll down the data pull starts again at the beginning. Right now I have 8 rows for testing in the database to make it easy. My control to get the next data set does not seem to be working otherwise it would go to the next set of results?
//item per page
$limit = 5;
$page =(int)(!isset($_GET['p']))?1: $_GET['p'];
// sql query
$sqlContent="SELECT make, model, year, carid FROM cars";
//Query start point
$start =($page * $limit)- $limit;
$resContent=$DB_con->query($sqlContent);
$rows_returned= $resContent->rowCount();//->fetchColumn();
// query for page navigation
if( $rows_returned > ($page * $limit)){
$next =++$page;
}
$sqlContent = $sqlContent ." LIMIT $start, $limit";
$finalContent = $DB_con->query($sqlContent);
if($finalContent === false) {
trigger_error('Error: ' . $DB_con->error, E_USER_ERROR);
} else {
$rows_returned= $finalContent->rowCount();//->fetchColumn();
}
?>
then display the results:
<?php while($rowContent = $finalContent->fetch()) {
$year = $rowContent['year'];
$make = $rowContent['make'];
$model = $rowContent['model'];
?>
<div class="row">
<div class="ride"><?php echo "$year $make $model"; ?></div>
</div>
<?php } ?>
</div>
</div>
<!--page navigation-->
<?php if(isset($next)):?>
<div class="nav">
<a href='index.php?p=<?php echo $next?>'>Next</a>
</div>
<?php endif ?>
</div>
This is something that I adapted for PHP from my days as a classic-asp programmer.
It provides a nice counter along with First, Last, Next & Previous links.
First is your sql query with two select statements depending on the number of records you want to show per page AND the total number of records. (in case the number you want to show is actually larger than the number of records in the db).
<?php
require'connections/conn.php';
$maxRows_rsList = 10; // the number of records you want to show per page
$pageNum_rsList = 0;
if (isset($_GET['pageNum_rsList'])) {
$pageNum_rsList = $_GET['pageNum_rsList'];
}
$startRow_rsList = $pageNum_rsList * $maxRows_rsList;
$query_rsList = $conn->prepare("SELECT make, model, year, carid FROM cars");
$query_limit_rsList = $conn->prepare("SELECT make, model, year, carid FROM cars LIMIT $startRow_rsList, $maxRows_rsList");
$query_limit_rsList->execute();
$row_rsList = $query_limit_rsList->fetch(PDO::FETCH_ASSOC);
if (isset($_GET['totalRows_rsList'])) {
$totalRows_rsList = $_GET['totalRows_rsList'];
} else {
$all_rsList = $query_rsList->execute();
$totalRows_rsList = $query_rsList->rowCount();
}
$totalPages_rsList = ceil($totalRows_rsList/$maxRows_rsList)-1;
$queryString_rsList = "";
if (!empty($_SERVER['QUERY_STRING'])) {
$params = explode("&", $_SERVER['QUERY_STRING']);
$newParams = array();
foreach ($params as $param) {
if (stristr($param, "pageNum_rsList") == false &&
stristr($param, "totalRows_rsList") == false) {
array_push($newParams, $param);
}
}
if (count($newParams) != 0) {
$queryString_rsList = "&" . htmlentities(implode("&", $newParams));
}
}
$queryString_rsList = sprintf("&totalRows_rsList=%d%s", $totalRows_rsList, $queryString_rsList);
$currentPage = $_SERVER["PHP_SELF"]; // so we stay on the same page just changing the recordset data
?>
Then our output
<table cellpadding="5" cellspacing="0" border="0">
<?php if($totalRows_rsList > $maxRows_rsList) { ?>
<tr>
<td colspan="2"><?php echo ($startRow_rsList + 1) ?> to <?php echo min($startRow_rsList + $maxRows_rsList, $totalRows_rsList) ?> of <?php echo $totalRows_rsList ?> cars<br />
<table border="0">
<tr>
<?php if ($pageNum_rsList > 0) { // Show if not first page ?>
<td width="25" nowrap="nowrap">
First
</td>
<?php } // Show if not first page ?>
<?php if ($pageNum_rsList > 0) { // Show if not first page ?>
<td width="25" nowrap="nowrap">
Prev
</td>
<?php } // Show if not first page ?>
<?php if ($pageNum_rsList < $totalPages_rsList) { // Show if not last page ?>
<td width="25" nowrap="nowrap">
Next
</td>
<?php } // Show if not last page ?>
<?php if ($pageNum_rsList < $totalPages_rsList) { // Show if not last page ?>
<td width="25" nowrap="nowrap">
Last
</td>
<?php } // Show if not last page ?>
</tr>
</table></td>
</tr>
<?php } else if(($totalRows_rsList == $maxRows_rsList) || ($totalRows_rsList < $maxRows_rsList)) { ?>
<tr><td colspan="2"> </td></tr>
<tr><td colspan="2"><?php echo ($startRow_rsList + 1) ?> to <?php echo min($startRow_rsList + $maxRows_rsList, $totalRows_rsList) ?> of <?php echo $totalRows_rsList ?> cars<br /></td></tr>
<?php } ?>
<tr><td>
<?php do {
$year = $row_rsList['year'];
$make = $row_rsList['make'];
$model = $row_rsList['model'];
?>
<div class="row">
<div class="ride"><?php echo "$year $make $model"; ?></div>
</div>
<?php } while($row_rsList = $query_limit_rsList->fetch(PDO::FETCH_ASSOC)) ?>
</div>
</div>
</td></tr>
</table>
$page =(int)(!isset($_GET['p']))?1: $_GET['p'];
should actually be
$page =(!isset($_GET['p']))?1: (int)$_GET['p'];
what you are doing is casting the boolean result of isset as an integer
I have a html table where i want to echo content out of a database table:
<table style="width: 100%;">
<tbody>
<tr>
<td>
<?php
$ergebnis = mysql_query("SELECT Test FROM testtable");
while($row = mysql_fetch_object($ergebnis))
{
echo $row->Test;
}
</td>
<td>
<?php
$ergebnis = mysql_query("SELECT Test2 FROM testtable");
while($row = mysql_fetch_object($ergebnis))
{
echo $row->Test2;
}
</td>
</tr>
</tbody>
</table>
Is there a way to cut short the php code, because it´s a bit too long to wirte in every
<?php
$ergebnis = mysql_query("SELECT Test FROM testtable");
while($row = mysql_fetch_object($ergebnis))
{
echo $row->Test;
}
If your example really is what you are doing you might want to rather get all data in a single query:
<?php
$ergebnis = mysql_query("SELECT Test, Test2 FROM testtable");
$recordset = mysql_fetch_array($ergebnis, MYSQL_ASSOC);
?>
<table style="width: 100%;">
<tbody>
<tr>
<?php foreach (array('Test', 'Test2') as $column) { ?>
<td>
<?php foreach ($recordset as $record) { ?>
<?php echo htmlspecialchars($record[$column], ENT_QUOTES, 'UTF-8'); ?>
<?php } ?>
<td>
<?php } ?>
</tr>
</tbody>
</table>
Also note that the mysql_* functions has been deprecated and will be removed from the language soon. If you want to make sure your code doesn't start throwing notices or even will stop functioning in the future you might want to start using either mysqli_* or PDO.
http://php.net/manual/en/mysqlinfo.api.choosing.php
Also note that I have added htmlspecialchars() to prevent possible XSS attacks.
Well, you can simply do it by having a function:
function query($a){
$ergebnis = mysql_query("SELECT $a FROM testtable");
while($row = mysql_fetch_object($ergebnis))
{
echo $row->$a;
}
}
and calling it like this:
<table style="width: 100%;">
<tbody>
<tr>
<td>
<?php
$a = 'Test';
query($a);
?>
</td>
<td>
<?php
$a = 'Test2';
query($a);
?>
</td>
</tr>
</tbody>
</table>
Create a function out of it. And use wherever you want.
You can make a function like this:
function query($a){
$ergebnis = mysql_query("SELECT $a FROM testtable");
while($row = mysql_fetch_object($ergebnis))
{
echo $row->$a;
}
}