1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Setting intervals between While or Foreach actions

Discussion in 'PHP' started by gabrola, Dec 16, 2008.

  1. #1
    Well I am creating a forum autoposter for IPB board in php and I have to use while statements inside other while statements or foreach statements inside while statements.
    I use a form containing of website: url, section id, account, password.
    For each site details are seperated by a ";"
    And a messages area in the same form.
    Containing: Title, description, post. Also separated by a ";"
    So say I want to post 3 messages to 2 forums in the form of:
    Forum 1: Post message 1, 2, 3
    Forum 2: Post messages 1, 2, 3
    Since IPB has a flood control I need a 10 second interval between each message posted.
    I tried this:
    
    		$urls = explode(";", $_POST['u']);
    		reset($urls);
    		$secs = explode(";", $_POST['s']);
    		$accs = explode(";", $_POST['a']);
    		$psws = explode(";", $_POST['p']);
    		$ttls = explode(";", $_POST['title']);
    		reset($ttls);
    		$ddcs = explode(";", $_POST['desc']);
    		$psts = explode(";", $_POST['Post']);
    		while (list($key, $value) = each($urls)) {
    			while (list($key2, $value2) = each($ttls)) {
    				//Action
    				sleep(10);
    			}
    		}
    		
    PHP:
    and this
    
    		$urls = explode(";", $_POST['u']);
    		reset($urls);
    		$secs = explode(";", $_POST['s']);
    		$accs = explode(";", $_POST['a']);
    		$psws = explode(";", $_POST['p']);
    		$ttls = explode(";", $_POST['title']);
    		reset($ttls);
    		$ddcs = explode(";", $_POST['desc']);
    		$psts = explode(";", $_POST['Post']);
    		while (list($key, $value) = each($urls)) {
    			foreach ($ttls as $key2 => $value2) {
    				//Action
    			sleep(10);
    			}
    		}
    		
    PHP:
    But it didn't work.
    It waited 10 seconds then did all the actions at once.
    Please help.

    Thanks.
     
    gabrola, Dec 16, 2008 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Unfortunately. As far I can see, that's completely valid and SHOULD wait 10 seconds between each iteration.

    Dan.
     
    Danltn, Dec 16, 2008 IP
  3. gabrola

    gabrola Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    73
    #3
    Well I see it in the same way so I tried to test using a simpler method to test.
    
    while (list($key, $value) = each($urls)) {
    	foreach ($ttls as $key2 => $value2) {
    		sleep(10);
    		echo "Key: $key2; Value: {$ttls[$key2]}<br />\n";
    	}
    }
    
    PHP:
    It waited 10 seconds then everything showed in 1 second :s

    Edit: Actually say they were 5 titles total it would wait 50 seconds the all 5 lines show.
     
    gabrola, Dec 16, 2008 IP
  4. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    sleep will wait but the output is buffered - everything will be output at once as the page is only sent to the browser when complete.
     
    Yesideez, Dec 16, 2008 IP
  5. gabrola

    gabrola Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    73
    #5
    Yep, I noticed that, it turned out to be a script error anyway, and now it works :D
     
    gabrola, Dec 16, 2008 IP
  6. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Oh, another thing about sleep()...

    I can't post links properly yet: uk2.php.net/sleep

     
    Yesideez, Dec 16, 2008 IP