Jump to content


Photo

Pulling random data.


  • Please log in to reply
3 replies to this topic

#1 skatermike21988

skatermike21988
  • Members
  • PipPipPip
  • Advanced Member
  • 100 posts
  • LocationFlorida

Posted 04 September 2006 - 08:08 PM

I am working on a project of creating a community script, i am wanting to pull 2-3 random new or old people from my database useing mysql, kinda like myspace's "cool new people" and i am having a hard time getting it to work this is my code:

//Pull and Display cool peorple 
$sql = mysql_query("SELECT display_name FROM users LIMIT 2") or die (mysql_error()); 
while($row = mysql_fetch_array($sql)){ 
    $row_array[] = $row['display_name'];
}
$random_row = $row_array[rand(1, count($row_array) - 1)]; 

$data = mysql_query("SELECT * FROM users WHERE display_name='$random_row'") 
or die(mysql_error()); 
while($info = mysql_fetch_array( $data ))
 { 
Print "<a href='http://www.profiles.friendshideout.com/?friendid=".$info['user_id'] . " "; 
Print "&sid=$sid' target='_self'>";

Print "<img src='".$info['default_photo'] . "' height='75' width='75'>"; 
echo "<br>$random_row</a></td>";

}
echo "</tr></table>";

When useing that code it is only pulling one person out and it is always the same.

Any help is appreciated.

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 04 September 2006 - 08:19 PM

Looks like far too much code. For a relatively small database, change your query to do the random selection for you:

//Pull and Display cool random people 
$sql = mysql_query("SELECT display_name FROM users ORDER by rand() LIMIT 2") or die (mysql_error()); 
... plus some more

Legend has it that reading the manual never killed anyone.
My site

#3 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 04 September 2006 - 08:21 PM

$random_row = $row_array[rand(1, count($row_array) - 1)];

Change that to:

$random_row = $row_array[rand(0, count($row_array) - 1)];

Remember, array index starts at 0.

#4 skatermike21988

skatermike21988
  • Members
  • PipPipPip
  • Advanced Member
  • 100 posts
  • LocationFlorida

Posted 05 September 2006 - 05:12 AM

thanks worked pefect :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users