I have created a gallery using a database. All the images are showing randomly on Home page. Now I need to add a new "Latest"index.php/latest.php category to my navigation bar.
I'm using infinite scroll and a little trick to order randomly$rand=date("i");.
Question: ORDER BY 1 DESC is not working on index.php/latest.php, because I have ordered by randomly on the Home page. How could I order by descending order on the Latest page without changing the Home page order?
index.php
<?php
include ("sqli.php");
$rand=date("i");
$seed=($rand);
$per_page = 9;
if (isset($_GET["page"])) {
$page = $_GET["page"];
}
else {
$page=1;
}
$start_from = ($page-1) * $per_page;
$query = "SELECT * FROM data ORDER BY RAND($rand) LIMIT $start_from, $per_page";
$result = mysqli_query ($con, $query);
<?php while ($row = mysqli_fetch_array($result)) { ?>
?>
<div id="gallery here"></div>
<?php> } ?>
latest.php
<?php
include ("sqli.php");
$per_page = 9;
if (isset($_GET["page"])) {
$page = $_GET["page"];
}
else {
$page=1;
}
$start_from = ($page-1) * $per_page;
$query = "SELECT * FROM data ORDER BY 1 DESC LIMIT $start_from, $per_page";
$result = mysqli_query ($con, $query);
?>
<?php while ($row = mysqli_fetch_array($result)) { ?>
?>
<div id="gallery here"></div>
<?php> } ?>
You have to set order by createdAt field(by your date field) in last.php:
$query = "SELECT * FROM data ORDER BY createdAt DESC LIMIT $start_from, $per_page";
This issue was solved by me. This issue related to infinite-scroll navigation. I hope my PHP code will help to someone. You can use this PHP code for jQuery Infinite-Scroll pagination. I'm here if you have any question. Thanks all.
Related
Below is the code, here i have used "select count(*) from departments" rather then using "select * from departments" so where am i wrong in this code. Can anyone help me out to sort it..
$num_rec_per_page=5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page=1;
};
$start_from = ($page-1) * $num_rec_per_page;
$sql_query = "SELECT * FROM departments LIMIT $start_from, $num_rec_per_page";
$result = $db_connection->query($sql_query);
if($result->num_rows > 0){
while($rows = $result->fetch_assoc()){
echo "<tr>";
echo "<td>".$rows["id"]."<td>";
echo "<td>".$rows["name"]."<td>";
echo "<tr>";
}
}
$sql = "SELECT count(*) FROM departments"; //select query for total records
$rs_result = $db_connection->query($sql); //run the query
$total_records =$rs_result->num_rows; //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='index.php?page=1'>".'|<'."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='index.php?page=".$i."'>".$i."</a> ";
};
echo "<a href='index.php?page=$total_pages'>".'>|'."</a> ";
I think that your only problem is the way you are trying to fetch total number of records. Set the SQL query to make the count and then retrieve it by reading the only row returned:
$sql = "SELECT count(*) AS total_records FROM departments";
$rs_result = $db_connection->query($sql);
$total_records = $rs_result->fetch_assoc()['total_records'];
$sql = "SELECT count(*) FROM departments"; //select query for total records
$rs_result = $db_connection->query($sql); //run the query
$total_records =$rs_result->num_rows; //count number of records
Ok, you should select .
Select count() would return a single number (number of rows). //count number of records
Which is already being extracted later on.
Total records is select * from departments
Number of records is select count(*) from departments
I'm having trouble with the following code from a tutorial, it seems very simple but there must be something I'm missing. There is nothing at all where my numbered page menu should be, can anyone see why?
if (isset($_GET["page"])) { // get page number for query
$page = $_GET["page"];
}
else {
$page = 1; // no page number? set it
};
$start_from = ($page-1) * 20;
$query = "SELECT * FROM posts";
$query.= " WHERE isstart = 'y' AND iscomplete = 'n' ORDER BY date DESC LIMIT $start_from, 20";
$start_from, 20"; // use LIMIT (and options) to make sure only 20 are displayed
$result = mysql_query($query);
$query_count = "SELECT COUNT(post_ID) FROM posts WHERE isstart = 'y'";
$count_result = mysql_query($query_count);
$count_results = mysql_fetch_row($count_result);
$total_posts = $count_results[0];
$total_pages = ceil($total_posts / 20); // get total pages needed for page menu
for ($i=1; $i<=$total_pages; $i++) { // set the page numbers
$pagelink = "Page: <a href='index_test.php?page=".$i."'>".$i."</a>"; // make the page menu
};
$top_body_text = '<p align="left">'.$pagelink.'</p>';
Currently my statement echo'ing $pagelink creates noting.
First thing, there is a error on this line
$start_from, 20";
It should be
$start_from = 20;
and lastly, you need to echo '$pagelink' inside the for loop in order to see each pages number listed out. Example below:
if (isset($_GET["page"]))
{ // get page number for query
$page = $_GET["page"];
}
else {
$page = 1; // no page number? set it
};
$start_f = 20; // use LIMIT (and options) to make sure only 20 are displayed
$start_from = ($page-1) * $start_f;
$query = "SELECT * FROM posts";
$query.= " WHERE isstart = 'y' AND iscomplete = 'n' ORDER BY date DESC LIMIT $start_from, 20";
$result = mysql_query($query);
$query_count = "SELECT COUNT(post_ID) FROM posts WHERE isstart = 'y'";
$count_result = mysql_query($query_count);
$count_results = mysql_fetch_row($count_result);
$total_posts = $count_results[0];
$total_pages = ceil($total_posts / 20); // get total pages needed for page menu
for ($i=1; $i<=$total_pages; $i++)
{ // set the page numbers
echo $pagelink = "Page: <a href='index_test.php?page=".$i."'>".$i."</a>"; ///Echo here
};
$top_body_text = '<p align="left">'.$pagelink.'</p>';
Remove this line -- it's not doing anything:
$start_from, 20"; // use LIMIT (and options) to make sure only 20 are displayed
You'd already defined $start_from here:
$start_from = ($page-1) * 20;
Then check out your results and see if you are all set.
I have this code which splits the mysql results into 10 results per page. but I want to edit it so that it limits links based on what the current page is. so on pages 1-10 it only shows page links 1-10 on pages 11-20 it only shows page links 11-20 and so on. how could I achieve this? Thanks.
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else
{
$page=1;
};
$start_from = ($page-1) * 10;
$message = "SELECT * FROM document WHERE email='$_SESSION[email]' ORDER BY id DESC LIMIT $start_from , 10";
// echo results
// make page links for results
$sql = "SELECT id FROM document WHERE email = '$_SESSION[email]'";
$rs_result = $db->query($sql);
$total_records = $rs_result->num_rows;
$total_pages = ceil($total_records / 10);
if($rs_result->num_rows >10) {
$page = "<p class = 'page'>";
for ($i=1; $i<=$total_pages; $i++)
{
$page .="<a href='results.html?page=".$i."'>".$i."</a> ";
}
$page .="</p>";
echo $page;
}
Do this
"SELECT * FROM document WHERE email='$_SESSION[email]' ORDER BY id DESC LIMIT 10 OFFSET $start_from";
Tell me if it works.
Im trying to get this next/previous image thing to work. But i don't know how to get this right. I only get the id 88 and 87, not the ones before that.
www.wallpapers.puffys.net
In my index.php page i got this code to show the image and get the next/previous image.
include 'db_connect.php';
#Last
$sql = "SELECT id FROM images WHERE validated=1 ORDER BY id DESC LIMIT 0,1";
$result = mysql_query($sql) or trigger_error(mysql_error()."".$sql);
$row = mysql_fetch_array($result);
$last = $row['id'];
#Next
$sql = "SELECT id FROM images WHERE validated=1 AND id>'".$last."' ORDER BY id DESC LIMIT 0,1";
$result = mysql_query($sql) or trigger_error(mysql_error()."".$sql);
$row = mysql_fetch_array($result);
$next = $row['id'];
#Previous
$sql = "SELECT id FROM images WHERE validated=1 AND id<'".$last."' ORDER BY id DESC LIMIT 0,1";
$result = mysql_query($sql) or trigger_error(mysql_error()."".$sql);
$row = mysql_fetch_array($result);
$previous = $row['id'];
?>
<!--Scripts-->
<script type="text/javascript" src="resources/jquery-1.7.1.js"></script>
<script>
$(document).ready(function() {
function showLoader() {
$('.loader').fadeIn(200);
}
function hideLoader() {
$('.loader').fadeOut(200);
}
$(".next").click(function() {
showLoader();
$("#wallpaper").load("get.php?id=<? echo $next; ?>", hideLoader);
return false;
});
$(".previous").click(function() {
showLoader();
$("#wallpaper").load("get.php?id=<? echo $previous; ?>", hideLoader);
return false;
});
showLoader();
$("#wallpaper").load("get.php?id=<? echo $last; ?>", hideLoader);
});
</script>
The get.php page:
include 'db_connect.php';
$id = mysql_real_escape_string($_GET['id']);
if ($id) {
$sql = "SELECT COUNT(*) FROM images WHERE id='".$id."' AND validated=1";
$result = mysql_query($sql) or trigger_error(mysql_error()."".$sql);
$num_rows = mysql_fetch_row($result);
if ($num_rows[0] == 1) {
$sql = "SELECT image FROM images WHERE id='".$id."' AND validated=1";
$result = mysql_query($sql) or trigger_error(mysql_error()."".$sql);
$row = mysql_fetch_array($result);
echo '
<img src="'.$row['image'].'" width="1024" height="576" alt="wallpaper" border="0">
';
}
else {
}
}
else {
}
?>
I know i am doing this wrong, but i don't know how to get this working. So i would hope someone could help me out.
Your problem is most likely this;
#Next
$sql = "SELECT id FROM images WHERE validated=1 AND id>'".$last.
"' ORDER BY id DESC LIMIT 0,1";
You get all IDs greater than the last, and order from highest to lowest which will give you the highest id available. You'll want to change it around to;
#Next
$sql = "SELECT id FROM images WHERE validated=1 AND id>'".$last.
"' ORDER BY id ASC LIMIT 0,1";
which will sort in the correct order and give you the lowest ID that is higher than the last.
This is my solution:
$NEW_ID = 0;
$SQL_FUNCTION = "SELECT ID FROM TableName";
$SQL_DATA = MYSQL_QUERY($SQL_FUNCTION);
WHILE($SQLDAT = MYSQL_FETCH_OBJECT($SQL_DATA)){
$ID = "$SQLDAT->ID";
if($NEW_ID < $ID){$NEW_ID = $ID;}
}
//NOW INSERT WITH THIS ID...
But I'm not sure if it works when more people simultaneously do this!?
(I would ask this question but i'm closed)
I have a MySQL query
SELECT * FROM 'redirect'
WHERE 'user_id'= \''.$_SESSION['user_id'].' \'
ORDER BY 'timestamp'`
I want to paginate 10 results per page. How Can I do it?
Here is a nice starting point:
<?php
// insert your mysql connection code here
$perPage = 10;
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$startAt = $perPage * ($page - 1);
$query = "SELECT COUNT(*) as total FROM redirect
WHERE user_id = '".$_SESSION['user_id']."'";
$r = mysql_fetch_assoc(mysql_query($query));
$totalPages = ceil($r['total'] / $perPage);
$links = "";
for ($i = 1; $i <= $totalPages; $i++) {
$links .= ($i != $page )
? "<a href='index.php?page=$i'>Page $i</a> "
: "$page ";
}
$r = mysql_query($query);
$query = "SELECT * FROM 'redirect'
WHERE 'user_id'= \''.$_SESSION['user_id'].' \'
ORDER BY 'timestamp' LIMIT $startAt, $perPage";
$r = mysql_query($query);
// display results here the way you want
echo $links; // show links to other pages
Use LIMIT.
SELECT *
FROM redirect
WHERE user_id = '35251'
ORDER BY timestamp
LIMIT 40, 10
40 is how many records to skip, 10 is how many to display.
There are also a few problems with your PHP. You use backticks (not single quotes) to surround table and column names. And you shouldn't use string concatenation to build your query.
Here is my code
which contains next and Previous button
<?php
$limit = 3; //set Number of entries to show in a page.
// Look for a GET variable page if not found default is 1.
if (isset($_GET["page"])) {
$page = $_GET["page"];
}
else { $page=1;
}
//determine the sql LIMIT starting number for the results on the displaying page
$page_index = ($page-1) * $limit; // 0
$All_Users=mysqli_query($con,"select * from users limit $page_index, $limit");
while($row=mysqli_fetch_array($All_Users))
{
//show data in table or where you want..
}
$all_data=mysqli_query($con,"select count(*) from users");
$user_count = mysqli_fetch_row($all_data); // say total count 9
$total_records = $user_count[0]; //9
$total_pages = ceil($total_records / $limit); // 9/3= 3
if($page >= 2){
echo "<a href='blog.php?page=".($page-1)."' class='btn
customBtn2'>Previous</a>";
}
if($page<$total_pages) {
echo "<a href='blog.php?page=".($page+1)."' class='btn customBtn2'>NEXT</a>";
}
?>
Use the LIMIT clausule of the query to limit the amount of results you retrieve from the database.
See: http://dev.mysql.com/doc/refman/5.1/en/select.html