Jump to content

Random Sorting


Riparian

Recommended Posts

I have 600 products in the database all with images.

 

Scenario

 

What I would like to do is initially display the products in random order paging after 30 images (This is no problem using sort by rand() limit 30 etc etc )

 

Problem:

When I go to page 2 and then back to page one all images that were on the first page are different.

 

Is there a simple way to randomize the display but keep them consistent  during paging?

 

Any help is greatly appreciated.

 

Cheers

 

Link to comment
Share on other sites

It happens, because, when you got back to page 1, the page generated a random results again.

I'm not sure what would be the disadvantages of this, but maybe you could try to cache your db results, then do the paging via PHP.

 

<?php 

if (!isset($_SESSION['results'])) {
$_SESSION['results'] = $all_of_your_rendomized_records;
} else {
$all_of_your_rendomized_records = $_SESSION['results'];
}

// then handle your $all_of_your_rendomized_records's pagination here using php 
?>

 

Im not sure though the disadvantages of this, if this will cosume much memory or something.

But I think it can handle with just 600 records. ;)

Link to comment
Share on other sites

Thanks for the help.

 

Unfortunately I am unsure as to how I can implement the code as the search criteria is somewhat complex (it is a very big program) i.e.

 

$_SESSION[sORT_COMBINED]=$_SESSION[WIDTH_CRITERIA].$_SESSION[sEARCH_CRITERIA].$_SESSION[EX_SEARCH_CRITERIA].$_SESSION[sHAPE_CRITERIA].$_SESSION[LENS_CRITERIA].$_SESSION[GENDER_CRITERIA].$_SESSION[bRAND_CRITERIA].$_SESSION[COLOUR_CRITERIA].$_SESSION[PRICE_CRITERIA].$_SESSION[TOTAL_WIDTH_CRITERIA].$_SESSION[size_CRITERIA].$_SESSION[sORT_CRITERIA];

 

$sql="select * from ".$_SESSION[use_table]." where in_stock='T'  ".$_SESSION[sORT_COMBINED] ;

 

from there is is a simple mysql result and while row statement.

 

I was thinking that if you are saying  I change the while to a foreach I would lose all the information in the rows[] ??

 

I hope this is clear (as mud)

 

Appreciate the help !

 

Cheers

 

Link to comment
Share on other sites

The problem I am trying to overcome is that when a user first goes onto the display page all of the products are listed in database order (they are all the same basic product with shape variations and cost differences) so the clients will initially see pages and pages of the same product.

 

I can sort by price, stock number etc etc but this does not alter the situation or present in the best light. A Jumble of colours, shapes and prices is far better.

 

This is why I tried to use rand() but if the client goes to view the product and then returns to view others that were on the same (viewed) page the original view is gone plus if moving to the next page the previously displayed images are likely to be presented again.

 

Maybe there is no answer to this or perhaps I could jumble the database itself somehow ?

 

Cheers

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.