SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2005
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can I solve this form foreach problem?

    Hello,
    I have a survey, which is consisted of many pages.
    In one page, there are many questions consisted of radio buttons, text fields and check boxes.

    And I'm thinking to implement this way..

    When a user click next button in this page, I will put those ($key:$value) sets in $_SESSION,
    and when he presses finish button in the last page, I need to email the results to the admin.

    For instance, in page 1, there are 9 questions.

    Q_1 , Q_2, Q_3 -> radio buttons
    Q_3a, Q_4, Q_5, Q_5a -> text field
    Q_6 -> check box
    Q_6a -> text field

    And I need to email to admin $key : $value in this order

    Q_1 , Q_2, Q_3, Q_3a, Q_4, Q_5, Q_5a, Q_6, Q_6a


    First thing what I did,

    PHP Code:
       foreach($_POST as $key => $val){
             if (
    is_array($val)){
                
    $msg.="Item: $key\n";
                foreach(
    $val as $v){
                   
    $v stripslashes($v);
                   
    $msg.="   $v\n";
                }
             } else {
                
    $val stripslashes($val);
                
    $msg.="$key$val\n";
             }
          } 
    But I found, if a user doesn't select any value of radio button or check box (yes, it's possible),
    then there is no $key for this radio button (check box too)

    So I found a quick-fix in this forum, so I fixed my codes

    PHP Code:
    $radio_names = array('Q_1''Q_2''Q_3''Q_6'); 
    foreach(
    $radio_names as $radio){   
        
    $_POST[$radio] = isset($_POST[$radio]) ? $_POST[$radio] : ''



       foreach(
    $_POST as $key => $val){
             if (
    is_array($val)){
                
    $msg.="Item: $key\n";
                foreach(
    $val as $v){
                   
    $v stripslashes($v);
                   
    $msg.="   $v\n";
                }
             } else {
                
    $val stripslashes($val);
                
    $msg.="$key$val\n";
             }
          } 
    But I found if I selected a value for Q_3 and don't select a value for Q_1 and Q_2,

    the $key : $value order is like


    Q_3, Q_3a, Q_4, Q_5, Q_5a, Q_6a, Q_1 , Q_2, Q_6

    Why is that?

    How can I fix this order to Q_1 , Q_2, Q_3, Q_3a, Q_4, Q_5, Q_5a, Q_6, Q_6a ?

    Thanks

  2. #2
    SitePoint Addict telos's Avatar
    Join Date
    Mar 2005
    Location
    192.168.2.34
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would store them this way:
    PHP Code:
    $_SESSION['questions'][1] = array('q' => 'What is your favorite color?''a' => 'red'); 
    The questions array would be in the order they asked it. I would also create the session array before hand and add the 'a' value after the form is processed.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another option is to sort the array based on the keys. see ksort() and natsort()

  4. #4
    SitePoint Zealot
    Join Date
    Sep 2005
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,
    I posted this thread while back ago, and everything works fine using ksort to sort the $_POST array,
    but I found there was a problem.

    One survey page has questions

    PHP Code:
    $radio_names = array('Q_7_a''Q_7_b''Q_8_a''Q_8_b''Q_8_c''Q_8_d''Q_8_e''Q_8_f''Q_8_g''Q_9_a''Q_9_b''Q_9_c''Q_9_d''Q_9_e''Q_9_f''Q_9_g''Q_9_h''Q_9_i','Q_10_a''Q_10_b''Q_10_c''Q_10_d'); 
    foreach(
    $radio_names as $radio){   
    $_POST[$radio] = isset($_POST[$radio]) ? $_POST[$radio] : '';         

        
    ksort($_POST); 
    Then the results after sorting is,

    PHP Code:
    'Q_10_a''Q_10_b''Q_10_c''Q_10_d''Q_7_a''Q_7_b''Q_8_a''Q_8_b''Q_8_c''Q_8_d''Q_8_e''Q_8_f''Q_8_g''Q_9_a''Q_9_b''Q_9_c''Q_9_d''Q_9_e''Q_9_f''Q_9_g''Q_9_h''Q_9_i' 
    I think it's because ksort thinks Q_10_a is ahead of Q_7_a because 1 is ahead of 7, right?

    I tried natsort, but it's not working either.

    Is there any way I can sort the right way without moving 'Q_10_a', 'Q_10_b', 'Q_10_c', 'Q_10_d' to the next page?

    Thanks,

    except

  5. #5
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Instead of the ksort there, you can use

    uksort($_POST, 'strnatcasecmp');

    which will do a case insensative comparison on the keys.
    - Robert

  6. #6
    SitePoint Zealot
    Join Date
    Sep 2005
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks it works!!


Bookmarks

Posting Permissions

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