bodenzord Posted July 10, 2006 Share Posted July 10, 2006 I think the subject says it all. I have a multidimensional array that I need to re-POST all the keys and variables in it's exact structure. How do I do this? For example, the array is structured as follows:[code] $_POST Array( [attributes] => Array ( [2] => Array ( [0] => Array ( [options_id] => 2 [option_name] => size [options_values_id] => 1 ) [1] => Array ( [options_id] => 2 [option_name] => size [options_values_id] => 2 ) ) [3] => Array ( [0] => Array ( [options_id] => 3 [option_name] => framing [options_values_id] => 3 ) [1] => Array ( [options_id] => 3 [option_name] => framing [options_values_id] => 4 ) ) ))[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14129-how-do-i-re-post-a-multidimensional-array/ Share on other sites More sharing options...
Houdini Posted July 10, 2006 Share Posted July 10, 2006 How about maybe showing the code or the form that actually produced such an array. Quote Link to comment https://forums.phpfreaks.com/topic/14129-how-do-i-re-post-a-multidimensional-array/#findComment-55343 Share on other sites More sharing options...
bodenzord Posted July 10, 2006 Author Share Posted July 10, 2006 I understand where you're coming from, but it's rather involved. So let me break it down:I'm altering the categories.php page within the OsCommerce admin area with a form to alter product attributes directly on this page instead of going to product_attributes.php. This is necessary for my site because the number of products is continuously growing and it's very cumbersome to make changes on the product_attributes.php page.Anyway, categories.php is currently functioning just fine. I'm just trying to customize it. So, after initially loading categories.php and making the necessary changes of the products attributes, it's redirected to itself but with a GET variable of "preview". This then allows me to preview the products attributes before inserting/updating the database. This is how the page normally works anyways, and I'm good up until this point. The redirect to the "preview" page works great, and the multidimensional arrays I've created are passed via HTTP_POST_VARS just fine.The next step is to redirect to the same page again with a GET variable of "update" and have the database updated. This is where I'm having a little difficulty. The problem is that I need to [u]re-POST[/u] these same arrays and variables back to the same page but with the new GET variable of "update". The page is currently redirecting fine(thanks to the OsCommerce team's code), the GET variable is working fine as well(thanks again to the OsCommerce team's code), all non-array variables are re-POSTED properly, but the multidimensional arrays I've created(my customization) are not passed through again because I don't know how to do this without rebuilding the entire arrays. As far as showing you code of how I'm trying to pass the multidimensional arrays through POST, I don't have any yet; I don't know how to do this. This is where I'm stuck. I was simply hoping that there was an easy way to re-POST the POST variables that are in a multidimensional array. With all non-array variables, this is easily accomplished with the following code(and once again, this is OsCommerce's code):[code]reset($HTTP_POST_VARS);while (list($key, $value) = each($HTTP_POST_VARS)) { if(!is_array($HTTP_POST_VARS[$key])){ echo tep_draw_hidden_filed($key, htmlspecialchars(stripslashes($value))); }}[/code]So I've tried using the serialize() and unserialize() functions, but that doesn't seem to work. I feel that I may just have to rebuild the arrays from scatch again, which is not very efficient. Does anybody have any ideas? Thank you for your time.Mike Quote Link to comment https://forums.phpfreaks.com/topic/14129-how-do-i-re-post-a-multidimensional-array/#findComment-55355 Share on other sites More sharing options...
bodenzord Posted July 11, 2006 Author Share Posted July 11, 2006 Alright...I solved my problem, but I feel that the code isn't very eloquent. I'm still hoping that someone can look at the following code and see if there is a better way to do what I'm doing. And FYI, the following code allows me to take two multi-dimensional arrays that are POST variables and re-POST them. One array has two levels and the other array has three levels. You'll notice that the code is working specifically on the named arrays and not anything generic. I was hoping that someone could offer some insight or suggestions the would simplify the code and generalize it to work on any multi-dimensional array of any size. Does anybody have any ideas?[code]/********************************************************************************************** The following section of code takes an HTTP_POST_VARS multi-dimensional array named HTTP_POST_VARS['options'] and allows the array to be re-POSTED. This is a two level array. The array is structured as follows: [options] => Array ( [0] => Array ( [id] => 4 [name] => size ) [1] => Array ( [id] => 3 [name] => framing ) )**********************************************************************************************/foreach($HTTP_POST_VARS['options'] as $key=>$value){ $a=0; $number = count($HTTP_POST_VARS['options'][$key]); while ($a<$number) { if (is_array($value)){ foreach($HTTP_POST_VARS['options'][$key] as $key2=>$value2){ echo tep_draw_hidden_field('options[' . $key . '][' . $key2 . ']', htmlspecialchars(stripslashes($value2))); } } $a++; }}/********************************************************************************************** The following section of code takes an HTTP_POST_VARS multi-dimensional array named HTTP_POST_VARS['attributes'] and allows the array to be re-POSTED. This is a three level array. The array is structured as follows: [attributes] => Array ( [4] => Array ( [0] => Array ( [pre_status] => true [post_status] => true [products_attributes_id] => 84 [options_id] => 4 [options_values_id] => 1 [option_name] => size [options_values_name] => 8x10 [options_price] => 75.0000 [price_prefix] => ) [1] => Array ( [pre_status] => true [post_status] => true [products_attributes_id] => 85 [options_id] => 4 [options_values_id] => 2 [option_name] => size [options_values_name] => 11x14 [options_price] => 90.0000 [price_prefix] => ) ) [3] => Array ( [0] => Array ( [pre_status] => false [post_status] => false [products_attributes_id] => 93 [options_id] => 3 [options_values_id] => 6 [option_name] => framing [options_values_name] => framed [options_price] => [price_prefix] => ) [1] => Array ( [pre_status] => false [post_status] => false [products_attributes_id] => 94 [options_id] => 3 [options_values_id] => 7 [option_name] => framing [options_values_name] => unframed [options_price] => [price_prefix] => ) ) )**********************************************************************************************/foreach($HTTP_POST_VARS['attributes'] as $key=>$value){ if (is_array($value)){ foreach($HTTP_POST_VARS['attributes'][$key] as $key2=>$value2){ $a=0; $number = count($HTTP_POST_VARS['attributes'][$key][$key2]); while ($a<$number) { if (is_array($value2)){ foreach($HTTP_POST_VARS['attributes'][$key][$key2] as $key3=>$value3){ echo tep_draw_hidden_field('attributes[' . $key . '][' . $key2 . '][' . $key3 . ']', htmlspecialchars(stripslashes($value3))); } } $a++; } } }}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14129-how-do-i-re-post-a-multidimensional-array/#findComment-56033 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.