May 4, 2012

Foreach loop only looping once?

Question by myladeybugg

I’m having trouble with my foreach loop opening multiple files.

My foreach loop is only running once for the first item number. The foreach loop runs then goes to the “api.php” page but then stops and doesn’t go back to the foreach loop to get the next item number. How do I tell it to go through all of item numbers in my database?

Would I use cURL somehow?

Thanks

Here’s my code:

$itemnumber = array("".$result['item_number']."");

foreach ($itemnumber as $item_number) {

echo "<form method="post" action="api.php" name="ChangeSubmit" id="ChangeSubmit">";
echo "<input type="text" name="item_number" value="{$item_number}" />";

echo "<script type="text/javascript">
function myfunc () {
var frm = document.getElementById("ChangeSubmit");
frm.submit();
}
window.onload = myfunc;
</script></form>";



}

Answer by Starx

You actually have only one item in your array. SO it is looping only once.

Your code $itemnumber = array("".$result['item_number'].""); will translate to

 $itemnumber = array($result['item_number']);

Because, the double double-quotes you provided have no significance in the code too. SO, at last the array will have only one item.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!