Jump to content

Archived

This topic is now archived and is closed to further replies.

ben1989

random display

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

Share this post


Link to post
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]

Share this post


Link to post
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

Share this post


Link to post
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
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
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
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.