PHP / multi-dimensional arrays / mysql

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 1171 Views - Last Post: 27 May 2013 - 07:49 PM Rate Topic: -----

#1 pokey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-May 13

PHP / multi-dimensional arrays / mysql

Posted 23 May 2013 - 07:55 PM

I am attempting to write a php program that gathers the following information from a form which it gathers from the $_POST
If I do a print_r
print_r($_POST);


I get the following results, however all the hidden fields are passed through for items they didn't order. I would like to have a review order screen which eliminates the keys which do not have a qty_# in them. for ex: if
qty_10 = "" then I want to eliminate similar keys like line_10, desc_10 and ps_10 from the array and only display the keys with a number in the qty_# lines.

Array ( [qty_3] => 20 [lin_3] => 3 [desc_3] => CHEESE, PARM RMNO BLND GRTD [ps_3] => 2/5 LB [qty_4] => 30 [lin_4] => 4 [desc_4] => CHEESE, PARM GRTD BAG REF IMP [ps_4] => 1/5 LB [qty_5] => 50 [lin_5] => 5 [desc_5] => CHEESE, RMNO HALF WHEEL WAX [ps_5] => 20# [qty_6] => [lin_6] => 6 [desc_6] => BEEF, GRND 81/19 FINE RAW F2F [ps_6] => 4/10LB [qty_7] => [lin_7] => 7 [desc_7] => BEEF, GRND 81/19 FINE RAW REF [ps_7] => 4/10LB [qty_8] => [lin_8] => 8 [desc_8] => TURKEY, BRST WHL MSCL ALL NTRL [ps_8] => 2/8 LBA [qty_9] => [lin_9] => 9 [desc_9] => CHICKEN, BRST STRIP BNLS RSTD [ps_9] => 4/2.5 LB [qty_10] => [lin_10] => 10 [desc_10] => OLIVE, RIPE STD SLCD IMP SPAIN [ps_10] => 6/#10 CN [qty_11] => [lin_11] => 11 [desc_11] => CLAM, OCEAN CKD CHPD CAN SHLF [ps_11] => 12/51 OZ [qty_12] => [lin_12] => 12 [desc_12] => ONION RING BATRD BEER 3/16 [ps_12] => 4/2.5LB [qty_13] => [lin_13] => 13 [desc_13] => TIRAMISU CUP [ps_13] => 12/3.5 OZ [qty_14] => [lin_14] => 14 [desc_14] => GELATO PISTACCHIO [ps_14] => 2/190 OZ [qty_15] => [lin_15] => 15 [desc_15] => GELATO CARMEL SEA SALT [ps_15] => 2/190 OZ




I am just learning php and I can not seem to figure out how to tear apart this array. I do not even know where to begin since the array is not an array with the same keys or I think I would do something like the following:
foreach($array as key => value){
not sure of code here
}


I want to accomplish two things.

1. order review and remove the lines not ordered.

2. write the final order to a mysql database.(php 5.3/mysql 5.0 - a godaddy server).
connect to database
foreach ($array as $key => $value)             {
 if ($value2 != ""){
 mysql_query("INSERT INTO orders (user_name, line_num, qty_ordered, description, pack_size) VALUES ('$_SESSION['user_name']', '$value['line_num'], '$value['qty_ordered']', '$value['description']', '$value['pack_size']')") or die(mysql_error());  
 }
 }



My first question is how do I address such a complex array? My second question is how do I insert it into the mysql database if all the keys are different?

Thank you in advance for any assistance that may be provided..

Is This A Good Question/Topic? 0
  • +

Replies To: PHP / multi-dimensional arrays / mysql

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10446
  • View blog
  • Posts: 38,684
  • Joined: 27-December 08

Re: PHP / multi-dimensional arrays / mysql

Posted 23 May 2013 - 09:12 PM

There are three elements here:
-qty_i
-desc_i
-lin_i

Where i is a numeric index variable. Try using a standard for loop here, rather than the foreach loop. That will help you access the appropriate $_POST elements.

Also, the mysql_*() family of functions is deprecated. You should avoid it. PDO and MySQLi are standards adherent, and you need to make sure to use their support of Prepared Statements.

In fact, a Prepared Statement is really what you need. Then you can just bind the new values on the insert. You won't have to keep creating a new query.

More on PDO.
Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3515
  • View blog
  • Posts: 10,142
  • Joined: 08-June 10

Re: PHP / multi-dimensional arrays / mysql

Posted 23 May 2013 - 09:51 PM

another option is to restructure the form field names into groups. given that 3 elements belong together, you could name them ingredient[][qty], ingredient[][lin] and ingredient[][desc], which would result in a nested array (whith these 3 elements nicely grouped) where you can easily apply array_filter() to it.
Was This Post Helpful? 1
  • +
  • -

#4 pokey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-May 13

Re: PHP / multi-dimensional arrays / mysql

Posted 23 May 2013 - 10:03 PM

There are 4 elements you forgot ps_i

I am still not really sure of what to do here. There are still items in the array I would like removed that do not belong. They are passed through to $_POST through with the hidden type of the form but if qty is 0 i need to remove the corresponding other 3 from the array (desc_,lin_,ps_)
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3515
  • View blog
  • Posts: 10,142
  • Joined: 08-June 10

Re: PHP / multi-dimensional arrays / mysql

Posted 23 May 2013 - 10:14 PM

Quote

I am still not really sure of what to do here.

you could choose either option and proceed from there.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10446
  • View blog
  • Posts: 38,684
  • Joined: 27-December 08

Re: PHP / multi-dimensional arrays / mysql

Posted 23 May 2013 - 10:18 PM

A simple if statement in the loop to check to make sure the quantity is greater than 0 could work.
Was This Post Helpful? 0
  • +
  • -

#7 pokey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-May 13

Re: PHP / multi-dimensional arrays / mysql

Posted 24 May 2013 - 02:54 AM

View Postmacosxnerd101, on 23 May 2013 - 10:18 PM, said:

A simple if statement in the loop to check to make sure the quantity is greater than 0 could work.


however, some of the array i need to remove is not 0, there is only 1 field that is 0, the other three have values. now what. :)
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10446
  • View blog
  • Posts: 38,684
  • Joined: 27-December 08

