RE: [PHP] Randomly select 5 records from a db

From: Date: Wed, 04 Oct 2000 01:22:07 +0000
Subject: RE: [PHP] Randomly select 5 records from a db
Groups: php.general 
Request: Send a blank email to php-general+get-18507@lists.php.net to get a copy of this message
Right, but what if you use an older version of MySQL (like in my case and the case of the guy who first asked this question) or (especially this issue) if you have 5 database entries to show on a page, then selecting an item the page has to change a little (like open the item deeper) while everything else is still there in the same order. But, the page HAS to be refreshed, what with a simple MySQL function would reorganize all the data. You then will need to keep the pointer on the same row to keep the order unchanged. (see www.j-door.com/experts.html). So what do you do then? everything will be repulled from the database again.. the best solution is to have a PHP function which creates you a random order basing on the number of items found. (The while loop will limit it to 5 like while(this happen) { ...your staff... if($x==5) Break;$x++; } then the random value (the pointer) will be registered by sessions. Then you just use a simple if statement, and In case the page does not need to be reRandomized the value from sessions is used giving you the exactly same order as it was a click ago. This is a real random(). Cheers! -----Original Message----- From: mohrt@ispi.net [mailto:mohrt@ispi.net]On Behalf Of Monte Ohrt Sent: Tuesday, October 03, 2000 10:42 PM To: Maxim Maletsky Cc: ben@digitalsmiths.com Subject: Re: [PHP] Randomly select 5 records from a db ORDER BY RAND() is supported in MySQL 3.23 and later. So to get what you are looking for: SELECT * FROM meminfo WHERE type='bene' ORDER BY RAND() LIMIT 5; Maxim Maletsky wrote: > > This is what I used ( when I had the exactly same question ) : > > function rand_num_array($count) > { > $stand_a = array(); > $rand_a = array(); > > for( $i = 0; $i < $count; $i++ ) > $stand_a[$i] = $i; > > $rand_max = $count; > for( $i = 0; $i < $count; $i++ ) > { > $rand_index = rand(0, $rand_max); > if( $rand_index == $rand_max ) > $rand_index = $rand_max - 1; > $rand_a[$i] = $stand_a[$rand_index]; > for( $j = $rand_index; $j < $rand_max - 1; $j++ ) > $stand_a[$j] = $stand_a[$j + 1]; > > $rand_max--; > } > > return $rand_a; > } > > And in your connection: > > if(!$id || !isset($array_ra)) > { > $array_ra = array(); > $array_ra = rand_num_array($v_mysql->Num); > } > > while( list($rand_key, $rand_val) = each($array_ra)) > { > $v_mysql->seek( $rand_val ); > $v_mysql->fetch_record(); > ..... > } > > Sorry I use a class for the mysql database, but I hope you will figure out > how to change it into "your way" > The $array_ra is registered by sessions, so in case you do something on the > page and it has to be refreshed > your data will not be resorted again. It is sorted randomly only in case you > access it for the first time (makes no sense to do it twice) > > Id is assigned only when someone selects a record, so also in this case the > data remains in order you saw it first time. > > See www.j-door.com/experts.html to understand what I am talking about, > refresh the staff few times and see the differences. > > Cheers!! > > P.S: my need for random sorting was that when we sale a place on our website > our clients often asked : "here will we be placed , on top or at the bottom? > " So I decided to answer: "It will depend on your luck" > > -----Original Message----- > From: Ben Weinberger [mailto:ben@digitalsmiths.com] > Sent: Tuesday, October 03, 2000 7:11 AM > To: php-general@lists.php.net > Subject: [PHP] Randomly select 5 records from a db > > Hi~ > I am constructing part of a PHP4 page that I would like to randomly select > 5 records from my MySQL db and display them. I know how to do the standard > selects and display the data, but nothing random. The database lists > benefits of being a member of our association, and we would like to display > different ones each time the query is performed-- is there a way to > randomly query the db for 5 records? > > I tried using the following but it didn't work.... > > $sql= "SELECT * from meminfo where type='bene'"; > $result= mysql_query($sql); > > while ($myrow= mysql_fetch_array($result)) { > $benefits=$myrow["title"]; > print "$benefits<BR>"; //this is just to test to make sure it's pulling the > data I want > > srand ((double) microtime() * 10000000); > $input = array ("$benefits", 5); > $rand_keys = array_rand ($input); > print $input[$rand_keys[0]]."\n"; > print $input[$rand_keys[2]]."\n"; > print $input[$rand_keys[3]]."\n"; > print $input[$rand_keys[4]]."\n"; > print $input[$rand_keys[5]]."\n"; > } > > Thanks, > -Ben > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net > > ------------------------------------------------------------------------ > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#18507) next »