Thread: POST arrays

    #1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    3

    POST arrays


    It's been awhile, so I need to know;

    How do I define and use $_POST arrays. For example. I have a button that increment's a variable every time it is pressed. When the user selects the finish button, it passes each count of that original button press via a array element related to that count, through POST. How do I define the post arrays and use them?

    I.E.
    PHP Code:
    $cls_key $_POST['class']; 
    but I want it to make a seperate element for each count:
    PHP Code:
    $cls_key1 $_POST['class']1;
    $cls_key2 $_POST['class']2;
    $cls_key3 $_POST['class']3
    Last edited by BitZoid; March 18th, 2013 at 10:50 AM.
    -- Success achieved from tribulation --
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    3
    I've been doing alot of C++, in C++ I could use something similar to this:
    PHP Code:
        $cls_key[$inc_cat];
        
    $sub_cls_key[$inc_cat];
        for (
    $i=0$i $inc_cat$i++) {
            
    $cls_key[$i] = $_POST['class'];
            
    $sub_cls_key[$i] = $_POST['subclass'];
        } 
    where inc_cat is the number of elements I need in the array. However, I don't know if this is valid in PHP and I also don't know how to get $_POST['class']1 and $_POST['class']2
    -- Success achieved from tribulation --
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,617
    Rep Power
    595
    What you are trying to do doesn't make sense to me but to get elements of an array passed via $_POST:
    PHP Code:
    $classArray=$_POST['class'];
    for (
    $i=0$i<count($classArray); $i++) {
       print(
    "$classArray[$i]<br />");

    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    3
    Would this create a new array element for every value in inc_cat?

    PHP Code:
        for ($i=0$i $inc_cat$i++) {
            
    $cls_key=array($i => $_POST['class']);
            
    $sub_cls_key=array($i =>$_POST['subclass']);
        } 
    I.E.
    $cls_key[1] = $_POST['class']1
    $sub_cls_key[1] = $_POST['subclass']1
    $cls_key[2] = $_POST['class']2
    $sub_cls_key[2] = $_POST['subclass']2
    $cls_key[3] = $_POST['class']3
    $sub_cls_key[3] = $_POST['subclass']3

    gw. I'm trying to have a "Add category and subcategory" button that every time clicked, adds a new cat and subcat drop down to the site and then when it's finished and done is clicked, I can make the changes in my database. The part I need help with is the code that stores the data after finished is clicked, so It can be put into the database query
    Last edited by BitZoid; March 18th, 2013 at 11:18 AM.
    -- Success achieved from tribulation --
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Arrays in PHP can be chained together.

    If you have:

    PHP Code:
    $array = array(
      
    'a' => array(
        
    => 'apple',
        
    => 'banana',
        
    => 'coconut'
      
    ),
      
    'b' => array(
        
    => 'adam',
        
    => 'betty',
        
    => 'cassie'
      
    )
    ); 
    Then to print 'betty' you want to print $array['b'][1];
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    3
    So in essence if I have a form that now has 3 sets of category selection drop down boxes, because "add new" was clicked 3 times, I just make the name property of each drop box (html) value different, starting at 1, 2, 3, etc..

    Code:
    <select name=class1>
    <select name=class2>
    etc...
    PHP Code:
    $_POST['class1'
    - How would I have the 1 value in this statement be a variable?

    Would this work?:
    PHP Code:
    $_POST['class'$i
    Last edited by BitZoid; March 18th, 2013 at 11:38 AM.
    -- Success achieved from tribulation --
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    3
    This only outputs the first set of class/subclass. It's blank for array element's 1. Why?

    PHP Code:
        // Handle MULTIPLE category enevlopes //
        
    for ($i=0$i $inc_cat$i++) {
            
    $cls_key=array($i => $_POST['class'.$i]);
            
    $sub_cls_key=array($i =>$_POST['subclass'.$i]);
        }
            echo 
    "cls_key0 = ".$cls_key[0].".";
            echo 
    "cls_key1 = ".$cls_key[1].".";
            echo 
    "sub_cls_key0 = ".$sub_cls_key[0].".";
            echo 
    "sub_cls_key1 = ".$sub_cls_key[1]."."
    Outputs:
    Code:
    cls_key0 = 24.cls_key1 = .sub_cls_key0 = 89.sub_cls_key1 = .
    My Form submissions look like this:
    PHP Code:
    if (isset($_GET['inc_cat'])) {
    for ($i=0; $i<$inc_cat; $i++) {
    ?>
    <!-- Handle Class Dropbox -->
    <p>
    <?php
    require_once (...); // Connect to the DB.
        
    $queryClass="SELECT DISTINCT(class), cls_key FROM class ORDER BY class ASC";
        
    $resultClass=mysql_query($queryClass);

        echo 
    "<p>Class: <select name=class".$i.">";

        while (
    $rowDropClass mysql_fetch_array($resultClass)) 
        {
            echo 
    "<option value='".$rowDropClass['cls_key']."'>".$rowDropClass['class']."</option>";
        }
        echo 
        
    "</select></p>";
    ?>
    </p>

    <!-- Handle SubClass Dropbox -->
    <p>
    <?php
    require_once (...); // Connect to the DB.
        
    $querySubClass="SELECT DISTINCT(subclass), sub_cls_key FROM subclass ORDER BY subclass ASC";
        
    $resultSubClass=mysql_query($querySubClass);

        echo 
    "<p>Subclass: <select name=subclass".$i.">";

        while (
    $rowDropSubClass mysql_fetch_array($resultSubClass)) 
        {
            echo 
    "<option value='".$rowDropSubClass['sub_cls_key']."'>".$rowDropSubClass['subclass']."</option>";
        }
        echo 
        
    "</select></p>";
    ?>
    </p>
    Last edited by BitZoid; March 18th, 2013 at 11:59 AM.
    -- Success achieved from tribulation --
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,617
    Rep Power
    595
    Because you did not pay attention to the correct syntax ManiacDan gave you.

    P.S. I also see you are using the deprecated MySQL extensions. You need to switch to PDO and prepared statements before going much further.

    Comments on this post

    • ManiacDan agrees
    • BitZoid disagrees
    Last edited by gw1500se; March 18th, 2013 at 12:02 PM.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    3
    Originally Posted by gw1500se
    Because you did not pay attention to the correct syntax ManiacDan gave you.

    P.S. I also see you are using the deprecated MySQL extensions. You need to switch to PDO and prepared statements before going much further.
    I know it's deprecated. It's an old system, I'm not converting it to PDO at this time.

    As for your suggestion gw, I used mine instead.
    PHP Code:
    $cls_key = array();
        
    $sub_cls_key = array();

        for (
    $i=0$i $_POST['inc_cat']; $i++) {
            
    $cls_key[]=$_POST['class'.$i];
            
    $sub_cls_key[]=$_POST['subclass'.$i];
        }
            echo 
    "cls_key0 = ".$cls_key[0].".";
            echo 
    "cls_key1 = ".$cls_key[1].".";
            echo 
    "sub_cls_key0 = ".$sub_cls_key[0].".";
            echo 
    "sub_cls_key1 = ".$sub_cls_key[1]."."
    Next time I'll try to pay closer attention to what ManiacDan says though. I like your signature, haha 2 people.
    Last edited by BitZoid; March 18th, 2013 at 12:20 PM.
    -- Success achieved from tribulation --
  18. #10
  19. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    You're naming your post fields incorrectly.

    name="something[1]"

    This produces $_POST['something'][1]
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  20. #11
  21. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    3
    IT works! Thanks Maniac!
    -- Success achieved from tribulation --
  22. #12
  23. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    3
    For search engine lookers, this was my solution:

    Relevant code segments:
    PHP Code:
    // Handle adding another category enevlope drop down set
    if (isset($_GET['inc_cat'])){

    }else{
    $inc_cat 1;
    }

    if (isset(
    $_GET['inc_cat'])) {
        
    $inc_cat $_GET['inc_cat'];
    }

    // Check if form has been submitted.
    if (isset($_POST['submitted'])) {

        
    $errors = array(); // initilize error array.

        // handle drop boxes
        
    $man_key $_POST['manufacturer'];

        
    // Handle MULTIPLE category enevlopes //
        
    $cls_key = array();
        
    $sub_cls_key = array();

        for (
    $i=0$i $_POST['inc_cat']; $i++) {
            
    $cls_key[]=$_POST['class'.$i];
            
    $sub_cls_key[]=$_POST['subclass'.$i];
        } 
    PHP Code:
            // Query to insert the cls and subcls into categories table //
            
    for ($i=0$i $_POST['inc_cat']; $i++) {
                
    $queryTest "INSERT INTO categories (mdl_key, cls_key, sub_cls_key) VALUES ('$recordIDpdf', '$cls_key[$i]', '$sub_cls_key[$i]')";
                
    $resultTest = @mysql_query ($queryTest); // Run Query 
    PHP Code:
    <?php
    // HANDLE MULTIPLE EVNELOPES OF CATEGORY //
    if (isset($_GET['inc_cat'])) {
    for (
    $i=0$i<$inc_cat$i++) {
    ?>
    <!-- Handle Class Dropbox -->
    <p>
    <?php
    require_once ('database stuff here'); // Connect to the DB.
        
    $queryClass="SELECT DISTINCT(class), cls_key FROM class ORDER BY class ASC";
        
    $resultClass=mysql_query($queryClass);

        echo 
    "<p>Class: <select name=class".$i.">";

        while (
    $rowDropClass mysql_fetch_array($resultClass)) 
        {
            echo 
    "<option value='".$rowDropClass['cls_key']."'>".$rowDropClass['class']."</option>";
        }
        echo 
        
    "</select></p>";
    ?>
    </p>

    <!-- Handle SubClass Dropbox -->
    <p>
    <?php
    require_once ('database stuff here'); // Connect to the DB.
        
    $querySubClass="SELECT DISTINCT(subclass), sub_cls_key FROM subclass ORDER BY subclass ASC";
        
    $resultSubClass=mysql_query($querySubClass);

        echo 
    "<p>Subclass: <select name=subclass".$i.">";

        while (
    $rowDropSubClass mysql_fetch_array($resultSubClass)) 
        {
            echo 
    "<option value='".$rowDropSubClass['sub_cls_key']."'>".$rowDropSubClass['subclass']."</option>";
        }
        echo 
        
    "</select></p>";
    ?>
    </p>
    <?php
    }
    }

    Comments on this post

    • gw1500se disagrees : This is not a solution to be copied by others. You are still not using the correct syntax provided by ManiacDan earlier.
    -- Success achieved from tribulation --
  24. #13
  25. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    While I'm glad you got it working, this is still wrong:
    $cls_key[]=$_POST['class'.$i];


    That should be:
    $cls_key[]=$_POST['class'][$i];

    As long as your "class" inputs are named:
    name="class[1]"
    name="class[2]"
    etc.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  26. #14
  27. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    3
    Thanks for the pointer Maniac! I was confused about how to append a counter at the end of my $_POST variables. Even though my code works, I don't want to use wrong syntax so I will fix that right away.
    -- Success achieved from tribulation --
  28. #15
  29. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    3
    Thanks, your way just feels better ManiacDan

    PHP Code:
    // Handle adding another category enevlope drop down set
    if (isset($_GET['inc_cat'])){

    }else{
    $inc_cat 1;
    }

    if (isset(
    $_GET['inc_cat'])) {
        
    $inc_cat $_GET['inc_cat'];
    }

    // Check if form has been submitted.
    if (isset($_POST['submitted'])) {

        
    $errors = array(); // initilize error array.

        // handle drop boxes
        
    $man_key $_POST['manufacturer'];

        
    // Handle MULTIPLE category enevlopes //
        
    $cls_key = array();
        
    $sub_cls_key = array();

        for (
    $i=0$i $_POST['inc_cat']; $i++) {
            
    $cls_key[]=$_POST['class'][$i];
            
    $sub_cls_key[]=$_POST['subclass'][$i];
        }
    ......
    echo 
    "<p>Subclass: <select name=subclass[".$i."]>"
    -- Success achieved from tribulation --

IMN logo majestic logo threadwatch logo seochat tools logo