RE: [PHP] display query results at random
I'm not positive on this, but I think I catch your drift. when you get the results of your query... place 1 = $results[2] place 2 = $results[0] place 5 = $results[4] Later, Jerry Lake Interface Engineering Technician -Original Message- From: Matthew Delmarter [mailto:[EMAIL PROTECTED]] Sent: Tuesday, September 25, 2001 9:15 PM To: [EMAIL PROTECTED] Cc: PHP Mailing List Subject: RE: [PHP] display query results at random Hi Lawrence, Thanks for your reply. I should clarify that I want to control where I output the result. Place 1 - show details for record 3 //I specifically want record 3 details here Place 2 - show details for record 1 //I specifically want record 1 details here Place 5 - show details for record 5 //I specifically want record 5 details here What I meant by 'random' was that it's not in the order returned by the mysql query. Matthew -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: Wednesday, September 26, 2001 3:56 PM To: [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Subject: RE: [PHP] display query results at random Return into an array, then loop through the array or shuffle it (easier)?. Note if you have nulls in DB, you will have to do something different. n'est pas? e.g. (untested) mysql_fetch_array -Original Message- From: Matthew Delmarter [mailto:[EMAIL PROTECTED]] Sent: September 26, 2001 12:01 PM To: PHP Mailing List Subject: [PHP] display query results at random How do I return a specific record from a db outside of the query? Let's say I "select * from table" (using mysql_fetch_object) and get 5 results. I want to display the 5 results in different / random places on the page. Place 1 - show details for record 3 Place 2 - show details for record 1 Place 5 - show details for record 5 How do I do this? I know how to loop thru a mysql result - but what if want to display things in a random order around the page? I could have multiple select statements - but this would increase the load on the server and is unnecessary as all the data was returned from the query - it's just a matter of knowing how to get it! Can anyone help? I am sorry if the question is confusing, but I can't think of a better way to explain it at the moment (i'm tired)... Matthew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] display query results at random
"Matthew Delmarter" <[EMAIL PROTECTED]> wrote: > How do I return a specific record from a db outside of the query? > > Let's say I "select * from table" (using mysql_fetch_object) and get 5 > results. I want to display the 5 results in different / random places > on the page. > > Place 1 - show details for record 3 > Place 2 - show details for record 1 > Place 5 - show details for record 5 > > How do I do this? Someone mentioned saving to an array. That's the way to go, but I'm not sure if the reply was clear. $i = 1; while ( $qd= mysql_fetch_object( $rs ) ) { if ( $i == 1 ) { $var1 = $qd->my_field; } else if ( $i == 2 ) { $var2 = $qd->my_field; } ... else if ( $i == 5 ) { $var5 = $qd->my_field; } } Then use $var1, $var2, ..., $var5 where needed after the loop finishes. I may be misunderstanding, so if you really want your query to return 5 records, but want to randomly pick a record for each of 5 random spots on your page then do this: $i = 1; while ( $qd= mysql_fetch_object( $rs ) ) { $var[] = $qd->my_field; $i++; } Then use array_rand() inside a loop to pull from $var[], removing each element after it's selected (more efficient) or adding code to ensure the same element isn't picked twice (less efficient). -- Steve Werby President, Befriend Internet Services LLC http://www.befriend.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] display query results at random
the sql RAND() function was mentioned, use it with ORDER BY like this : SELECT foo,bar FROM tablename ORDER BY RAND() this use of RAND() requires at least version 3.23 you can use LIMIT too : SELECT foo,bar FROM tablename ORDER BY RAND() LIMIT 10 your result will be random, loop through accordingly. see : http://www.mysql.com/doc/M/a/Mathematical_functions.html for more details. regards, Philip Olson On Wed, 26 Sep 2001, Matthew Delmarter wrote: > Hi Lawrence, > > Thanks for your reply. > > I should clarify that I want to control where I output the result. > > Place 1 - show details for record 3 //I specifically want record 3 > details here > Place 2 - show details for record 1 //I specifically want record 1 > details here > Place 5 - show details for record 5 //I specifically want record 5 > details here > > What I meant by 'random' was that it's not in the order returned by > the mysql query. > > Matthew > > -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, September 26, 2001 3:56 PM > To: [EMAIL PROTECTED] > Cc: [EMAIL PROTECTED] > Subject: RE: [PHP] display query results at random > > > Return into an array, then loop through the array or shuffle it > (easier)?. > > Note if you have nulls in DB, you will have to do something different. > > n'est pas? > > > > e.g. (untested) > > mysql_fetch_array > > mysql_connect ($host, $user, $password); > $result = mysql_db_query ("database","select user_id, fullname from > table"); > $j=0; > while ($row[$j] = mysql_fetch_array ($result)) { > $j++; > } > mysql_free_result ($result); > > srand ((float)microtime()*100); > shuffle ($row); > > //do output stuff with array > > ?> > > -Original Message- > From: Matthew Delmarter [mailto:[EMAIL PROTECTED]] > Sent: September 26, 2001 12:01 PM > To: PHP Mailing List > Subject: [PHP] display query results at random > > > How do I return a specific record from a db outside of the query? > > Let's say I "select * from table" (using mysql_fetch_object) and get 5 > results. I want to display the 5 results in different / random places > on the page. > > Place 1 - show details for record 3 > Place 2 - show details for record 1 > Place 5 - show details for record 5 > > How do I do this? > > I know how to loop thru a mysql result - but what if want to display > things in a random order around the page? I could have multiple select > statements - but this would increase the load on the server and is > unnecessary as all the data was returned from the query - it's just a > matter of knowing how to get it! > > Can anyone help? > > I am sorry if the question is confusing, but I can't think of a better > way to explain it at the moment (i'm tired)... > > Matthew > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: > [EMAIL PROTECTED] > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] display query results at random
Hi Lawrence, Thanks for your reply. I should clarify that I want to control where I output the result. Place 1 - show details for record 3 //I specifically want record 3 details here Place 2 - show details for record 1 //I specifically want record 1 details here Place 5 - show details for record 5 //I specifically want record 5 details here What I meant by 'random' was that it's not in the order returned by the mysql query. Matthew -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: Wednesday, September 26, 2001 3:56 PM To: [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Subject: RE: [PHP] display query results at random Return into an array, then loop through the array or shuffle it (easier)?. Note if you have nulls in DB, you will have to do something different. n'est pas? e.g. (untested) mysql_fetch_array -Original Message- From: Matthew Delmarter [mailto:[EMAIL PROTECTED]] Sent: September 26, 2001 12:01 PM To: PHP Mailing List Subject: [PHP] display query results at random How do I return a specific record from a db outside of the query? Let's say I "select * from table" (using mysql_fetch_object) and get 5 results. I want to display the 5 results in different / random places on the page. Place 1 - show details for record 3 Place 2 - show details for record 1 Place 5 - show details for record 5 How do I do this? I know how to loop thru a mysql result - but what if want to display things in a random order around the page? I could have multiple select statements - but this would increase the load on the server and is unnecessary as all the data was returned from the query - it's just a matter of knowing how to get it! Can anyone help? I am sorry if the question is confusing, but I can't think of a better way to explain it at the moment (i'm tired)... Matthew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] display query results at random
Return into an array, then loop through the array or shuffle it (easier)?. Note if you have nulls in DB, you will have to do something different. n'est pas? e.g. (untested) mysql_fetch_array -Original Message- From: Matthew Delmarter [mailto:[EMAIL PROTECTED]] Sent: September 26, 2001 12:01 PM To: PHP Mailing List Subject: [PHP] display query results at random How do I return a specific record from a db outside of the query? Let's say I "select * from table" (using mysql_fetch_object) and get 5 results. I want to display the 5 results in different / random places on the page. Place 1 - show details for record 3 Place 2 - show details for record 1 Place 5 - show details for record 5 How do I do this? I know how to loop thru a mysql result - but what if want to display things in a random order around the page? I could have multiple select statements - but this would increase the load on the server and is unnecessary as all the data was returned from the query - it's just a matter of knowing how to get it! Can anyone help? I am sorry if the question is confusing, but I can't think of a better way to explain it at the moment (i'm tired)... Matthew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] display query results at random
How do I return a specific record from a db outside of the query? Let's say I "select * from table" (using mysql_fetch_object) and get 5 results. I want to display the 5 results in different / random places on the page. Place 1 - show details for record 3 Place 2 - show details for record 1 Place 5 - show details for record 5 How do I do this? I know how to loop thru a mysql result - but what if want to display things in a random order around the page? I could have multiple select statements - but this would increase the load on the server and is unnecessary as all the data was returned from the query - it's just a matter of knowing how to get it! Can anyone help? I am sorry if the question is confusing, but I can't think of a better way to explain it at the moment (i'm tired)... Matthew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]