dcsimg
www.webdeveloper.com
Results 1 to 6 of 6

Thread: PHP Need help please

  1. #1
    Join Date
    Sep 2006
    Posts
    18

    PHP Need help please

    This coding...

    PHP Code:
    $content $_POST;
      
    $body '';
      for(
    $i=1$i $content['itemCount'] + 1$i++) {
    $name 'item_name_'.$i/* product name variable */
    $quantity 'item_quantity_'.$i/* product quantity variable */
    $price 'item_price_'.$i/* product price variable */
    $total $content[$quantity]*$content[$price]; /* product total price variable (price*quantity) */
    $grandTotal += $total/* accumulating the total of all items */
    $body .= 'Item #'.$i.': '.$content[$name]."\n".'Qty x '.$content[$quantity].' --- Unit Price $'.number_format($content[$price], 2'.''')."\n".'Subtotal $'.number_format($total2'.''')."\n"/* creating a semantic format for each ordered product */
    $body .= '=================================='."\n";

    Produces this....
    Code:
    ==================================
    Item #1: 1-15+B-1
    Qty x 12 --- Unit Price $41.00
    Subtotal $492.00
    ==================================
    Item #2: 1-30+B-1
    Qty x 122 --- Unit Price $31.00
    Subtotal $3782.00
    ==================================
    Item #3: 1-0.5+HS+C
    Qty x 1 --- Unit Price $115.00
    Subtotal $115.00
    ==================================
    Which is exactly what I need BUT I also need to pull the individual elements into hidden fields in a button that will send this info to a shopping cart. My current code is:
    PHP Code:
    for($i=1$i $content['itemCount']+1$i++) {
    echo 
    '<input type="hidden" name="Qty" value="'.$content[$quantity].'">
    <input type="hidden" name="Item" value="'
    .$content[$name].'">
    <input type="hidden" name="Price" value="'
    .$content[$price].'">
    <input type="hidden" name="Shipping" value="-1">'
    ;} 
    But this simply takes the last item and repeats it for as many times as their are items - if there are 3 items posted, it will repeat the 3rd item 3 times. How can I get it to output each of the 3 items?
    Last edited by NogDog; 09-18-2013 at 10:48 AM. Reason: added [php] and [code] tags

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,673
    You either need to move that code into the first for() loop so that $content gets redefined on each iteration, or else duplicate that portion of the code in your second for() loop, so that $content gets redefined each time there (which would break the "Don't Repeat Yourself" [DRY] principle of programming ).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Sep 2006
    Posts
    18
    I'm not sure I understand. If I do a dump on the current code at the top (the first for() loop)-everything comes out fine. One programmer suggested that I needed to number the fields because I can't have duplicate field names - that produced price_1, price_2, etc. but the content was still just that of the last item. Are you suggesting that I create new variables for the form?

  4. #4
    Join Date
    Sep 2006
    Posts
    18
    Got it! I created a new variable called $data and entered it in the first for() section at the top like this:
    $data .='<input type="hidden" name="Qty" value="'.$content[$quantity].'">
    <input type="hidden" name="Item" value="'.$content[$name].'">
    <input type="hidden" name="Price" value="'.$content[$price].'">
    <input type="hidden" name="Shipping" value="-1">';

    Then, in between the <form> tags I simply put <?php echo $data ?> and it's working now! Thanks NogDog - you got me thinking.

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,673
    Quote Originally Posted by mawells View Post
    Got it! ... Thanks NogDog - you got me thinking.
    Great: it's always nice to see someone here who doesn't need everything served up on a silver platter.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,589
    I can only add that you also employ some form of sanitizing routine, simply passing the contents of $_POST to another variable does not sanitize the inputs.

    Example of a simple routine.

    PHP Code:
    function clean($value){
        return 
    stripslasheshtmlentities$value ));
    }
        
    $content = array(
                
    "name"=>"",
                
    "quantity"=>"",
                
    "itemCount"=>"",
                
    "price"=>""
                
    );

    // parse the POST values and store in the content as they are now clean            
    foreach( $content as $k=>$v )
        
    $content$k ] = clean$_POST[$k] ); 
    Then the $content array will contain cleaned values to avoid any possible script breaking or possible SQL Injection.
    If your post falls off the page, bump it. ...
    Please remember to wrap any code you have in forum tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles