vynsane Posted May 14, 2007 Share Posted May 14, 2007 Hi, all... I have a form that is dynamically generated by the fields I have in a table. This table generates the navigation for my site, and I want to create a page that will allow me to reorder and hide/show and activate/deactivate the links. The form is working and I have the form generating an array based on the data collected on submit. I need to know how to break this data apart so I can update my site navigation table with the correct values. The table looks like this: | ID | orderNum | sectionName | link | visible | active | the ID is just a primary auto increment. The orderNum is one of the cells that needs to be updated by the form, as are "visible" and "active". orderNum has a value anywhere between "1" and "12" right now (but could be more depending on sections added...) and visible and active only have a value of either "Y" or "N" (for yes or no, natch...) I have a form that just outputs the array onscreen after submit, and the array looks like this: Array ( [orderNum] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 [10] => 11 [11] => 12 ) [visible] => Array ( [0] => Y [1] => Y [2] => Y [3] => Y [4] => N [5] => Y [6] => Y [7] => Y [8] => Y [9] => N [10] => N [11] => N ) [acitve] => Array ( [0] => Y [1] => Y [2] => Y [3] => Y [4] => Y [5] => Y [6] => Y [7] => Y [8] => Y [9] => Y [10] => Y [11] => Y ) [submit] => Update Navigation ) obviously, the "0's" go together (1, Y, Y), the "1's" (2, Y, Y), the "5's" (4, N, Y) and so on... how can I break this data apart so as to insert/update them into the correct record? Quote Link to comment https://forums.phpfreaks.com/topic/51366-solved-form-data-array-need-to-update-table/ Share on other sites More sharing options...
Barand Posted May 14, 2007 Share Posted May 14, 2007 <?php foreach ($data['orderNum'] as $k = $ono) { $visible = $data['visible'][$k]; $active = $data['active'][$k]; // update using $ono, $visible, $active } ?> Quote Link to comment https://forums.phpfreaks.com/topic/51366-solved-form-data-array-need-to-update-table/#findComment-252938 Share on other sites More sharing options...
vynsane Posted May 14, 2007 Author Share Posted May 14, 2007 Okay, cool. Looks pretty straight-forward. Should I use a hidden "ID" input to WHERE against in my UPDATE? EDIT: Hrm... getting an error: Parse error: syntax error, unexpected '=', expecting ')' in path/to/sitenav.php on line 31 that's this line: foreach ($data['orderNum'] as $k = $orderNum) { (I changed $ono to $orderNum just so as to keep things straight in my head...) Quote Link to comment https://forums.phpfreaks.com/topic/51366-solved-form-data-array-need-to-update-table/#findComment-252952 Share on other sites More sharing options...
Barand Posted May 14, 2007 Share Posted May 14, 2007 change to this (I always forget the ">") foreach ($data['orderNum'] as $k => $orderNum) { Quote Link to comment https://forums.phpfreaks.com/topic/51366-solved-form-data-array-need-to-update-table/#findComment-252960 Share on other sites More sharing options...
vynsane Posted May 14, 2007 Author Share Posted May 14, 2007 Ah, yes, that did the trick. What about adding the ID as a WHERE in the UPDATE? Like this... foreach ($data['orderNum'] as $k => $orderNum) { $visible = $data['visible'][$k]; $active = $data['active'][$k]; $ID = $data['ID'][$k]; $insert = "UPDATE (table name) SET orderNum='$orderNum', visible='$visible', active='$active' WHERE ID = $ID"; I'm getting an error on submission of the form: Warning: Invalid argument supplied for foreach() in /path/to/sitenav.php on line 31 Should I be using "$_POST" instead of "$data"? Quote Link to comment https://forums.phpfreaks.com/topic/51366-solved-form-data-array-need-to-update-table/#findComment-252967 Share on other sites More sharing options...
Barand Posted May 14, 2007 Share Posted May 14, 2007 Yes, good idea to include an array of IDs too Quote Link to comment https://forums.phpfreaks.com/topic/51366-solved-form-data-array-need-to-update-table/#findComment-252983 Share on other sites More sharing options...
vynsane Posted May 14, 2007 Author Share Posted May 14, 2007 It's not updating the table... my "if (mysql_query($insert)) is telling me that the insert is working, but nothing is getting changed. I have the entire thing set up like this at the moment: if (isset($_POST['submit'])){ print_r($_POST); foreach ($_POST['orderNum'] as $k => $orderNum) { $visible = $_POST['visible'][$k]; $active = $_POST['active'][$k]; $ID = $_POST['ID'][$k]; // update using $orderNum, $visible, $active $insert = "UPDATE (table name) SET orderNum='$orderNum', visible='$visible', active='$active' WHERE ID = $ID"; } } It's just not updating anything. I no longer got the error above Warning: Invalid argument supplied for foreach() in /path/to/sitenav.php on line 31 using "$_POST" instead of "$data", but should I go back to "$data"? Quote Link to comment https://forums.phpfreaks.com/topic/51366-solved-form-data-array-need-to-update-table/#findComment-252988 Share on other sites More sharing options...
Barand Posted May 14, 2007 Share Posted May 14, 2007 I used "$data" as all I knew from your first post was that it was an array and I had to call it something. Quote Link to comment https://forums.phpfreaks.com/topic/51366-solved-form-data-array-need-to-update-table/#findComment-253001 Share on other sites More sharing options...
vynsane Posted May 14, 2007 Author Share Posted May 14, 2007 I figured it out, I had my query for the insert outside of the foreach. Thanks so much for your help! Quote Link to comment https://forums.phpfreaks.com/topic/51366-solved-form-data-array-need-to-update-table/#findComment-253012 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.