George L.
George L.

Reputation: 65

Display random records in each page but not the same

Well guys i have this query

$mysql = "select * from xxx where active = 1 order by Rand() limit $start,12";
mysql_query($mysql);

Everything works great so far.

I want: when i am pressing the next button (page 2 or three etc) to see the next 12 random records but do not display the first 12 random records that i had in my previus page!

Thank you all! p.s Sorry guys for my bad english!

Upvotes: 2

Views: 434

Answers (5)

Ali
Ali

Reputation: 1

SOLUTION - that works a treat.

  1. do a select random search of all required records
  2. generate a random user-id eg. "smith".rand(1000,10000)
  3. form a string of all random keys upto required no of records per page
  4. insert above in a table/field containing a corresponding page no.
  5. repeat/loop above until no more pages/recs remaining - use array_splice(str,from,to) - then use $notscreen = print_r($splice, true) for string storage to table -> randompages:

tb.rec-no | user-id | pageno | string ( with keys of recs upto recs/page)

122 | aj7894 | p1 | [0]=>100[1]=>400[2]=>056[3]=>129

123 | aj7894 | p2 | [x]99=>[x]240=>[x]7895[x]458=>320

... upto whole array of pages /no of records / all pages - no duplication of data - only 1-column of key of recs stored in random as retrieved

  1. use user-id & pageno with WHERE to pull out random keys for that individual user & page

  2. convert string back to array and pull out matching key recs for specific pages using the array in a SELECT WHERE query with implode

  3. re-circ [ user-id & pageno ] using $_GET/POST for duration of search/view - reinitialise when new view or new search commences

notes:

-better to use list for search - but requires more work to format string - should give random page results as originally stored

  • problem with array matching is it orders records per page; lowest being first - not so random for the page display

  • temp table no good - because cannot be accessed when script is thrown back to server for 2nd and more time - it's lost from memory by mysql

  • php rules - no flimsy cookies or java-script !

BIG PROBLEM - SOLVED.

re-compsense for help received from your posts / answers.

Happy Days!

Upvotes: 0

Jurgo
Jurgo

Reputation: 905

You can keep track of the previously shown records' ids and put them in an array.

In your query use id NOT IN (array)

Upvotes: 1

Suriyamoorthy Baskaran
Suriyamoorthy Baskaran

Reputation: 635

Apply the concept of Systematic Random Sampling,

  • Number the records N, decide on the n (pagination size, eg: 10, 20)
  • (sample size) that you want or need k = N/n = the interval size
  • Randomly select an integer between 1 to k then take every k th unit

Refer: http://www.socialresearchmethods.net/kb/sampprob.php

Try using the following script in your showdata.php file

$per_page = 12; 
$sqlc = "show columns from coupons"; 
$rsdc = mysql_query($sqlc); 
$cols = mysql_num_rows($rsdc); 
$page = $_REQUEST['page']; 
$start = ($page-1)*12; 

$N = 1000; //Total rows in your table (query to get it dynamically)
$n = $per_page;
$k = ceil($N/$n);

$range[] = $page;

for($i=1;$i<$n;$i++) {
 $range[] = ($page+$k)*$i;
}

$sqln = "SELECT * FROM ( SELECT @rownum:= @rownum+1 AS rindex, n.* FROM xxx n, (SELECT @rownum := 0) r ) AS rows WHERE rindex IN (".implode(',',$range).")";
$rsd = mysql_query($sqln); 

Upvotes: 0

Ajay Khedkar
Ajay Khedkar

Reputation: 73

You need to keep one array (e.g $arrRecordIds) to track all the id's of records shown on previous pages.

When you are on first page:

$arrRecordIds=array(); // Empty array

When you are on second page:

$arrRecordIds=array_merge($arrRecordIds, $arrNewRecordIds);array_unique( $arrRecordIds );

If your select query simply concat- where id NOT IN ( implode(',', $arrRecordIds ) )

Here $arrNewRecordIds should contains id's of the records on the page.

Upvotes: 1

Fry_95
Fry_95

Reputation: 241

Just try to retrieve the data you need in an array, randomize it with shuffle() in PHP, and paginate the result with some JQuery, it will be awesome, just one query and no refresh. ;)

Upvotes: 1

Related Questions