Jump to content

Limitation too early


user4fun

Recommended Posts

Jhecht, and justsomeguy have been helping me a great deal with this task and i am posting a fresh one because there is alittle problem that i am trying to work on, maybe other people would toss in their 2 cents.

$sql = "SELECT COUNT(*) as total FROM confirmed";$ans = mysql_query($sql,$link) or die(mysql_error());$total = mysql_fetch_object($ans);mysql_free_result($ans);$noLongerAllowed=array();$i = rand(0,$total->total);while(!in_array($i,$noLongerAllowed)){$ans = mysql_query($sql,$link) or die(mysql_error());$sql = "SELECT * FROM confirmed LIMIT $i,1";  $row = mysql_fetch_object($ans);	$rand_picked= $row->Website;//some things will happen to $rand_picked//open in new window url = $random_picked//the new open window is top left corner	echo $rand_picked ."<br />";//sleep( for 5 seconds)//close the newly opened window//Update column GC in table.confirmed WHERE Website == $rand_picked//the new GC value is equal to GC++ (the old value +1)			$noLongerAllowed[]=$i;			$i=rand(0,$total->total);			print_r($noLongerAllowed); }

For some reason it is stopiing after picking between 12 to 16 records. I need it to keep going untill a button in clicked that closes the window (or stop the loop) thus stopping the random selection.ORWhen maybe 30 selection are made

Link to comment
Share on other sites

Have a look at this line:

while(!in_array($i,$noLongerAllowed)){

In conjunction with some code later on this means that the program will continue selecting records until it picks one that it has already chosen, at which point it stops. Hmm... try this:

$sql = "SELECT COUNT(*) as total FROM confirmed";$ans = mysql_query($sql,$link) or die(mysql_error());$total = mysql_fetch_object($ans);mysql_free_result($ans);$noLongerAllowed=array();$i = rand(0,$total->total);for ($a = 1; $a <= 500; $a++){$ans = mysql_query($sql,$link) or die(mysql_error());$sql = "SELECT * FROM confirmed LIMIT $i,1";  $row = mysql_fetch_object($ans);	$rand_picked= $row->Website;//some things will happen to $rand_picked//open in new window url = $random_picked//the new open window is top left corner	echo $rand_picked ."<br />";//sleep( for 5 seconds)//close the newly opened window//Update column GC in table.confirmed WHERE Website == $rand_picked//the new GC value is equal to GC++ (the old value +1)			$noLongerAllowed[]=$i;			while (in_array($i, $noLongerAllowed)) {				   $i=rand(0,$total->total);			}			print_r($noLongerAllowed);}

This means that it will loop 500 times, each time selecting a new value from the array. The while loop prevents the same record from being selected twice. However, the problem with this is that if you only have, say, 600 records in your table twoards the end it will take awhile for the script to produce a new link... hmm...Also, you can't make your browser open a new window using PHP, you will have to use a client-side script like JavaScript to do so:

//open in new window url = $random_pickedecho "<script type=\"text/javascript\">\n";echo "popup = window.open('$random_picked');\n";echo "popup.moveTo(0,0);\n";echo "</script>\n";

Link to comment
Share on other sites

You need to account for several situations. One situation is if you want to display more records then you actually have. In that case, you would end up just displaying all of them. The other situation is if you have more records then you want to display. In that case, you would stop when you reach the limit. So you need to count how many records there are and decide what you want to do. Also, I said it in the other thread, but you can't make a button that will stop PHP from processing. If you want the user to control which or how many records they see, they either need to submit a form and PHP will read how many records they want and then show them, or you need to have PHP write out all the records into a Javascript array and then control how they show up on the page using Javascript.

Link to comment
Share on other sites

The pages are coming out the url being my domain then the correct value, they are not in my serveria m tyring to make it http://$rand_picked

echo "<script type=\"text/javascript\">\n";echo "popup = window.open('$rand_picked');\n";echo "popup.moveTo(0,0);\n";echo "</script>\n";

Thank alot for the informationIn regards to how many is shown, welli do have a side function that will update the number of files shown, It will eventually go through all the records, and that is fine, I would like for that to happen, there are only 85 records anywaysbut if the user decides to do something else and stop the page, then at least the few that were shown would get updated// the GC++The goal is that EVENTUALLY there will be enough records that no one will sit there and see them all. When the number of records reach that limit where it is too much. That is when the randomess comes in and the GC++ update. I can later add extra coding that would look at the GC value and view the files that were not so lukily randomly selected before. More like a median number and where the other files fall in comparison to that.It ias a big fun project and i am learning alot while doing it.Really, i am probably the only crazy User4Fun. I just like doing it. lolRight now the files will keep going untill the $a max is reached. I think, and that is exactly what i want.I do think i will make that form throug to wher a user can pick how many files they want before passing it to the php code. Offcourse the Max will me the COUNT(*) of records in the table.

Link to comment
Share on other sites

OK. Just to make sure you're understanding right, all of the commented stuff:

//open in new window url = $random_picked//the new open window is top left corner	echo $rand_picked ."<br />";//sleep( for 5 seconds)//close the newly opened window

Cannot be done with PHP. You don't use PHP to open or close windows or interact with the user in any way, and in return the user cannot interact with PHP in any way (the user cannot stop PHP from running, or change how it runs). If you make PHP sleep for 5 seconds, the user will see that as the page taking an extra 5 seconds to load. They won't see the one appear, then 5 seconds later another one appears until they hit stop. That's just not how PHP works, a browser like IE in particular waits until the entire page is downloaded before displaying anything. Anything you want to do that involves the user interacting with the page needs to be done through either forms or Javascript, those are the only possibilities.

Link to comment
Share on other sites

yeah, i am using javascript to open the url.after the javascript oens the window the php will naturally go on to the next line of code right?that is when sleep(5)after 5 secondswindow.closeafter that the code will go back to the while{until the condition is met}is that right?

Link to comment
Share on other sites

No, the PHP code finishes before the user has a chance to ever press the button. By the time the user is looking at the page in their browser, the PHP has already finished. PHP does not keep running while the user is using the page. PHP does not interact with Javascript at all other then something like AJAX. PHP will not wait for a Javascript event to happen like window.open or window.close because PHP is finished running by the time the user is able to trigger a Javascript event.You seem to be under the impression that the user will be able to interact in some way with the page while PHP is running, and that is not true. The user only has a chance to use the page after PHP has already finished running. If the user clicks on something or submits a form, then a new PHP thread starts, it doesn't continue an old one.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...