Re: PHP / multi-dimensional arrays / mysql

Posted 24 May 2013 - 08:22 AM

You're missing the point. You don't remove values from $_POST. Just ignore the ones you don't want. If the quantity is 0, go to the next record. If you use a standard for loop as I suggested, you can look at all four pieces of information associated with the record in one iteration of the loop.
Was This Post Helpful? 0
  • +
  • -

#9 Particule42  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 13
  • Joined: 24-May 13

Re: PHP / multi-dimensional arrays / mysql

Posted 24 May 2013 - 08:30 AM

Hi,
I don't know if you already found a solution, but why are you posting fields you don't need.
If you can change your form, you can post only fields you need, fields that have qty.

And if you need a PHP solution, you can use a while loop that start with 1 and end when you don't find a value. Below a fast example:

$i = 1;
while (isset($_POST['qty_' . $i])) {
  if (empty($_POST['qty_' . $i])) {
     continue;
  }
  $qty = $_POST['qty_' . $i];
  $lin = $_POST['lin_' . $i];
  $desc = $_POST['desc_' . $i];
  $ps = $_POST['ps_' . $i];

  // do something with these values here

  $i++;
}



But i think you need to post only what you need. Hope it help.

ps: sorry the bad english ....
Was This Post Helpful? 2
  • +
  • -

#10 Particule42  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 13
  • Joined: 24-May 13

Re: PHP / multi-dimensional arrays / mysql

Posted 24 May 2013 - 09:12 AM

Use array_key_exists instead of isset function, to be sure the key exists and is not equals to null. See PHP doc (example 1 with array) for more information link
Was This Post Helpful? 0
  • +
  • -

#11 pokey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-May 13

Re: PHP / multi-dimensional arrays / mysql

Posted 24 May 2013 - 01:36 PM

That is a good point, why am i posting values i do not need.

I guess it should start with the code from the form that doesnt seem to be working so I do not have to pass fields that I do not need, then i will not have to remove the fields when i loop through the array.

   // Connect to database
   // Fetching from your database table.
            $query = "SELECT * FROM $usertable";
            $result = mysql_query($query);
