Jump to content

Rand Winner


user4fun

Recommended Posts

$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `confirmed` ");while ($offset_row = mysql_fetch_object($offset_result)) {$offset = $offset_row->offset;$result = mysql_query( " SELECT * FROM `confirmed` LIMIT $offset, 1 " ); echo ".$result['Email'].";//more codesleep (3); //sleep for 3 seconds//more code}

The problemThe table has column 1 ConfirmedIDcolumn 2 Usernamecolumn 3 UCcolumn 4 GCI need to create a loop and the cycle would keep going$Picked = the value of $Username in the radomly selected row //more codecreate a 3 second sleep() where the code would stop for 3 seconds then//more codeat the end of more code itowuld go back and select another random row

Link to comment
Share on other sites

hmm... thats a toughieHow about something like this:

$sql = "SELECT COUNT(*) as total FROM confirmed";$ans = mysql_query($sql) or die(mysql_error());$total = mysql_fetch_object($ans);mysql_free_result($ans);$noLongerAllowed=array(); $i = rand(0,$total->count); //get a random offset; $count=0;while(!in_array($i,$noLongerAllowed) && $count<$total->count){ $sql = "SELECT * FROM confirmed LIMIT $i,1"; $ans = mysql_query($sql) or die(mysql_error());  $row = mysql_fetch_object($ans);	//Do whatever you like now;  //Don't touch this though  $noLongerAllowed[]=$i;  //This index is no longer allowed, so lets say so; $i=rand(0,$total->count); $count++;}

That should do it i believe...

Link to comment
Share on other sites

WOWit will take a week to figure out what you did, loli will go hunting with what you gave me so i would understand itthe place wher you put in////get a random offset;is that where i would put my $offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `confirmed` ");

Link to comment
Share on other sites

WOWit will take a week to figure out what you did, loli will go hunting with what you gave me so i would understand itthe place wher you put in////get a random offset;is that where i would put my $offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `confirmed` ");

Link to comment
Share on other sites

You shouldn't have to alter the code i gave you at all. Try it out and see if it works. If it doesnt, post back any errors or such that occur and I'll help you work them out.

Link to comment
Share on other sites

$sql = "SELECT COUNT(*) as total FROM confirmed";$ans = mysql_query($sql) or die(mysql_error());$total = mysql_fetch_object($ans);mysql_free_result($ans);$noLongerAllowed=array();$i = rand(0,$total->count);//get a random offset;$count=0;while(!in_array($i,$noLongerAllowed) && $count<$total->count){$sql = "SELECT * FROM confirmed LIMIT $i,1";$ans = mysql_query($sql) or die(mysql_error());  $row = mysql_fetch_object($ans);	//Do whatever you like now;	echo $row->user_column_here."<br />";	//Replace this line with the actual user column. Seeing as i dont know what that is, i just had to guess.  //Don't touch this though  $noLongerAllowed[]=$i;  //This index is no longer allowed, so lets say so;$i=rand(0,$total->count);$count++;}

the echo statement is what you should alter. I dont know the username column's name so i had to guess.

Link to comment
Share on other sites

here is the full code just in case, the page comes out empty

$link = mysql_connect("mysql", "", "") or die ("No connection");   mysql_select_db("business") or die ("no database");  $sql = "SELECT COUNT(*) as total FROM confirmed";$ans = mysql_query($sql) or die(mysql_error());$total = mysql_fetch_object($ans);mysql_free_result($ans);$noLongerAllowed=array();$i = rand(0,$total->count);//get a random offset;$count=0;while(!in_array($i,$noLongerAllowed) && $count<$total->count){$sql = "SELECT * FROM confirmed LIMIT $i,1";$ans = mysql_query($sql) or die(mysql_error());  $row = mysql_fetch_object($ans);	//Do whatever you like now;	echo $row->Website "<br />";  //Don't touch this though  $noLongerAllowed[]=$i;  //This index is no longer allowed, so lets say so;$i=rand(0,$total->count);$count++;}

