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.