Riparian Posted October 8, 2011 Share Posted October 8, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/248678-random-sorting/ Share on other sites More sharing options...
iblood Posted October 8, 2011 Share Posted October 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/248678-random-sorting/#findComment-1277175 Share on other sites More sharing options...
Riparian Posted October 9, 2011 Author Share Posted October 9, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/248678-random-sorting/#findComment-1277323 Share on other sites More sharing options...
fenway Posted October 9, 2011 Share Posted October 9, 2011 Go back after how long? When do you want it to be random again? How did you decide it should be random the first time? Quote Link to comment https://forums.phpfreaks.com/topic/248678-random-sorting/#findComment-1277493 Share on other sites More sharing options...
Riparian Posted October 9, 2011 Author Share Posted October 9, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/248678-random-sorting/#findComment-1277598 Share on other sites More sharing options...
fenway Posted October 10, 2011 Share Posted October 10, 2011 Then you need to store the product order in the session. Quote Link to comment https://forums.phpfreaks.com/topic/248678-random-sorting/#findComment-1277912 Share on other sites More sharing options...
requinix Posted October 10, 2011 Share Posted October 10, 2011 RAND allows you to seed the number generator. Create a seed, store it in the session or page form or wherever, and use that in your query every time. Quote Link to comment https://forums.phpfreaks.com/topic/248678-random-sorting/#findComment-1277930 Share on other sites More sharing options...
Riparian Posted October 11, 2011 Author Share Posted October 11, 2011 Very much appreciate the help... will "Solve" this and work on your suggestions. Cheers and thanks Quote Link to comment https://forums.phpfreaks.com/topic/248678-random-sorting/#findComment-1277999 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.