Web Analytics Made Easy -
StatCounter Having a problem working with PHP arrays - CodingForum

Announcement

Collapse
No announcement yet.

Having a problem working with PHP arrays

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Having a problem working with PHP arrays

    Hi all! New to the forums and looking for some help with this code I'm writing. I can't tell for the life of me what's going on and would greatly appreciate any constructive input

    I have a form in which someone can add rental items. Initially, the form only has fields for a single rental item, but I have it so they can add fields (using javascript).

    Each new set of fields is incremented by 1. For example--

    Original fields:
    Code:
    <input type="text" name="item0Name" />
    <input type="text" name="item0Qty" />
    <input type="text" name="item0Cost" />
    Newfields:
    Code:
    <input type="text" name="item1Name" />
    <input type="text" name="item1Qty" />
    <input type="text" name="item1Cost" />
    
    <input type="text" name="item2Name" />
    <input type="text" name="item2Qty" />
    <input type="text" name="item2Cost" />
    Each time a new item is added, I also have a javascript counter going that is passed to the php:

    Code:
    $itemCounter = $_GET['itemCounter'];
    For now, I am manually creating the variables in a test file, and trying to work with outputting the array.

    Here is what I have so far:
    Code:
    // Manually set for testing
    $item1Name = "Item 1 Name";
    $item1Qty = "Item 1 Qty";
    $item1Cost = "Item 1 Cost";
    $item1Total = "Item 1 Total";
    
    $item2Name = "Item 2 Name";
    $item2Qty = "Item 2 Qty";
    $item2Cost = "Item 2 Cost";
    $item2Total = "Item 2 Total";
    
    $item3Name = "Item 3 Name";
    $item3Qty = "Item 3 Qty";
    $item3Cost = "Item 3 Cost";
    $item3Total = "Item 3 Total";
    
    // Detect how many different rentals items have been added
    // $itemCounter = $_GET['itemCounter'];
    $itemCounter = 2; // Manually set for testing
    echo $itemCounter."<br /><br />";
    
    $addRentalItems=0;
    
    //For each of those rental items...
    while($addRentalItems <= $itemCounter)
    	{
    		// ... create an array using the unique item number ...
    		${'item'.$itemCounter} = array(
    		
    			// ... insert the assumed variables from the form with the matching item number ...
    			"Name" => "${'item'.$itemCounter.'Name'}",
    			"Quantity" => "${'item'.$itemCounter.'Qty'}",
    			"Cost" => "${'item'.$itemCounter.'Cost'}",
    			"Total" => "${'item'.$itemCounter.'Total'}"
    		);
    		
    		// ... and add the new array to the $rentalItems array
    		$rentalItems[] = ${'item'.$itemCounter};
    		
    		$addRentalItems++;
    		
    	};
    
    // For each array inside of $rentalItems, output the contained values
    foreach($rentalItems as $r){
    	
    	echo "
    		<strong>$r[Name]</strong> <br />$r[Quantity] x $r[Cost] = $r[Total]<br /><br />
    	";
    };
    When I run that script, I get the values for the last item printed 3 times

    I don't know why or what to do.

  • #2
    You've made that way over complicated.

    For your inputs on the html side you can use a dynamic array without numbers:
    Code:
    <input type="text" name="itemName[]" />
    <input type="text" name="itemQty[]" />
    <input type="text" name="itemCost[]" />
    
    <input type="text" name="itemName[]" />
    <input type="text" name="itemQty[]" />
    <input type="text" name="itemCost[]" />
    On the PHP side you can then rest assured that each set of items comes through in the same order so you just do this:

    PHP Code:
    //$Key is the index integer, $Value is the input in the box
    foreach($_POST['itemName'] as $Key => $Value)
       {
       
    //Do something with the 3 fields
       
    print $_POST['itemName'][$Key];
       print 
    $_POST['itemQty'][$Key];
       print 
    $_POST['itemCost'][$Key];
       } 
    "Tango says double quotes with a single ( ' ) quote in the middle"
    '$Name says single quotes with a double ( " ) quote in the middle'
    "Tango says double quotes ( \" ) must escape a double quote"
    '$Name single quotes ( \' ) must escape a single quote'

    Comment


    • #3
      On the PHP side you can then rest assured that each set of items comes through in the same order so you just do this:
      Is this true? This is spectacular. I have done all kinds of things to get around this issue, including putting in a hidden counter field that increments using JS. So this works with the POST method, does it work with the GET method as well? I guess it doesn't matter if it doesn't, but I am just curious.

      Comment


      • #4
        Yes it works with both $_GET and $_POST.

        The idea being this:

        Item[] = 1
        Item[] = 2
        Item[] = 3

        The Array will always start from top => Bottom OR Left => Right. Thats how the browser will send them and thats how PHP will receive and handle them.

        Therefore if you then do this:
        SubItem[] = 1
        SubItem[] = 2
        SubItem[] = 3

        They will be in matching pairs with the other array. That works in $_GET because your arrays will be passed in the same order - left to right in the url.

        I really need to make some more tip topics for my sig lol.
        "Tango says double quotes with a single ( ' ) quote in the middle"
        '$Name says single quotes with a double ( " ) quote in the middle'
        "Tango says double quotes ( \" ) must escape a double quote"
        '$Name single quotes ( \' ) must escape a single quote'

        Comment


        • #5
          This was definitely useful. I knew (correct me if I'm wrong) that if you just assigned a variable to the $_POST array, so
          PHP Code:
          $pArray $_POST
          that you would get an array of all the items posted. I have been finding the array_key_exists() function useful with this. What I didn't know was that it would gather an array of all variables posted with the same name. I thought it would just give you the value of the last item posted, and you would lose any other values.

          You also say;

          The Array will always start from top => Bottom OR Left => Right. Thats how the browser will send them and thats how PHP will receive and handle them.
          Will it just read the code in order...line by line. So if A appears on line 10 and B on line 11, both with the same name it will get A then B. The only reason I ask is because if you applied a right float to A, and a left float to B, with no clear on A, you could have a situation where A was coded before B, but appear to the left of it. So is it really the order in which the fields were coded? or will it always read top to bottom, left to right?
          Last edited by JamesOxford; Aug 18, 2011, 07:50 PM.

          Comment


          • #6
            Originally posted by JamesOxford View Post
            Will it just read the code in order...line by line. So if A appears on line 10 and B on line 11, both with the same name it will get A then B. The only reason I ask is because if you applied a right float to A, and a left float to B, with no clear on A, you could have a situation where A was coded before B, but appear to the left of it. So is it really the order in which the fields were coded? or will it always read top to bottom, left to right?
            Yes. Even though float takes it out of regular flow, the browser will still send the form information in the order it is written.

            ...I think haha you should test that, but I'm sure I'm right.
            Useful function to retrieve difference in times
            The best PHP resource
            A good PHP FAQ
            PLEASE remember to wrap your code in [PHP] tags.
            PHP Code:
            // Replace this
            if(isset($_POST['submitButton']))
            // With this
            if(!empty($_POST))
            // Then check for values/forms. Some IE versions don't send the submit button 
            Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

            Comment


            • #7
              Ignoring the floats thing (not sure if you're referring to css or php), the browser will send the $Array[] fields in the order they are listed in the html source. That is, if you have 6 fields and they're all called $Array[] the browser will send them in the order that the html engine parses them.

              Here is an example:
              Code:
              <html>
                 <head>
                 <title>Example</title>
                 </head>
                 
                 <body>
                    <form>
                       <input type="text" name="name[]">
                       <input type="text" name="name[]">
                       <input type="text" name="name[]">
                       <input type="text" name="name[]">
                       
                       <input type="text" name="address[]">
                       <input type="text" name="address[]">
                       <input type="text" name="address[]">
                       <input type="text" name="address[]">
                    </form>
                 </body>
              </html>
              The arrays above will be transmitted in the same order. The name array will be transmitted top to bottom as will the address array. That means the first item of the name array will match the first item of the address array. The second item in the name array will match the second item in the address array and so on.

              You could also do this:
              Code:
              <html>
                 <head>
                 <title>Example</title>
                 </head>
                 
                 <body>
                    <form>
                       <input type="text" name="name[]"><input type="text" name="name[]"><input type="text" name="name[]"><input type="text" name="name[]">
                       
                       <input type="text" name="address[]"><input type="text" name="address[]"><input type="text" name="address[]"><input type="text" name="address[]">
                    </form>
                 </body>
              </html>
              Again, the browser will read the fields from left to right so they will be transmitted in the same order as they would from top to bottom. The fields will match their partners in the address array.

              You could also do this:
              Code:
              <html>
                 <head>
                 <title>Example</title>
                 </head>
                 
                 <body>
                    <form>
                       <input type="text" name="name[]"><input type="text" name="address[]">
                       <input type="text" name="name[]"><input type="text" name="address[]">
                       <input type="text" name="name[]"><input type="text" name="address[]">
                       <input type="text" name="name[]"><input type="text" name="address[]">
                    </form>
                 </body>
              </html>
              Again, its the order in which the fields are listed that is important. As long as they are placed in the page in the correct order and the user inputs in the matching fields they will be transmitted to PHP in the same order.

              What you then do with those arrays in PHP is entirely down to you but they will match in order when your script receives them.
              "Tango says double quotes with a single ( ' ) quote in the middle"
              '$Name says single quotes with a double ( " ) quote in the middle'
              "Tango says double quotes ( \" ) must escape a double quote"
              '$Name single quotes ( \' ) must escape a single quote'

              Comment


              • #8
                Hi tangoforce,

                Thanks for the input! I tried your solution and it's definitely much more elegant

                The only issue I'm having now is outputting the values in a way that I can send them in an email.

                Instead of having an array for each item detail, I was trying to have each item be an array itself. That way, when I send them via a php mail function, I can easily add then to the $message variable as strings.

                Of course, since there's no way of knowing how many items will be sent, I needed to have php create strings from arrays as needed, and then add them all to the email content.

                Do you have any suggestions for this?

                Comment


                • #9
                  Can you show us in code what you're trying to achieve? - Sorry I'm finding your description a bit hard to follow (that and I've got a ton of other stuff going on so concentration isn't easy at the minute).
                  "Tango says double quotes with a single ( ' ) quote in the middle"
                  '$Name says single quotes with a double ( " ) quote in the middle'
                  "Tango says double quotes ( \" ) must escape a double quote"
                  '$Name single quotes ( \' ) must escape a single quote'

                  Comment


                  • #10
                    Originally posted by tangoforce View Post
                    Can you show us in code what you're trying to achieve? - Sorry I'm finding your description a bit hard to follow (that and I've got a ton of other stuff going on so concentration isn't easy at the minute).
                    Hi tango!

                    Sorry for the confusion-- I didn't think you'd reply so fast, so I just kind of posted and was going to show you all progress in a bit.

                    Here is what I'm currently working with--

                    Form.php:
                    PHP Code:
                    <form method="POST" action="submit.php">


                    <
                    fieldset>
                                <
                    legend>Items Ordered</legend>
                                
                                <
                    label>Item # 1</label>
                                
                    <table>
                                    <
                    tr>
                                        <
                    td class="leftCol">     
                                            <
                    label for="item0">Name Description</label>
                                        </
                    td>
                                        <
                    td class="rightCol">
                                            <
                    input type="text" class="inputText" name="itemName[]" id="item0" />
                                        </
                    td>
                                    </
                    tr>
                                    <
                    tr>
                                        <
                    td class="leftCol">
                                            <
                    label for="item0Qty">Quantity</label>
                                        </
                    td>
                                        <
                    td class="rightCol">
                                            <
                    input type="text" class="inputText" name="itemQty[]" id="item0Qty" />
                                        </
                    td>
                                    </
                    tr>
                                    <
                    tr>
                                        <
                    td class="leftCol">
                                            <
                    label for="item0Cost">Cost Per Unit</label>
                                        </
                    td>
                                        <
                    td class="rightCol">
                                            <
                    input type="text" class="inputText" name="itemCost[]" id="item0Cost" />
                                        </
                    td>
                                    </
                    tr>
                                    <
                    tr>
                                        <
                    td class="leftCol">
                                            <
                    label for="item0Total">Total</label>
                                        </
                    td>
                                        <
                    td class="rightCol">
                                            <
                    input type="text" class="inputText" name="itemTotal[]" id="item0Total" />
                                        </
                    td>
                                    </
                    tr>
                                </
                    table>
                                <
                    hr />
                                <
                    div id="dynamicInput">
                                </
                    div>
                         <
                    input type="button" value="Add another rental item" onClick="addInput('dynamicInput');">


                    </
                    form


                    Submit.php:
                    PHP Code:

                    if(isset($_POST['itemName'])) {

                    $to "MYEMAI[email protected]";
                    $subject "New Rental Order";

                    foreach(
                    $_POST['itemName'] as $Key => $Value)
                                   {
                                       
                    $itemName $_POST['itemName'][$Key];
                                       
                    $itemQty $_POST['itemQty'][$Key];
                                       
                    $itemCost $_POST['itemCost'][$Key];
                                       
                    $itemTotal $_POST['itemTotal'][$Key];
                                       
                                       
                    $rentalItems[] = $itemName;
                                       
                    $rentalItems[] = $itemQty;
                                       
                    $rentalItems[] = $itemCost;
                                       
                    $rentalItems[] = $itemTotal;
                                       
                    $rentalItems[] = " ";
                                   }
                                   
                                
                    $rentalItemsList implode("\n"$rentalItems);

                    $body ="$rentalItemsList";
                                
                    PRINT 
                    "Thanks for submitting your order!<br />";
                    PRINT 
                    "REMEMBER TO COLLECT THE CREDIT CARD INFO!";

                    mail ($to$subject$body);

                                
                            } else {
                            
                                echo 
                    "An Error Occured!";
                            
                            }




                    It seems to work ok, but any suggestions would be much appreciated.

                    Thanks again for all of your help!

                    Comment


                    • #11
                      Hi

                      If it works ok, I'd be tempted to just use it as-is but if you really want to know I'd probably change one thing (and this is purely personal preference and doesn't really affect performance) along with your code formatting:

                      This is untested
                      PHP Code:
                      if(isset($_POST['itemName']))
                         {
                         
                      $to "[email protected]";
                         
                      $subject "New Rental Order";

                         
                      $body '';

                         foreach(
                      $_POST['itemName'] as $Key => $Value)
                            {
                            
                      $itemName $_POST['itemName'][$Key];
                            
                      $itemQty $_POST['itemQty'][$Key];
                            
                      $itemCost $_POST['itemCost'][$Key];
                            
                      $itemTotal $_POST['itemTotal'][$Key];

                            
                      $body .= "
                            
                      $itemName
                            
                      $itemQty
                            
                      $itemCost
                            
                      $itemTotal
                            
                            "
                            
                      }

                         
                      $rentalItemsList implode("\n"$rentalItems);
                         
                      $body $rentalItemsList;

                         PRINT 
                      "Thanks for submitting your order!<br />";
                         PRINT 
                      "REMEMBER TO COLLECT THE CREDIT CARD INFO!";
                         
                      mail ($to$subject$body);
                         }
                      else
                         {
                         echo 
                      "An Error Occured!";
                         } 
                      Also mail() should always be inside an if() conditional in case it doesn't work and you need to know.
                      Last edited by tangoforce; Aug 19, 2011, 03:07 PM.
                      "Tango says double quotes with a single ( ' ) quote in the middle"
                      '$Name says single quotes with a double ( " ) quote in the middle'
                      "Tango says double quotes ( \" ) must escape a double quote"
                      '$Name single quotes ( \' ) must escape a single quote'

                      Comment


                      • #12
                        Thanks for the help, tangoforce!

                        Sorry for the formatting; I was trying to post in a hurry, but I guess that's no excuse when I know someone's going to be reading through it

                        I'll go ahead and try your changes and see if it breaks anything.

                        Comment


                        • #13
                          Code amended with missing $body = '';. This serves as a bump and email if set.
                          "Tango says double quotes with a single ( ' ) quote in the middle"
                          '$Name says single quotes with a double ( " ) quote in the middle'
                          "Tango says double quotes ( \" ) must escape a double quote"
                          '$Name single quotes ( \' ) must escape a single quote'

                          Comment

                          Working...
                          X