Jump to content


Photo

random display


  • Please log in to reply
8 replies to this topic

#1 ben1989

ben1989
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 20 June 2006 - 03:24 PM

hi i have been registerd b4 but i forget all mi login stuff lol

im building a php site but on the home page i would like it to pull rando info from a table

the table is labed members and the rows are

ID, membername, location, country, age, gender

i would like it to randoly pull the id and display the info for that id if u know what i mean

thnaks

-ben

need for info just ask :)

#2 Ferenc

Ferenc
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts

Posted 20 June 2006 - 03:40 PM

Something like this would work, as long as the id's are numerical, and don't have gaps in them (1,2,5,6 will error periodicly).

<?php
$sql = "SELECT * FROM members";
$result = mysql_query($sql)or die (mysql_error());
// get the total
$total = mysql_num_rows($result) or die (mysql_error());
// get a random id from the data base..
$rand = rand(1,$total);
$sql .= " WHERE ID = '$rand'";

$rand_sql = mysql_query($sql) or die (mysql_error());
while ($output = mysql_fetch_array($rand_sql)){
    echo $output['membername']."<br>";
    echo $output['location'];
    
}
?>

Everything you want to know is here.

#3 ben1989

ben1989
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 20 June 2006 - 03:46 PM

[!--quoteo(post=386057:date=Jun 20 2006, 10:40 AM:name=Ferenc)--][div class=\'quotetop\']QUOTE(Ferenc @ Jun 20 2006, 10:40 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Something like this would work, as long as the id's are numerical, and don't have gaps in them (1,2,5,6 will error periodicly).

<?php
$sql = "SELECT * FROM members";
$result = mysql_query($sql)or die (mysql_error());
// get the total
$total = mysql_num_rows($result) or die (mysql_error());
// get a random id from the data base..
$rand = rand(1,$total);
$sql .= " WHERE q_id = '$rand'";

$rand_sql = mysql_query($sql) or die (mysql_error());
while ($output = mysql_fetch_array($rand_sql)){
    echo $output['membername']."<br>";
    echo $output['location'];
    
}
?>
[/quote]

hah ill try that thanks m8


exelent thnaks you just one last thing how can i do it so it dose it 5 times??

selects the random id 5 times and echos all 5 results?


thanks -ben

#4 Ferenc

Ferenc
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts

Posted 20 June 2006 - 03:57 PM

[!--quoteo(post=386059:date=Jun 20 2006, 09:46 AM:name=ben_stringer)--][div class=\'quotetop\']QUOTE(ben_stringer @ Jun 20 2006, 09:46 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
hah ill try that thanks m8
exelent thnaks you just one last thing how can i do it so it dose it 5 times??

selects the random id 5 times and echos all 5 results?
thanks -ben
[/quote]


place it in a for loop

for($i = 0; $i < 5 ;$i++){

//code to display 5x
}
Everything you want to know is here.

#5 ben1989

ben1989
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 20 June 2006 - 04:06 PM

hmm im havin a alittle truble were do i pu it coz i have just tryed and i get the same 5 times lol i am a bit off a newbi sorry

#6 Ferenc

Ferenc
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts

Posted 20 June 2006 - 04:29 PM

$sql = "SELECT * FROM members";
$result = mysql_query($sql)or die (mysql_error());
// get the total
$total = mysql_num_rows($result) or die (mysql_error());
// get a random id from the data base..

// begin for loop
for($i = 0; $i < 5 ;$i++){
$rand = rand(1,$total);
$sql .= " WHERE ID = '$rand'";

$rand_sql = mysql_query($sql) or die (mysql_error());
while ($output = mysql_fetch_array($rand_sql)){
echo $output['membername']."<br>";
echo $output['location'];

}
// end for loop
}
Everything you want to know is here.

#7 ben1989

ben1989
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 20 June 2006 - 04:32 PM

im gettin 1 result lol and an error You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE ID = '1'' at line 1

#8 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 20 June 2006 - 04:35 PM

ditch all that. you don't need php to do that for you when you can have mysql do it for you.

$sql = "SELECT * FROM members ORDER BY RAND() limit 5";
$result = mysql_query($sql);

while ($list = mysql_fetch_array($result)) {
   foreach ($list as $key => $val) {
      echo $key . " : " . $val . " ";
   }
   echo "<br>";
}

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#9 Ferenc

Ferenc
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts

Posted 20 June 2006 - 04:37 PM

Yes, that is much easier...
Everything you want to know is here.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users