Forum Moderators: coopster

Message Too Old, No Replies

Infinite Loop Problem?

Simple array issue

         

briesm

7:49 pm on May 31, 2005 (gmt 0)

10+ Year Member



My past array problem was solved...though I can't seem to execute a while loop, it gets hung up and times out...I defined all those pushed variables via post..
$field_qty1 = $_POST['qty1']; //etc. And if I change the while loop to an if statement it will execute it.

$ordersArray = array();

array_push($ordersArray, $field_qty1, $field_partID1, $field_unit1, $field_unitprice1, $field_total, $field_description1);

$arraySize = count($ordersArray);
$itr=0;

while($arraySize > $itr);
{
//Simplified Code
echo $ordersArray[$itr];
echo $ordersArray[$itr+1];
echo $ordersArray[$itr+2];
echo $ordersArray[$itr+3];
echo $ordersArray[$itr+4];
echo $ordersArray[$itr+5];

$itr = $itr+6;
}

StupidScript

8:51 pm on May 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



while($arraySize > $itr) { 

should be:

while($itr < $arraySize) { 

Also might want to try:

for($itr=0;$itr<$arraySize;$itr++) { 

...yaddayadda...

$itr=$itr+5; //compensate for increment in loop

}

Also, use

sizeof($orderArray)
instead of
count()
.

briesm

9:12 pm on May 31, 2005 (gmt 0)

10+ Year Member



TY so much. Hmmm...but I got one more question...How could I pass an array through sessions...I passed form text fields by doing:

$field_qty1 = $_POST['qty1'];

then on the previous php page as well as this one i'd have:

<?php
$field_qty1 = "";
if (isset($_SESSION['qty1'])) $field_qty1 = $_SESSION['qty1'];
elseif ($_POST['qty1']) {
$_SESSION['qty1'] = $_POST['qty1'];
$field_qty1 = $_SESSION['qty1'];
}
?>
<input type="text" name="qty1" size="5">

How could I incorporate an $ordersArray array variable for example? Any help is much appreciated...hehe, i just picked up php after doing java for a bit but my transition is not as smooth as i'd like it to be :)

StupidScript

10:58 pm on May 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Same thing. A
$_SESSION[]
variable can be a single element or another array. Try this:

<?php

session_start();

$ordersArray=array("item1","item2");

$_SESSION["orders"]=$ordersArray;

foreach($_SESSION["orders"] as $val) {

echo "Order: ".$val."<br />\n";

}

?>

briesm

1:51 pm on Jun 1, 2005 (gmt 0)

10+ Year Member



Hmmm, Still having issues...essentially, I'm pushing new elements into the array in each new generated page via the php script.

So if I have this:
$ordersArray = array();
if (isset($_SESSION['orders'])) {
$ordersArray = $_SESSION['orders'];
}

That should check to see if the session has passed along the array? If so $ordersArray receives the array?

THen I'll push the vars i want added:

array_push($ordersArray, $field_qty1, $field_partID1, $field_unit1, $field_unitprice1, $field_total, $field_description1);

and finally save the new larger array to the session for next time?
$_SESSION['orders'] = $ordersArray;

But this does not work.

mcibor

2:10 pm on Jun 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



array_push is not a good solution, as it is resources consuming. Better option is

$ordersArray[] = array($field_qty1, $field_partID1, $field_unit1, $field_unitprice1, $field_total, $field_description1);

or $_SESSION['orders[]'] = array($field_qty1, $field_partID1, $field_unit1, $field_unitprice1, $field_total, $field_description1);

moreover stupidScript, you don't have to write for($i, ... ,$i++) and then $i = $i+5, but can do it straight:

for($itr = 0; $itr < $arraySize; $itr + 6){...}

Best regards!
Michal Cibor

briesm

2:37 pm on Jun 1, 2005 (gmt 0)

10+ Year Member



But by writing the code you suggested aren't I recreating a new array. Essentially, I need to create an array in the script, add the variables that were passed along via the php, then pass along that array to the next generated page, pushing the next variables onto the end of the array. What i'm doing is outputting the array data, and creating new form fields. The user can then fill out the fields again and on the next page, what was in the array gets outputted again along with what the user just filled out in the newly created forms, and it goes on and on. So I think push is necessary for this, or at least the only clear solution i can think of right now.

As for the problem, the array does not appear to be passed on, a new array is created containing only the most recent form field values, yet I need to maintain an array each time adding the new values at the end of it. I think it's gotta be something with the way I'm saving it to the session?

StupidScript

4:39 pm on Jun 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Once again, Michal, you are the voice of reason. I gotta say, "Doh!" Too obvious, for me! ;)

briesm, are you using

session_start()
on every page? Would you please post a representative bit of code, and not just the extracted bits? It's hard to see the complete picture.

mcibor

9:33 pm on Jun 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The push_array is unnecessary

The
$ordersArray[] = array($field_qty1, $field_partID1, $field_unit1, $field_unitprice1, $field_total, $field_description1); does the trick. Such code adds and array to the end of $ordersArray. if you have in
$order = array("a", "b", "c");//And you write

$order[] = "d"; // Then you have "a", "b", "c", "d". now if you write
$order[] = array("e", "f");// Then you get "a","b","c", "d", "e", "f". Without using push

To really check your code see what's in $ordersArray before you push new values and after you push values. There may be some problem in there.

Best regards
Michal Cibor

StupidScript

9:49 pm on Jun 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is a caveat, however. Try this example:

<?php

$thisarr=array();

$thisarr[]="0";

$thisarr[]="1";

$thisarr[]="2";

$thisarr[]=array("3","4","5");

$i=0;

foreach($thisarr as $val) {

echo $i.": ".$val."<br />\n";

$i++;

}

?>

In fact,

$thisarray[3]
is an array. While it may be comma-separated, it would still need to be dealt with as an array, wouldn't it?

Here's the difference:

<?php

$thisarr=array();

$thisarr[]="0";

$thisarr[]="1";

$thisarr[]="2";

$thisarr[]=array("3","4","5");

$i=0;

foreach($thisarr as $val) {

if (is_array($val)) {

foreach($val as $subval) {

echo $i.": ".$subval."<br />\n";

$i++;

}

}

else {

echo $i.": ".$val."<br />\n";

$i++;

}

}

?>