I am new to php. I am trying to run a loop 5 times. Each time the loop runs I need to run a batch file and test the value of a query. If the query is > 0 exit the loop. Otherwise run a different batch file and continue.

Start Loop
Run an exec(batchfile)
SELECT COUNT(item_code) AS ItemCount
FROM items
ItemCount > 0
yes - exit loop and continue with code
No
Repeat up to 5 times
If after 5 times, exit loop and run exec(RunRefresh2.bat)
Continue wtih code

I don't know which type of loop to run the necessary syntax. Can anyone help?

Recommended Answers

All 3 Replies

You would need two loops, one that would handle your conditional repetitions and one which handled the execution of your batch files.

I would probably start with a for loop for the repetitions and a do-while loop for the batch statements.

I will research and see how to do this.

Here is the code I used in my project

$a=0;
exec('c:\Inetpub\wwwroot\RunRefresh.bat') ; //refresh table	
sleep(2);
$sql="SELECT COUNT(item_code) AS ItemCount FROM items";//run query
$stmt = sqlsrv_query( $conn, $sql); //query
$rows=sqlsrv_fetch_object($stmt); //get row
while($a<20 && $rows->ItemCount==0)
{
	exec('c:\Inetpub\wwwroot\RunRefresh.bat') ; //refresh table	
	sleep(2);
	$sql="SELECT COUNT(item_code) AS ItemCount FROM items";//run query
	$stmt = sqlsrv_query( $conn, $sql); //query
	$rows=sqlsrv_fetch_object($stmt); //get row
	$a++;
}
/*$sql="SELECT COUNT(item_code) AS ItemCount FROM items";//run query
$stmt = sqlsrv_query( $conn, $sql); //query
$rows=sqlsrv_fetch_object($stmt); //get row*/
if($rows->ItemCount==0 && $time<20)//if table returns an empty result	 and t< 20 refresh process, then...
{
	_exec('c:\Inetpub\wwwroot\RunRefresh2.bat'); //execute
	sleep(20);     //sleep
	if(empty($time)) //if it's the first time, add $time=1;
	{
		$time=1;
	}
	else
	{
		$time++; //otherwise plus
	}
	header('Location:http://MyServer/RunRefresh.php?t='.$time); ///refresh with t variable
}
else
{
	echo '<script>alert("All items have been refreshed")</script>';
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.