Link to comment
Share on other sites

Now its time for debug.

ini_set('display_errors',1);error_reporting(E_ALL);$link = mysql_connect("mysql", "", "") or die ("No connection");   mysql_select_db("business") or die ("no database");  $sql = "SELECT COUNT(*) as total FROM confirmed";$ans = mysql_query($sql,$link) or die(mysql_error());$total = mysql_fetch_object($ans);echo $total->count."<br />\n";mysql_free_result($ans);$noLongerAllowed=array();$i = rand(0,$total->count);//get a random offset;echo $i."<br />\n";while(!in_array($i,$noLongerAllowed)){$sql = "SELECT * FROM confirmed LIMIT $i,1";echo $sql."<br />";$ans = mysql_query($sql,$link) or die(mysql_error());  $row = mysql_fetch_object($ans);	//Do whatever you like now;	echo $row->Website ."<br />";  //Don't touch this though  $noLongerAllowed[]=$i;  //This index is no longer allowed, so lets say so;$i=rand(0,$total->count);  echo $i."<br />";  echo "<pre>\n";  print_r($noLongerAllowed);  echo "</pre>\n";}

Try that and tell me if there's any output or error messages

Link to comment
Share on other sites

GOT SOMETHINGNotice: Undefined property: count in /ucic/rand3.php on line 17Notice: Undefined property: count in /ucic/rand3.php on line 200SELECT * FROM confirmed LIMIT 0,1WWW.MYSITE.COMNotice: Undefined property: count in /ucic/rand3.php on line 430Array( [0] => 0)

Link to comment
Share on other sites

GOT SOMETHINGfULL ccODE AGAIN

ini_set('display_errors',1);error_reporting(E_ALL);$link = mysql_connect("mysql", "", "") or die ("No connection");   mysql_select_db("business") or die ("no database"); $sql = "SELECT COUNT(*) as total FROM confirmed";$ans = mysql_query($sql,$link) or die(mysql_error());$total = mysql_fetch_object($ans);echo $total->count."<br />\n";mysql_free_result($ans);$noLongerAllowed=array();$i = rand(0,$total->count);//get a random offset;echo $i."<br />\n";if (in_array($i,$noLongerAllowed))echo "no loop"; while(!in_array($i,$noLongerAllowed)){$sql = "SELECT * FROM confirmed LIMIT $i,1";echo $sql."<br />";$ans = mysql_query($sql,$link) or die(mysql_error());  $row = mysql_fetch_object($ans);	//Do whatever you like now;	echo $row->Website ."<br />";  //Don't touch this though  $noLongerAllowed[]=$i;  //This index is no longer allowed, so lets say so;$i=rand(0,$total->count);  echo $i."<br />";  echo "<pre>\n";  print_r($noLongerAllowed);  echo "</pre>\n";}

Link to comment
Share on other sites

It is because i am an idiot and i forgot the ?> at the end of the php section.sorryAfter making your changes i am getting thisNotice: Undefined property: count in /ucic/rand3.php on line 1718SELECT * FROM confirmed LIMIT 18,1www.choconet1.com63Array( [0] => 18)SELECT * FROM confirmed LIMIT 63,1www.randgnovelties.com7Array( [0] => 18 [1] => 63)SELECT * FROM confirmed LIMIT 7,1www.annsnoveltiesandgifts.com5Array( [0] => 18 [1] => 63 [2] => 7)does that mean it is working

Link to comment
Share on other sites

lol wow i feel smart right now. in my head for some reason i thought i named the variable count in the query... whoops.Here, i edited this. Shouldn't give any errors now

