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 09:48 AM. Reason: added [php] and [code] tags

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    22,335
    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 ).
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  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
    22,335
    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.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  6. #6
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,876
    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.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode 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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.14658 seconds
  • Memory Usage 2,914KB
  • Queries Executed 13 (?)
More Information
Template Usage (35):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (3)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (6)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (6)postbit
  • (6)postbit_onlinestatus
  • (6)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (71):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates