views:

43

answers:

1

I'm trying to create a php script which retries another php script up to 3 times until an error is displayed. I'm thinking perhaps this could be done using a php loop? If the code works successfully the first time, then there is no need for it to retry 3 times, however, if it doesn't work the first time, then it should retry the php script up to 3 times till an error message is displayed.

Using php coding, I've managed to make a script which grabs/fetches content from another location using "file_get_contents" and thereafter gives each word/data a php variable. All this was done by getting help from other members on stackoverflow (which I extremely appreciate). The code below is what does it all:

$searchdata = file_get_contents('http://www.example.com');

list($no1, $no2, $no3, $no4, $no5, 
     $no6, $no7, $no8, $no9) = explode(" ", $searchdata);

So, I'd like to add some sort of loop which retries this script up to 3 times; if it doesn't work the first time. To determine whether the script works the first/second/third time or not, the text "#endofscript" or "failure" should be found when using "file_get_contents". If anything else is displayed other than "#endofscript" or "failure" that should be counted as an error and should be looped till found. If it still isn't found after the third try, could an error message be displayed? Such as "Error - Please try again".

Thank you for all your assistance and I will appreciate each and every single reply. If you need more details, please feel free to ask. And again, I'm really grateful for this. :)

+2  A: 
$maxTries = 3;
for ($try=1; $try<=$maxTries; $try++) {
    // your code
    if ($success) {
        break;
    }
}

// if $try > 3, script failed
webbiedave
Hi webbiedave, I extremely appreciate your reply :). I understand how the code works slightly, however, I believe at the moment the code doesn't know when it's a success as the "$success" variable is empty. How would I go about incorporating the "#endofscript" and "failure" text as a success and anything else as a error? Also, how do I go about showing an error message if it doesn't work the 3rd time? Kind regards, and I extremely appreciate your response.
AUllah1
What about: `if ($no9 == '#endofscript') { $success = true; } else { $success = false; }` and `if ($try > 3) { echo 'Error - Please try again'; }`
webbiedave
Thank you for the reply webbiedave, I am really grateful of them. Those codes seems like they could do the job, however, how would I go about incorporating those? Would they look something like:$maxTries = 3;for ($try=1; $try<=$maxTries; $try++) {// your codeif ($no9 == '#endofscript') { $success = true; } else { $success = false; } {break;}if ($try > 3) { echo 'Error - Please try again'; }}And also, when it displays the error message, is it possible to have it as a php variable? So that I can display the message else where on page?
AUllah1
And apologies for the hassle but isn't the $success variable empty? As in it doesn't mean anything? Again, I appreciate your response. Thank you for everything. :)
AUllah1
Then how about an upvote ;)
webbiedave
Lol, to do so I need at least 15 reputation; I don't have that. But, I can put this question as accepted answer. :)
AUllah1