ini_set('display_errors',1);error_reporting(E_ALL);$link = mysql_connect("mysql", "", "") or die ("No connection");   mysql_select_db("business") or die ("no database");$sql = "SELECT COUNT(*) as count FROM confirmed";$ans = mysql_query($sql,$link) or die(mysql_error());$total = mysql_fetch_object($ans);echo $total->count."<br />\n";mysql_free_result($ans);$noLongerAllowed=array();while($i = rand(0,$total->count) && !in_array($i,$noLongerAllowed)){$sql = "SELECT * FROM confirmed LIMIT $i,1";echo $sql."<br />";$ans = mysql_query($sql,$link) or die(mysql_error());  $row = mysql_fetch_object($ans);	//Do whatever you like now;	echo $row->Website ."<br />";  //Don't touch this though  $noLongerAllowed[]=$i;  //This index is no longer allowed, so lets say so;  echo "<pre>\n";  print_r($noLongerAllowed);  echo "</pre>\n";  mysql_free_result($ans);   //Saves table overhang.}

That should give the same effect. If not then just revert back to the original code(after replacing all items of $total->count with $total->total instead). If you want to limit the amount of rows displayed(as this script should go for every single row in the database) let me know and it's about a 2 line solution to limit the number of rows displayed.

Link to comment
Share on other sites

I would like it to cycle through the whole databasethe final code

ini_set('display_errors',1);error_reporting(E_ALL);$link = mysql_connect("mysql", "", "") or die ("No connection");   mysql_select_db("business") or die ("no database"); $sql = "SELECT COUNT(*) as count FROM confirmed";$ans = mysql_query($sql,$link) or die(mysql_error());$total = mysql_fetch_object($ans);echo $total->total."<br />\n";mysql_free_result($ans);$noLongerAllowed=array();while($i = rand(0,$$total->total ) && !in_array($i,$noLongerAllowed)){$sql = "SELECT * FROM confirmed LIMIT $i,1";echo $sql."<br />";$ans = mysql_query($sql,$link) or die(mysql_error());  $row = mysql_fetch_object($ans);	//Extra stuff coming;	echo $row->Website ."<br />";   $noLongerAllowed[]=$i;	echo "<pre>\n";  print_r($noLongerAllowed);  echo "</pre>\n";  mysql_free_result($ans);   }

right now i am getting thisNotice: Undefined property: total in /ucic/rand3.php on line 18Notice: Object to string conversion in /ucic/rand3.php on line 22Notice: Undefined variable: Object in /ucic/rand3.php on line 22

Link to comment
Share on other sites

Sorry, my bad. Here, this should finally work

<?php//the error_reporting and ini_set thing were just for debuging purposes.$link = mysql_connect("mysql", "", "") or die ("No connection");   mysql_select_db("business") or die ("no database");$sql = "SELECT COUNT(*) as count FROM confirmed";$ans = mysql_query($sql,$link) or die(mysql_error());$total = mysql_fetch_object($ans);mysql_free_result($ans);$noLongerAllowed=array();while($i = rand(0,$total->count ) && !in_array($i,$noLongerAllowed)){$sql = "SELECT * FROM confirmed LIMIT $i,1";echo $sql."<br />";$ans = mysql_query($sql,$link) or die(mysql_error());  $row = mysql_fetch_object($ans);	//Extra stuff coming;	echo $row->Website ."<br />";  $noLongerAllowed[]=$i;   //this index is no longer allowed  mysql_free_result($ans);  //Saves database overhang  }?>

Link to comment
Share on other sites

cool, it ispicking a random row and display the website value, now what needs to be changed to make it print the website value,a nd then move to another oneuntil it cycles throught the databse or the user hits the stop button on a form that i am placing this with

Link to comment
Share on other sites

First off, a stop button on a form won't stop PHP. If you want the user to be able to control that, then you need to use AJAX or just plain Javascript instead. PHP will finish before they ever have a chance to press the button, because PHP runs on the server, not in the browser. The server finishes doing what it's doing with PHP, then the browser can do things like push a button to make something happen. The only way the form communicates with the server is when you submit it.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...