bubbasheeko Posted February 7, 2009 Share Posted February 7, 2009 I seldom use arrays, but I have been left with alternative on this one. So I have an array: foreach ($data as $key => $value) { Doing whatever here. } Inside the array are about 20 different fields sent from a previous page. I only want like 5 of them. So say...there is first_name, last_name, address, hphone, email as the 'field' names. I want to put the five of those fields into my database and ignore the rest. How would I go about doing that? Oh...and I should add that the need to use "INSERT ... ON DUPLICATE KEY UPDATE" is important as there may be updates passed to this app that would need the original item updated and not a new record created. Quote Link to comment https://forums.phpfreaks.com/topic/144177-array-how-to-grab-certain-items-from-it/ Share on other sites More sharing options...
landavia Posted February 7, 2009 Share Posted February 7, 2009 >>So I have an array: where? i don't see it.. i see how to explode/parse the array.. u not give the array >>So say...there is first_name, last_name, address, hphone, email as the 'field' names. I want to put the five of those fields into my database and ignore the rest. How would I go about doing that? sry.. you don't clear about this.. so none can help you.. how about take one step back 1. what page before this? 2. what procedure u do in this? 3. why u said about key? where key u put? are in name, handphone, addr etc? 4. what kind key? Primary, unique, index?? thx Quote Link to comment https://forums.phpfreaks.com/topic/144177-array-how-to-grab-certain-items-from-it/#findComment-756590 Share on other sites More sharing options...
bubbasheeko Posted February 7, 2009 Author Share Posted February 7, 2009 Sorry, I provided as much information I felt comfortable providing seeing it is in regards to a payment processor. 1. PayPal IPN. $data. It's an array, it has what ever PayPal sends via $_POST to the ipn processing app that this is part of. I do not need all of the information, only about 10 of the 25-30 items it sends back. 2. The array being created is an associative array: The $_POST data is what comes from the PayPal IPN. foreach ($_POST as $field=>$value) { $data["$field"] = $value; $post_string .= $field.'='.urlencode(stripslashes($value)).'&'; } 3. "INSERT ... ON DUPLICATE KEY UPDATE" is the title of the mysql function I will need to use because there will be duplicate key records and I want those to be updated rather than a new record created. 4. Primary key would be the transaction number from the PayPal IPN. Basically I know how I want it and how to do it, except for grabbing the records out of the array that I need. I would assume the following: foreach ($p->ipn_data as $key => $value) { if($key == 'fieldname' || $key == 'fieldname2' || $key == 'fieldname3' || $key == 'fieldname4') { NOW SHOULD I BUILD A NEW ARRAY WITH THE STRIPPED INFORMATION? OR IS THERE A WAY THAT I COULD BUILD THE QUERY HERE TO BE USED AFTER THE FOREACH? } } Quote Link to comment https://forums.phpfreaks.com/topic/144177-array-how-to-grab-certain-items-from-it/#findComment-756792 Share on other sites More sharing options...
.josh Posted February 7, 2009 Share Posted February 7, 2009 The point of a foreach loop is to loop through an entire array, where you don't necessarily know how many elements there are. If you already know what array elements and their keys you want to use, then you don't need a foreach loop. You can use an individual element as a regular variable by its key. Example: $array = array('a'=>1,'b'=>2,'c'=>3); echo $array['a']; // output: 1 Quote Link to comment https://forums.phpfreaks.com/topic/144177-array-how-to-grab-certain-items-from-it/#findComment-756803 Share on other sites More sharing options...
bubbasheeko Posted February 7, 2009 Author Share Posted February 7, 2009 Hi Crayon, Well damn! Why didn't I think of that...too many projects on the go - causes confusion :S I know what the element keys are. I will take your suggestion here, seeing it will most definitely work. Thanks for your helpful response on this! Quote Link to comment https://forums.phpfreaks.com/topic/144177-array-how-to-grab-certain-items-from-it/#findComment-756811 Share on other sites More sharing options...
bubbasheeko Posted February 7, 2009 Author Share Posted February 7, 2009 hmm...okay I didn't note the variable correctly. In the foreach loop that built the array it was being stored in $p->data. Now...if I try you suggestion $p->data['element']; I receive the following output: Array['element'] I can not wait to figure this out so I don't have to ask for help again...I knew when I was in college I should not have missed the day we discussed arrays....lol Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/144177-array-how-to-grab-certain-items-from-it/#findComment-756819 Share on other sites More sharing options...
.josh Posted February 7, 2009 Share Posted February 7, 2009 Are you using $p->ipn_data or $p->data or $_POST or what? I see all kinds of variables being used in your foreach loops your posting. Whatever you are using, I don't know how deep the arrays go before you get to your actual data. do echo "<pre>"; print_r($p->data); echo "</pre>"; and post what it echoes. Quote Link to comment https://forums.phpfreaks.com/topic/144177-array-how-to-grab-certain-items-from-it/#findComment-756823 Share on other sites More sharing options...
bubbasheeko Posted February 7, 2009 Author Share Posted February 7, 2009 $p->ipn_data is the variable. Sorry the first one was by memory. The second one was a typo (my bad). I have it open and working on it now so I just copied and pasted. Sorry for the confusion there. There is no output screen to see if print_r($p->ipn_data) works. This page is called behind the scenes so I wouldn't see the output. PayPal called for purchase -> PayPal processes the payment -> PayPal calls the IPN file at the time of the payment process(to update database or shoot off an email to the 'seller'.) Right now I am testing the output by using email. body .= "$p->ipn_data['element']"; The output is: Array['element']. It is an associative array ($key -> $value). There are no other arrays within this one - just one record at a time. Update...I did incorporate the print_r within the body of the email - result = '1' Quote Link to comment https://forums.phpfreaks.com/topic/144177-array-how-to-grab-certain-items-from-it/#findComment-756833 Share on other sites More sharing options...
bubbasheeko Posted February 7, 2009 Author Share Posted February 7, 2009 Figured it out.... Simply using $p->ipn_data['element'] produces the correct data. I think it didn't like the fact that I had it this way before: body .= "$p->ipn_data['element']"; Changing it to: body .= $p->ipn_data['element']; Worked. So that makes it a bit easier. Thank you for your help! Quote Link to comment https://forums.phpfreaks.com/topic/144177-array-how-to-grab-certain-items-from-it/#findComment-756844 Share on other sites More sharing options...
.josh Posted February 7, 2009 Share Posted February 7, 2009 Ah, gotcha. Problem was with your quotes. It wasn't parsing variable because the quotes was confusing php. Just so you know in the future, php will parse variables inside double quotes, but it sometimes gets confused when handling arrays and properties and arrays within properties, etc... you could have also done this: body .= "{$p->ipn_data['element']}"; The brackets tells php where the start and end of the variable is, so it knows what to parse as a variable. Quote Link to comment https://forums.phpfreaks.com/topic/144177-array-how-to-grab-certain-items-from-it/#findComment-756851 Share on other sites More sharing options...
bubbasheeko Posted February 8, 2009 Author Share Posted February 8, 2009 That's funny. Thanks for the preemptive response. I came back because I couldn't get my array variables to be read into the database. Turns out it was because of the quote issue. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/144177-array-how-to-grab-certain-items-from-it/#findComment-757211 Share on other sites More sharing options...
bubbasheeko Posted February 8, 2009 Author Share Posted February 8, 2009 Okay...it worked once...but hasn't since. Here is the code that I have....please keep in mind for testing purposes I have matched the array keys to the field/column names in the database. $body .= "Transaction ID:" . $p->ipn_data['txn_id'] . "\n"; $body .= "Gross: " . $p->ipn_data['mc_gross'] . "\n"; $body .= "Tax: " . $p->ipn_data['tax'] . "\n"; $body .= "First Name: " . $p->ipn_data['first_name'] . "\n"; $body .= "Last Name: " . $p->ipn_data['last_name'] . "\n"; $body .= "Email Address: " . $p->ipn_data['payer_email'] . "\n"; $body .= "Street Address: " . $p->ipn_data['address_street'] . "\n"; $body .= "City: " . $p->ipn_data['address_city'] . "\n"; $body .= "State/Province: " . $p->ipn_data['address_state'] . "\n"; $body .= "Postal Code: " . $p->ipn_data['address_zip'] . "\n"; $body .= "Country: " . $p->ipn_data['address_country_code'] . "\n"; $body .= "PayPal Address Status: " . $p->ipn_data['address_status'] . "\n"; $body .= "Item Number: " . $p->ipn_data['item_number'] . "\n"; $body .= "Quantity: " . $p->ipn_data['quantity'] . "\n"; $body .= "Currency: " . $p->ipn_data['mc_currency'] . "\n"; $body .= "Payment Status: " . $p->ipn_data['payment_status'] . "\n"; $body .= "Payment Type: " . $p->ipn_data['payment_type'] . "\n"; This is the code that builds the body of a test email so i can see that there is content there. Which works great, I see everything I am supposed to. This code that takes certain items from the array so I can enter it into a database is what is causing my headaches. // DATABASE ENTRY $insert_update = mysql_query("INSERT INTO `ticketevents` (`txn_id`, `mc_gross`, `tax`, `first_name`, `last_name`, `payer_email`, `address_street`, `address_city`, `address_state`, `address_zip`, `address_country_code`, `address_status`, `item_number`, `quantity`, `mc_currency`, `payment_status`, `payment_type`) VALUES (" . $p->ipn_data['txn_id'] . ", " . $p->ipn_data['mc_gross'] . ", " . $p->ipn_data['tax'] . ", " . $p->ipn_data['first_name'] . ", " . $p->ipn_data['last_name'] . ", " . $p->ipn_data['payer_email'] . ", " . $p->ipn_data['address_street'] . ", " . $p->ipn_data['address_city'] . ", " . $p->ipn_data['address_state'] . ", " . $p->ipn_data['address_zip'] . ", " . $p->ipn_data['address_country_code'] . ", " . $p->ipn_data['address_status'] . ", " . $p->ipn_data['item_number'] . ", " . $p->ipn_data['quantity'] . ", " . $p->ipn_data['mc_currency'] . ", " . $p->ipn_data['payment_status'] . ", " . $p->ipn_data['payment_type'] . ") "); And I haven't even got to the ON DUPLICATE KEY update. Can anybody see the problem with my database entry code? Quote Link to comment https://forums.phpfreaks.com/topic/144177-array-how-to-grab-certain-items-from-it/#findComment-757216 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.