JordanC Posted June 23, 2021 Share Posted June 23, 2021 (edited) Hi! My question is probably simple but I've been scratching my head for hours now... I'm pretty new to php. I have an online form for orders; when submitted, an email is sent to the shop manager containing the info the client has filled in. So I pass all the info in the code below, but I can't manage to echo the array containing the order's items/qty/item_code. (I'm using Wordpress, Oxygen Builder, Metabox and Code Snippets, if that helps). Here's the code (used as a Code Snippet): add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) { if ( 'command-form' !== $config['id'] ) { return; } $name = rwmb_meta( 'name', '', $post_id ); $email = rwmb_meta( 'email', '', $post_id ); $phone = rwmb_meta( 'phone', '', $post_id ); $orders = rwmb_meta( 'order', '', $post_id ); */ ---> this is the array I wish to show the elements from (array --> [0]:{item:"item", qty:"qty", code:"code"]}, [1]:{}, etc...} */ $comments = rwmb_meta( 'comments', '', $post_id ); $reception = rwmb_meta( 'reception', '', $post_id ); $destination = rwmb_meta( 'destination', '', $post_id ); $payment = rwmb_meta( 'payment', '', $post_id ); $body = "Vous avez reçu une nouvelle commande en ligne! Nom: $name Courriel: $email Téléphone: $phone Commande: */ ---> this is where I want it displayed, but since it is within $body = " ", I can't seem to be able to use foreach( ) or even print_r( )... */ Commentaires: $comments Réception: $reception Paiement: $payment"; $headers = ['Content-type: text/html', "Reply-To: $email"]; wp_mail( 'address@domain.com', 'Nouvelle commande en ligne', $body ); }, 10, 2 ); Help with this would be greatly appreciated! Please let me know if any details are missing. Thanks in advance! Jordan Edited June 23, 2021 by JordanC Quote Link to comment https://forums.phpfreaks.com/topic/312967-echo-an-array-inside-form-confirmation-email/ Share on other sites More sharing options...
Solution kicken Posted June 23, 2021 Solution Share Posted June 23, 2021 (edited) Use a foreach loop above the $body and create a new variable containing the order details as a string, then use that variable in $body. $orderDetails = ''; foreach ($orders as $order){ $orderDetails .= $order . '\n'; } It seems your orders are JSON encoded, so you'll probably want to json_decode them and format them nicely within that foreach loop. Edited June 23, 2021 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/312967-echo-an-array-inside-form-confirmation-email/#findComment-1587497 Share on other sites More sharing options...
JordanC Posted June 23, 2021 Author Share Posted June 23, 2021 Thanks a lot! With some tinkering it worked, except for one small detail: I doubled the 'foreach' loop since it is an array in an array, like this: $orderDetails = ''; foreach ($orders as $order){ $order_d = ''; foreach ($order as $order_element){ $order_d .= $order_element . ' / '; } $orderDetails .= '- ' . $order_d . '<br>'; } and the outcome is: Nom: Jordan Courriel: --- Téléphone: 555-5555 Commande: - feux d'artifice / 1 / 3546 / <br>- ampoule 100w / 1 / 3456 / <br> Commentaires: Réception: cueillette Paiement: comptant Small problem is, whichever I use, '/n' or '<br>', it just shows up as plain text... I've tried using 'var_dump(json_decode($variable));' on my variables, but then nothing shows at all in the output message. I've read the 'json_decode' link twice and tried a bunch of stuff within the loop, but must be missing something. Your reply was right on, though, I'll mark it as the solution! Quote Link to comment https://forums.phpfreaks.com/topic/312967-echo-an-array-inside-form-confirmation-email/#findComment-1587498 Share on other sites More sharing options...
Barand Posted June 23, 2021 Share Posted June 23, 2021 Post the raw data that you get in $order before you start your processing and we'll see if we can help with your formatting Quote Link to comment https://forums.phpfreaks.com/topic/312967-echo-an-array-inside-form-confirmation-email/#findComment-1587500 Share on other sites More sharing options...
JordanC Posted June 23, 2021 Author Share Posted June 23, 2021 Sure! Here's 3 tests I added to the code (the submitted form contained 2 arrays/order-elements): $test_raw = ''; foreach ($orders as $order){ $test_raw .= $order . '/n'; } $test_json = ''; foreach ($orders as $order){ $json_dec = var_dump(json_decode($order)); $test_json .= $json_dec . '/n'; } $test_json_2 = ''; $dec_orders = var_dump(json_decode($orders)); foreach ($dec_orders as $dec_order){ $test_json_2 .= $dec_order . '/n'; } And here's the output: Test_raw: Array/nArray/n Test_json: /n/n Test_json_2: Quote Link to comment https://forums.phpfreaks.com/topic/312967-echo-an-array-inside-form-confirmation-email/#findComment-1587502 Share on other sites More sharing options...
Barand Posted June 23, 2021 Share Posted June 23, 2021 Nothing there resembles what you had in your original post, which was 2 hours ago, JordanC said: [0]:{item:"item", qty:"qty", code:"code"]}, [1]:{}, etc...} Quote Link to comment https://forums.phpfreaks.com/topic/312967-echo-an-array-inside-form-confirmation-email/#findComment-1587503 Share on other sites More sharing options...
JordanC Posted June 23, 2021 Author Share Posted June 23, 2021 (edited) That was from memory of how the array is structured, not an output. Just tried a few more things trying to access the raw data, with poor results: $test_orderDetails = ''; foreach ($orders as $order){ $dec_order = var_dump(json_decode($order)); $order_d = ''; foreach ($dec_order as $order_element){ $order_d .= $order_element . ' - '; } $test_orderDetails .= '- ' . $order_d . '|||'; } $test_printr = ''; foreach ($orders as $order){ $p_order = print_r($order); $test_printr .= $p_order . '/n'; } $test_ordersPr = print_r($orders); To get this: Test_orderDetails: - |||- ||| Test_printr: 1/n1/n Test_ordersPr: 1 When this code: $orderDetails = ''; foreach ($orders as $order){ $order_d = ''; foreach ($order as $order_element){ $order_d .= $order_element . ' - '; } $orderDetails .= '- ' . $order_d . '|||'; } Outputs this: Commande: - feux d'artifice - 1 - |||- vis 3/4 - 1 - ||| Edited June 23, 2021 by JordanC Quote Link to comment https://forums.phpfreaks.com/topic/312967-echo-an-array-inside-form-confirmation-email/#findComment-1587504 Share on other sites More sharing options...
kicken Posted June 23, 2021 Share Posted June 23, 2021 If you simply var_dump($orders); then it will show you what exactly is contained in that variable. I'm guessing is just a multi-dimensional array and not JSON like your original post implied. If so, you'd just access whatever keys you need or foreach over them all. foreach ($orders as $order){ foreach ($order as $key => $value){ $orderDetails .= $key.': '.$value.'\n'; } //... } Quote Link to comment https://forums.phpfreaks.com/topic/312967-echo-an-array-inside-form-confirmation-email/#findComment-1587508 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.