Jump to content

Recommended Posts

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 :)
Link to comment
https://forums.phpfreaks.com/topic/12469-random-display/
Share on other sites

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).

[code]<?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'];
    
}
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/12469-random-display/#findComment-47704
Share on other sites

[!--quoteo(post=386057:date=Jun 20 2006, 10:40 AM:name=Ferenc)--][div class=\'quotetop\']QUOTE(Ferenc @ Jun 20 2006, 10:40 AM) [snapback]386057[/snapback][/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).

[code]<?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'];
    
}
?>[/code]
[/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
Link to comment
https://forums.phpfreaks.com/topic/12469-random-display/#findComment-47706
Share on other sites

[!--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) [snapback]386059[/snapback][/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
}
Link to comment
https://forums.phpfreaks.com/topic/12469-random-display/#findComment-47713
Share on other sites

$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
}
Link to comment
https://forums.phpfreaks.com/topic/12469-random-display/#findComment-47721
Share on other sites

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

[code]
$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>";
}
[/code]
Link to comment
https://forums.phpfreaks.com/topic/12469-random-display/#findComment-47724
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.