?>
<table border=1>
<tr><td align="center" bgcolor="LightGrey">Line#</td><td align="center"  bgcolor="LightGrey">Description</td><td align="center"  bgcolor="LightGrey">Pack Size</td><td align="center"  bgcolor="LightGrey">Order Qty</td></tr>

           <? 
           
           if ($result) {
?>

<form name="Form2" method="POST" action="./review_order.php" enctype="multipart/form-data" id="Form2">
                
                <?
                while($row = mysql_fetch_assoc($result)) {
                    $name = $row["$line_num"];
                    $name2 = $row["$description"];
                    $name3 = $row["$SPEC_SIZE"];
                    ?>
                    <tr><td>
                    
                   <? echo $name;?> &nbsp;&nbsp;&nbsp;&nbsp; </td> 
                    <td><?echo $name2; ?> &nbsp;&nbsp;&nbsp;&nbsp;</td>
                    <td><? echo $name3; ?> &nbsp;&nbsp;&nbsp;&nbsp;</td>
                    <td><input type="text"size="5" id="order1" name="qty_<?echo $name; ?>" value=""> </td>
                    <input type="hidden"size="15" id="order2" name="lin_<?echo $name; ?>" value="<? echo $name;?>">
                    <input type="hidden"size="255" id="order3" name="desc_<?echo $name;?>" value="<? echo $name2;?>">
                    <input type="hidden"size="25" id="order4" name="ps_<?echo $name;?>" value="<?echo $name3;?>">
               
                    </tr>
                    <?
                }
              ?>
 <input type="submit" id="Button1" name="" value="Submit Order"></form>
 <?
            }
?>
 



I suppose I need to eliminate the hidden fields that are not associated with the qty_i if it is "" with an if statement, right?
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3331
  • View blog
  • Posts: 11,263
  • Joined: 12-December 12

Re: PHP / multi-dimensional arrays / mysql

Posted 24 May 2013 - 02:52 PM

As has already been mentioned, you do not need to eliminate hidden fields; just ignore any values in the POST data that you do not need.

It is possible using Javascript to remove the hidden elements from the page before the form is submitted, or it is possible to unset some of the posted-variables, but this is generally a pointless exercise when you can just ignore them.
Was This Post Helpful? 0
  • +
  • -

#13 pokey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-May 13

Re: PHP / multi-dimensional arrays / mysql

Posted 24 May 2013 - 03:23 PM

View Postandrewsw, on 24 May 2013 - 02:52 PM, said:

As has already been mentioned, you do not need to eliminate hidden fields; just ignore any values in the POST data that you do not need.

It is possible using Javascript to remove the hidden elements from the page before the form is submitted, or it is possible to unset some of the posted-variables, but this is generally a pointless exercise when you can just ignore them.


I do not think ignoring the values is the way to go since they have values, i would not even know how to ignore them since they do have values. I do not know much about php, and I know nothing about javascript, but i think I should fix the form and make the values not appear, since they have values, but really should not be there (see form above).

I think i need to fix the form first.
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10446
  • View blog
  • Posts: 38,684
  • Joined: 27-December 08

Re: PHP / multi-dimensional arrays / mysql

Posted 24 May 2013 - 03:52 PM

Quote

I do not think ignoring the values is the way to go since they have values

So what if they have values. You clearly don't want to do anything with them. This is part of validating your data. You can either ignore it, or tell the user it is invalid and force them to enter valid data. Think if this is a shopping cart. If the quantity is 0, that means the user didn't select any of the item. So that is a value that shouldn't be added to the shopping cart. Does it really make sense to pester the user about it if they don't want the item?

The other thing is that you really can't control what the user does. You have to write code that will handle even the most troublesome users. So just ignoring the unwanted values is less intensive than removing them. It's not like you need the current values in the $_POST superglobal to be persistent. You insert the ones you want into the database and discard the rest.
Was This Post Helpful? 0
  • +
  • -

#15 pokey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-May 13

Re: PHP / multi-dimensional arrays / mysql

Posted 24 May 2013 - 04:16 PM

View Postmacosxnerd101, on 24 May 2013 - 03:52 PM, said:

Quote

I do not think ignoring the values is the way to go since they have values

So what if they have values. You clearly don't want to do anything with them. This is part of validating your data. You can either ignore it, or tell the user it is invalid and force them to enter valid data. Think if this is a shopping cart. If the quantity is 0, that means the user didn't select any of the item. So that is a value that shouldn't be added to the shopping cart. Does it really make sense to pester the user about it if they don't want the item?

The other thing is that you really can't control what the user does. You have to write code that will handle even the most troublesome users. So just ignoring the unwanted values is less intensive than removing them. It's not like you need the current values in the $_POST superglobal to be persistent. You insert the ones you want into the database and discard the rest.


how do you ignore the value if it has a valid value like the desc_# fields. the key is desc_# and the value is like "hotdog buns".
there are none ordered in the qty_# field but the hidden value was still passed through the $_POST for desc_#, pac_# and line_#.

I do not need the those three fields if the qty_# field is 0.

If I am to review the cart and i do a for loop it will display all the fields. If all the fields = "" i would know how to filter it but they have values which is confusing me.

sorry i am new to php and am stuggling through this.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2