how to paginate data fetched from databse - php

lets say if there are 13 records the latest 5 or from 9-13 in the first page,
from 4-8 in second page and 1-3 in the third page
i've tried this but its for the first page only
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mysqli_login";
// Create connection
$connection= new mysqli($servername, $username, $password, $dbname);
$query = mysqli_query($connection, "SELECT name,submittedby,trn_date FROM new_record ORDER BY id DESC LIMIT 5")or die(mysqli_error($connection));
while ($row = mysqli_fetch_array($query)) {
$fileName = $row['name'];
$fileContents = file_get_contents("txt/$fileName");
$poster = $row['submittedby'];
$date = $row['trn_date'];
echo ("posted by :$poster | posted date : $date");
echo ("$fileContents");
}
?>

Your code should be like this
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test_db";
// Create connection
$connection= new mysqli($servername, $username, $password, $dbname);
$current_page = 1; // 1=>refer to the first page, 2=> second page and so on..
if(!empty($_GET['page_no']){
$current_page = $_GET['page_no'];
}
$to = "5"; // it is no of record which you wants to show on each page, you can change it's value as per your need.
$from = ($current_page - 1) * $to;
$query = mysqli_query($connection, "SELECT name,submittedby,trn_date FROM new_record ORDER BY id DESC LIMIT $from , $to")or die(mysqli_error($connection));
while ($row = mysqli_fetch_array($query)) {
$fileName = $row['name'];
$fileContents = file_get_contents("txt/$fileName");
$poster = $row['submittedby'];
$date = $row['trn_date'];
echo ("posted by :$poster | posted date : $date");
echo ("$fileContents");
}
?>
your url should be something like http://localhost/projects/?page_no=1
replace "http://localhost/projects/" with your actual url
Hope this will help!!

Here's an article that discusses this topic. You need to offset your query based on the current page. So it will be LIMIT 5 OFFSET <amount based on the page>.

Related

How can I set variable php and mysql code

I want to set a variable "name",
I tried: if(isset($_POST['name'])), But not result.
I was also looking for other options but could not get results.
I have a PHP code:
<?php
$servername = "localhost";
$username = "..";
$password = "..";
$dbname = "..";
$conn = new mysqli($servername, $username, $password, $dbname);
$response = array();
$posts = array();
$sql = "SELECT name, addres, status, date FROM silknet WHERE name='test'";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result)) {
$name=$row['name'];
$addres=$row['addres'];
$status=$row['status'];
$date=$row['date'];
$response[] = array('name'=> $name, 'addres'=> $addres, 'status'=>$status,
'date'=>$date);
}
echo json_encode($response);
?>
Add to the top:
$name = $_POST['name'];
And edit this line as:
$sql = "SELECT name, addres, status, date FROM silknet WHERE name='$name'";

how to make previous and next button in php

I'm fetching my songs from MySQL database and I'll be adding more song in the futures so I can't hard coded the max number so I'll be really appreciated if I can get any help or suggestion how to do it.
you can use the following query to get the total records in DB
$query = 'SELECT count(*) as total FROM song';
for checking if the max is reached
<? if ($page*$recordsPerPage < $total) : ?>
Next
complete example for hiding the next
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test2";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (!isset($_GET['id'])) {
$id = 1;
} else {
$id = (int)$_GET['id'];
}
$recordsPerPage = 10 ;
// $query = 'SELECT * FROM song WHERE 1 LIMIT ' . (($id - 1) * $recordsPerPage) . ' ' . $recordsPerPage;
//Here need to handle the songs data retreive
$sql = "select count(*) as total FROM songs";
$result = $conn->query($sql);
$data = $result->fetch_assoc();
if ($id*$recordsPerPage < (int)$data["total"])
echo "<a href='page/details.php?id=". ($id+1) ."'>Next</a>";
$conn->close();

Show records of a particular year in one page

<?php
$hostname = "localhost";
$user = "root";
$password = "";
$dbname = "abcd";
$conn = new mysqli($hostname, $user, $password, $dbname);
$sql="SELECT YEAR(date) as year from events GROUP BY year";
$row2 = mysqli_fetch_array(mysqli_query($conn,$sql));
$result2 = $conn->query($sql);
$eyear=$row2['year'];
$year="2017";
for ($counter = 1; $counter <=5; $counter++){
echo'<p>'.$year.'</p>';
$year--;
}
?>
I want to retrieve records of separate years and display results for a particular year in one page

mysql can't return a row result

I am connected to the database, a page has lots of content so I'll only share the part that doesnt return a value in php, but it returns a value in MySQL
Here is the code;
$query = "SELECT firstname FROM users WHERE id = '17'";
$query_run = mysql_query($query);
$row = mysql_fetch_row($query_run);
echo $row[0];
Changing the code I shared first to this, solved the problem. Thanks to anyone who tried to help.
$query = "SELECT firstname FROM users WHERE id = '17'";
$query_run = mysqli_query($conn, $query);
$row = mysqli_fetch_row($query_run);
echo $row[0];
And made bit changes to the connect.inc.php which I also shared in comment.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "notsitesi";
$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error) {
die("Connection failed");
}
?>

How to retrieve images from database in php

I have a database which has a table called 'propImages' and there are two columns.- 'pid' and 'location'.
And i have data in the database where multiple images can contained by single pid.
image contains database data
now i want to retrieve images from database according to given pid. there can be more than one image.
All i know it there should be an iteration to retrieve images.
I want to display images in HTML .
can you please show me the way to do it in php?
Thanks in advance guys
This may help you
<?php
include 'inc/database.php';
$conn = new mysqli($servername, $username, $password, $database);
$propid = $_GET['propid'];
$sql = "SELECT * FROM propImages WHERE propid='" . $propid . "';";
$result = $conn->query($sql);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<img src=" . $row['image'] . ">";
}
}
else {
echo "No results";
}
?>
in the inc/database.php :
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "database";
?>
To see how it works try visiting : file.php?propid=22
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "databasename";
// Create connection
$con = mysqli_connect($servername, $username, $password, $dbname);
//create sql
$sql = "SELECT * FROM `propImages` where pid='$YOUR_PID'";
$result = mysqli_query($con, $sql);
$row = mysqli_num_rows($result);
//retrive data print here
if($row > 0){
while($col = mysqli_fetch_assoc($result))
{
echo $col['location'];
}
} else {
echo 'no result found.';
}
?>
wish it helps

Categories