The Do While loop works well with echo. but loop only once with a function


#1

Here is the code:

do{
   
		//Insert row content into array.
		$keywords = preg_split("#\<(.*?)\>#", $row);

		//Insert relevant data into DB


		add_data($keywords);
		
	    }
	else{
			// If row is irrelevant - continue to next row
			continue;
		}

}while (strpos($row, 'Closed P/L') != true);

Jere is the function

function add_data($keywords)
{
	global $db;

	$ticket =$keywords[2];
	$o_time = $keywords[4];
	$type = $keywords[6];
	$size = $keywords[8];
	$item = substr($keywords[10], 0,  -1);
	$o_price = $keywords[12]; 
	$s_l = $keywords[14];
	$t_p = $keywords[16];
	$c_time = $keywords[18];
	$c_price = $keywords[20];
	$profit = $keywords[28];

	try
	{
		$sql = "
			INSERT INTO `data`
			(ticket, o_time, type, size, item, o_price, s_l, t_p, c_time, c_price, profit)
			VALUES
			(:ticket, :o_time, :type, :size, :item, :o_price, :s_l, :t_p, :c_time, :c_price, :profit)";	
			

		$stmt = $db->prepare($sql);
		$stmt->bindParam('ticket', $ticket, PDO::PARAM_STR);
		$stmt->bindParam('o_time', $o_time, PDO::PARAM_STR);
		$stmt->bindParam('type', $type, PDO::PARAM_STR);
		$stmt->bindParam('size', $size, PDO::PARAM_STR);
		$stmt->bindParam('item', $item, PDO::PARAM_STR);
		$stmt->bindParam('o_price', $o_price, PDO::PARAM_STR);
		$stmt->bindParam('s_l', $s_l, PDO::PARAM_STR);
		$stmt->bindParam('t_p', $t_p, PDO::PARAM_STR);
		$stmt->bindParam('c_time', $c_time, PDO::PARAM_STR);
		$stmt->bindParam('c_price', $c_price, PDO::PARAM_STR);
		$stmt->bindParam('profit', $profit, PDO::PARAM_STR);

		$stmt->execute();
		
		//return true;	
	}
	catch(Exception $e)
		
	
	{
	   return false;
	   echo 'something is wrong. Here is the system\'s message:<br>'.$e;
	}
}

This code is supposed to insert 100 rows into the DB.

Yet when I run it, it loops only once, inserts one row and stops.

If I replace the function call with
echo $keywords[4].'<br>';
It works perfectly.

What is missing so that it will insert all rows into DB?


#2

Did you remove code? This shouldn’t be in the code you posted…

	else{
			// If row is irrelevant - continue to next row
			continue;
		}

#3

I don’t know if it is the problem, but I suspect it’s this line

}while (strpos($row, 'Closed P/L') != true); 

http://php.net/manual/en/function.strpos.php

Warning

This function may return Boolean FALSE , but may also return a non-Boolean value which evaluates to FALSE . Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

i.e. if the string position is false or at the beginning - 0 - it will be evaluated as “not loosely equal to true”.


#5

No, I didn’t remove code.
It works perfect when I remove the call to function and replace it with echo to print the data.

My problem is that when the call to function is there, it loops once, inserts 1 row to DB and stops instead of continuing to next row

The code you mentioned is there in order to skip rows with irelevant data.


#6

So there IS code that you didn’t post - there has to be because otherwise that closing bracket is on the do, which is not good (it should throw an error for mismatched brackets, actually)

Actually, I believe I know what’s causing this…it’ll ALWAYS quit as soon as it hits this, which would be after the first run through the code.

strpos will either return FALSE (meaning the text isn’t found) OR a number starting at zero meaning the text is found at position 0 (the beginning) or more. It will never return true.


#7

In the code provided, where does $row change? Surely without this being read from somewhere, the do() loop will run either never, or for ever, depending on whether the string (which never changes, here) contains “Closed P/L” or not.

But it does seem strange that it’s not complaining about the unbalanced curly-braces, and the standalone else{} clause without a corresponding if{} to trigger it. I don’t think (but I’m not 100% sure) that you can have a do() without a corresponding while() at the end, but surely it will evaluate the closing curly-brace just before the else{} clause to be that closer, as @DaveMaxwell said? And then throw a syntax error because there’s no while() clause?