Page 1 of 1

creating a loop for printing invoice detail (need help here)

Posted: Sun Nov 20, 2011 3:17 pm
by grabber_grabbs
i want to make the code to print up to 100 detail lines of an invoice.
If $name_X has a value, then print the line with all the info (itemno_X, Name_X, Qty_X, Price_X etc. etc..)

In my code i did 20 lines manually and it is printing the invoice right, no problem. So the only thing missing is creating the loop do print up to 100 lines.
Here is the code i have to print 20 lines :

Code: Select all

if( $NAME_0 ) {$strMessageBody .= "<TR ><TD>$ID_0 </TD><TD> $NAME_0</TD><TD align='right'>$QUANTITY_0</TD><TD align = 'right'>$PRICE_0 </TD><TD align = 'left'>/$UofM_0</TD><TD align = 'right'>$extvalue</TD></TR>";}
if( $NAME_1 ) {$strMessageBody .= "<TR><TD>$ID_1 </TD><TD> $NAME_1</TD><TD align='right'>$QUANTITY_1</TD><TD align = 'right'>$PRICE_1 </TD><TD align = 'left'>/$UofM_1</TD><TD align = 'right'>$extvalue</TD></TR>";}
if( $NAME_2 ) {$strMessageBody .= "<TR><TD>$ID_2 </TD><TD> $NAME_2</TD><TD align='right'>$QUANTITY_2</TD><TD align = 'right'>$PRICE_2 </TD><TD align = 'left'>/$UofM_2</TD><TD align = 'right'>$extvalue</TD></TR>";}
etc etc.... for 20 lines....

So basically, it is printing the line IF there is information inside $NAME_X

I am sure there is something that can be done to loop it 100 times avoiding to write 100 lines manually.
(Waiting for Celauran here :D )

Code: Select all

for ($x = 0; $x <= 100; $x++) {

  $STRNAME = '$NAME_' . $x ;
  $STRQTY = '$QUANTITY_' . $x;
  etc... etc... for all the other fields of info.

  if ($STRNAME) {$strMessageBody .= "<TR ><TD>$STRNAME </TD><TD>$STRQTY </TD></TR>";}

}
now, with the way the code is written, it is printing the varialbe name instead of the content of it.

thanks again for helping.

Re: creating a loop for printing invoice detail (need help h

Posted: Sun Nov 20, 2011 7:37 pm
by McInfo
Spend a few minutes learning about arrays.

Instead of writing code like

Code: Select all

<?php
$item_0 = 'a';
$item_1 = 'b';
for ($i = 0; $i < 2; ++$i) {
    echo ${"item_$i"};
}
write code like

Code: Select all

<?php
$items = array (
    0 => 'a',
    1 => 'b',
);
for ($i = 0; $i < 2; ++$i) {
    echo $items[$i];
}

Re: creating a loop for printing invoice detail (need help h

Posted: Sun Nov 20, 2011 9:14 pm
by Celauran
Your '$QUANTITY_' is not being evaluated because you're using single rather than double quotes.

I'm curious about why you're using $NAME_1, $NAME_2, etc. Seems inefficient. Where is this data coming from? How is it stored? How is it being retrieved?

Re: creating a loop for printing invoice detail (need help h

Posted: Sun Nov 20, 2011 9:49 pm
by grabber_grabbs
the data is stored in the customer cookies... these are the cart data that i want to process and to send an Email to the customer with the cart items.

Re: creating a loop for printing invoice detail (need help h

Posted: Mon Nov 21, 2011 6:16 am
by Celauran
Are they stored as an array you could traverse using foreach?

Re: creating a loop for printing invoice detail (need help h

Posted: Mon Nov 21, 2011 8:32 am
by grabber_grabbs
ok, all the cart data is transfered to my php code via a html form with Post method. My cart is displayed on the cust side, and while listing all the items, i am making an <input hidden to store all the info memory item_0, item_1 item_2 etc... to be sent when the form is submited. The cart data is not stored in cookies (was at the beginning until i noticed that i could only put 50 items in the cart because of cookies space limitation, using frames to store data now). All the data is well transfered into the server, no problem. The only thing is that (and if i cannot fix the loop, this not a big problem, i will cut an past until i reach up to 100 lines....) i have to write the code to print all the cart item into the invoice individually... $item_0 , $item_1 etc.... until i reach the end of the carts..
I was thinking of creating a for loop that would print all the $item_0, $item_1 etc until isset($item_X) return false

Re: creating a loop for printing invoice detail (need help h

Posted: Mon Nov 21, 2011 1:09 pm
by grabber_grabbs
Celauran wrote:Your '$QUANTITY_' is not being evaluated because you're using single rather than double quotes.

I'm curious about why you're using $NAME_1, $NAME_2, etc. Seems inefficient. Where is this data coming from? How is it stored? How is it being retrieved?
Well i could as well be using an other variable... this is just to check if the variable exist.... if it exist, then print it. So if nothing in variable $NAME_10, there is nothing into all the others ($QTY_10, $NAME_10 etc etc)