Random values in MySql query with pagination - php

I have advanced search on my web page, now how this works is as follows. When a search is made, random results appear on the content page, now this page also included pagination, so my problem is everytime the visitor goes to the 1st page different results appear. Is it possible to use pagination with this, or will the ordring always be random.
I'm using the a query like
SELECT * FROM table ORDER BY RAND() LIMIT 0,20;

You should use a seed for the MySQL RAND to get consistent results. In PHP you do a
$paginationRandSeed = $_GET['paginationRandSeed']?
( (int) $_GET['paginationRandSeed'] ):
rand()
;
and in MySQL you use that seed
"SELECT * FROM table ORDER BY RAND(".$paginationRandSeed.") LIMIT 0,20"
Of course you'll need to propagate the initial seed in the page requests.
Good luck,
Alin

Related

paginating random list sql

Im working in a basic pagination list where I need all the results be retrieved ramdomly
this is why I use
SELECT *
FROM table
WHERE 1
ORDER BY rand()
It works well until I need to paginate it...
SELECT *
FROM table
WHERE 1
ORDER BY rand()
LIMIT $offset, $recordsperPage
How do I retrieve the whole list in random order, but when paginated, every page do not repeat the prior random words?
If you provide a consistent seed to the rand() call, you'll get the same sequence. You might use something derived from the date, for example, to give the same results for the day, or generate some other random seed from PHP and save it in the session, to give the same results only for a given visitor.

Solution Mysql Random query migrate when goto another page

I have table Post, user can search Post by find by keyword.
So, i using SQL query by like keyword and order by RAND().
When show result, i using GridView and Pagination data. And has problem, when go to another page. Example from page 1 go to page 2 with same keyword. i Will Query again and order by Rand(). So some data in page 1 can appear in page 2.
That data duplicate and not good.
So how can i solve this problem. and data query when goto page other will same with data in the first query.
I using Yii2 in my project.
Ok, I think I get it. Example MySQL query:
SELECT * FROM articles WHERE name LIKE '%tag%' LIMIT 0, 20;
Or you can use MySQL's MATCH() instead of LIKE, it doesn't matter. Then LIMIT 0, 20 is limit, how many results to show for the first page. For the second page it should be LIMIT 1, 20. This one you know I except. Then you get an array of results in the PHP, and want to get random values. PHP has such a function for you and it is called suffle().
So you just use shuffle($results); and then you can print them with foreach or whatever you use to print the data. Note that shuffle() returns boolean, so do not use $results = shuffle($results);. Hope this helps.

php fetch database, displaying them customized.

During fetching data from database with $result = mysqli_query($con,"select * from MY_table, it display 100 columns at a time. my main aim is to display 10 columns at a time, with the help of next and previous buttons user gonna read remaining. is it possible to do with Javascript , or AJAX, or PHP alone.
The word you're looking for is 'pagination'. You can divide your query results into pages. PHP can fetch a part of the query results, and you can click on a button or link to fetch the next page. That next page can be loaded by an entire page refresh, or by Ajax. In either case, PHP fetches the next results and returns them.
9lessons.info has a tutorial on this exact same topic. Well, more a bunch of snippets than a tutorial, but still..
If you're just starting: forget about Ajax at first. Start with reading about the LIMIT clause in MySQL. It allows you to query a subresult of the query, by limiting the returned rows to a specific range.
You can pass a page (or a range) in the url, so when you click 'Next page', you can just call PHP again with ?page=2 or something in the url. Based on that number, you can query a different range of rows.
The last step is to update only a part of the page using Ajax instead of doing a full page refresh.
You can use limit to your sql query to do this
$start = 0;
$result = mysqli_query($con,"select * from MY_table limit ".$start.", 10");
initially $start will be 0 the change the $start variable as per your prev next ..

PHP/MySQL Show first X results, hide the rest

Does anyone know how to bring in all of a mysql tables' results, only show the first X, (say 10), and then hide the rest using jquery? I just need to know how to show only the first X results in one page, then the rest in a seperate page using href.
My aim is to only show the first 10 results, but provide a link at the bottom of the page allowing the user to show all of the results. Was thinking the hyperlink could just re-execute the query but thought it would be easier to show/hide using jquery.Thanks
Rather than using jquery, you can use MySQL to retrieve only the desired results.
SELECT * FROM TableName LIMIT 10
That will retreive the first 10 results. Then, to display the next 10 on the next page,
SELECT * FROM TableName LIMIT 10 OFFSET 10
and the next page
SELECT * FROM TableName LIMIT 10 OFFSET 20
If you are using PHP, you can build your query to show the correct results depending on which page you are on:
// $currentPage is set elsewhere in the script, and is zero-based.
$resultsPerPage = 10;
$currentOffset = $resultsInPage * $currentPage;
$query = "SELECT * FROM TableName LIMIT $resultsInPage OFFSET $currentOffset";
Actually you are searching for Pagination. There are some useful jQuery paginations plugins:
jQuery pagination plugin
DataTables Plugin
Or using PHP:
Pagination in PHP
You need pagination look at the some tutorial to do this. there are lot of code examples on this do googling
here are some links
http://www.codediesel.com/php/simple-pagination-in-php/ -
http://php.about.com/od/phpwithmysql/ss/php_pagination.htm
It might be "easier", but it's a lot less resource efficient to load all the data (and then optionally show it) in the manner you're describing.
I'd really recommend using multiple queries (perhaps via Ajax/lazy loading) rather than using the approach you're discussing.
I'd do this with a LIMIT statement in the Select-statement of your SQL query. In most cases there's no need to send all the data to the client and then let javascript hide the data you don't wanna show.

Sorting by date in reviews

In many reviews, there's usually a feature to sort by date, helpfulness, etc... Essentially these work by querying a new MySQL line right?
For example:
$sql = "SELECT * FROM TABLE ORDER BY TimeAdded"
$sql = "SELECT * FROM TABLE ORDER BY Helpfulness"
Or would there be a better way to do it?
Secondly, making pages for reviews. Is it as simple as using a loop and a limit in the MySQL query to show 10 results per page?
Edit:
For a huge review site, would letting MySQL handle all the sorting be ideal?
You could either let the database manage the ordering as you have there (recommended) or you could load all the results and then sort them using your PHP code. Alternatively, you could put them all into HTML and sort them using JavaScript.
For pagination, you can use the LIMIT and OFFSET clauses (or LIMIT 10,10) to page through a resultset (of course, if the resultset changes, your pagination may also change).
SELECT * FROM posts ORDER BY helpfulness LIMIT 10,10
OR
SELECT * FROM posts ORDER BY helpfulness LIMIT 10 OFFSET 10
For combining pagination and sorting, I'd definitely recommend leaving it up to the database to handle.
You got it right on both counts, Doug.
For a huge review site, you still want MySQL to sort the results, you just want to make use of server-side optimizations (a good my.cnf, server-side caching, etc) and code caching. That's a whole 'nother ball of wax, but the basics are basically the same.

Categories