Re: Randomly select 5 records from a db
| From: | Monte Ohrt | Date: | Tue, 03 Oct 2000 13:41:56 +0000 |
| Subject: | Re: Randomly select 5 records from a db | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-18417@lists.php.net to get a copy of this message | ||
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