fishbaitfood Posted September 5, 2011 Share Posted September 5, 2011 Hey all, This question is coming forth of another topic, but since that topic is not really about this issue, I posted this new topic. Okay, so I made a table, and each row is generated with a WHILE loop. In this while loop, there's also a form generated for each row/record, for updating them seperately. I'm having trouble with naming those forms to process them seperately. I could name them like name="form20110001", with the number being the record's id. But since there can be gaps between id's, how can I retreive them efficiently? And I want them to process all on a single process page, obviously. How can I do this easily? Can someone give me a concise example? Would appreciate it a lot. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/246488-handling-multiple-dynamic-forms-on-one-page/ Share on other sites More sharing options...
fishbaitfood Posted September 5, 2011 Author Share Posted September 5, 2011 This is my code so far: <?php $checkboxFields = array( 'fact-sent', 'fact-payed', 'domain', 'content','design-sent', 'design-approved', 'design-sliced', 'temp-website', 'temp-website-sent', 'temp-website-approved', 'cms', 'seo', 'analytics', 'webmaster-tools', 'website', 'website-online'); while ($row = mysql_fetch_array($result)) { $formHTML = "<td class=\"customer\">{$row['project']}</td>\n"; $formHTML .= "<td class=\"customer\">{$row['customer']}</td>\n"; foreach($checkboxFields as $fieldName) { $checked = ($row[$fieldName]) ? ' checked="checked"' : ''; $formHTML .= "<td class=\"data\"><input class=\"checkbox\" type=\"checkbox\" name=\"{$fieldName}\"{$checked} /></td>\n"; } // OUTPUT echo "<form name=\"form{$row['project']}\" class=\"checkbox_form\" action=\"process_form.php\">\n"; echo "<input type=\"hidden\" name=\"project\" value=\"{$row['project']}\" />\n"; echo "<tr>\n"; echo $formHTML; echo "<td><input type=\"submit\" name=\"submit\" value=\"Update\" /></td>\n"; echo "</tr>\n"; echo "</form>\n"; } ?> The content isn't really of importance, it's just about processing the multiple generated forms to one process file. I also can't really find a tutorial about this, which is very strange. This is a common thing used, right? Quote Link to comment https://forums.phpfreaks.com/topic/246488-handling-multiple-dynamic-forms-on-one-page/#findComment-1265706 Share on other sites More sharing options...
fishbaitfood Posted September 5, 2011 Author Share Posted September 5, 2011 Anyone? I would appreciate a simple example on how to do this. Quote Link to comment https://forums.phpfreaks.com/topic/246488-handling-multiple-dynamic-forms-on-one-page/#findComment-1265787 Share on other sites More sharing options...
PFMaBiSmAd Posted September 5, 2011 Share Posted September 5, 2011 If you are making a separate form for each row of data (your current code), by definition, that means that you can only submit one at a time. The id you put into the hidden field would uniquely identify which row you would update in the database table. Quote Link to comment https://forums.phpfreaks.com/topic/246488-handling-multiple-dynamic-forms-on-one-page/#findComment-1265788 Share on other sites More sharing options...
fishbaitfood Posted September 6, 2011 Author Share Posted September 6, 2011 Oh gosh... :-\ Sometimes I overlook the most obvious thing! Thanks for reminding me! Quote Link to comment https://forums.phpfreaks.com/topic/246488-handling-multiple-dynamic-forms-on-one-page/#findComment-1265800 Share on other sites More sharing options...
fishbaitfood Posted September 6, 2011 Author Share Posted September 6, 2011 Yesterday it seemed so obvious.. But while coding this, the problem comes back. So I have one big form. Inside it, I generate rows/records with a while loop. In that while loop, I generate fields with a foreach loop. Those fields have the same name in each row/record. And that's my problem now, when posting the data. How do I solve this efficiently? I'm breaking my head on this one. Quote Link to comment https://forums.phpfreaks.com/topic/246488-handling-multiple-dynamic-forms-on-one-page/#findComment-1266014 Share on other sites More sharing options...
PFMaBiSmAd Posted September 6, 2011 Share Posted September 6, 2011 So I have one big form If you changed the method from what you are doing in the code you posted (one form for each row of data, not one big form holding all the data), you would need to post your current code. Quote Link to comment https://forums.phpfreaks.com/topic/246488-handling-multiple-dynamic-forms-on-one-page/#findComment-1266021 Share on other sites More sharing options...
fishbaitfood Posted September 6, 2011 Author Share Posted September 6, 2011 I was just about to add my code to my previous post, but my time expired... But if it could be done with multiple forms fairly easy, I would prefer that actually. It's just the problem about naming those generated forms/inputs, to process them according to their ID (project) Here's the code: <?php $get_all = "SELECT * FROM checklist ORDER BY project ASC;"; include_once('connection.inc.php'); $con = mysql_connect(MYSQL_SERVER, MYSQL_USERNAME, MYSQL_PASSWORD) or die ("Connection failed: " . mysql_error()); mysql_select_db("cre8") or die ("Can't open database: " . mysql_error()); $result = mysql_query($get_all) or die ("Query failed: " . mysql_error()); $num = mysql_numrows($result); if($num < 1) { echo '<p class="info">No records in database.</p>'; } else { echo '<p class="info">Amount of records: '.$num.'</p>'; ?> <form name="check_form" class="checkbox_form" action="check_process.php"> <input class="submit_btn" type="submit" name="submit" value="Update" /> <table id="check_table"> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <tr> <td class="vert"> </td> <td class="vert"> </td> <td class="vert"><p>fact-sent</p></td> <td class="vert"><p>fact-payed</p></td> <td class="vert"><p>domain</p></td> <td class="vert"><p>content</p></td> <td class="vert"><p>design-sent</p></td> <td class="vert"><p>design-approved</p></td> <td class="vert"><p>design-sliced</p></td> <td class="vert"><p>temp-website</p></td> <td class="vert"><p>temp-website-sent</p></td> <td class="vert"><p>temp-website-approved</p></td> <td class="vert"><p>cms</p></td> <td class="vert"><p>seo</p></td> <td class="vert"><p>analytics</p></td> <td class="vert"><p>webmaster-tools</p></td> <td class="vert"><p>website</p></td> <td class="vert"><p>website-online</p></td> </tr> <?php $checkboxFields = array( 'fact-sent', 'fact-payed', 'domain', 'content','design-sent', 'design-approved', 'design-sliced', 'temp-website', 'temp-website-sent', 'temp-website-approved', 'cms', 'seo', 'analytics', 'webmaster-tools', 'website', 'website-online'); while ($row = mysql_fetch_array($result)) { $formHTML = "<td class=\"customer\">{$row['project']}</td><input type=\"hidden\" name=\"project[]\" value=\"{$row['project']}\" />\n"; $formHTML .= "<td class=\"customer\">{$row['customer']}</td>\n"; foreach($checkboxFields as $fieldName) { $checked = ($row[$fieldName]) ? ' checked="checked"' : ''; $formHTML .= "<td class=\"data\"><input class=\"checkbox\" type=\"checkbox\" name=\"{$fieldName}\"{$checked} /></td>\n"; } // OUTPUT echo "<tr>\n"; echo $formHTML; echo "</tr>\n"; } ?> </table> </form> <?php } ?> And with this script, I can't even echo out the posted ID's (projects). I have a blank page, containing the data in the url. <?php $checkboxFields = array( 'fact-sent', 'fact-payed', 'domain', 'content','design-sent', 'design-approved', 'design-sliced', 'temp-website', 'temp-website-sent', 'temp-website-approved', 'cms', 'seo', 'analytics', 'webmaster-tools', 'website', 'website-online'); if(isset($_POST['submit'])) { $projects = $_POST['project']; $projects = implode(', ', $projects); echo "<p>{$projects}</p>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/246488-handling-multiple-dynamic-forms-on-one-page/#findComment-1266023 Share on other sites More sharing options...
PFMaBiSmAd Posted September 6, 2011 Share Posted September 6, 2011 In your other thread for this problem, mjdamato both told and showed how to use one big form and identify which record the checkbox belong with - If you want one form to update many records, the solution is simply. Name the fields so that they are arrays with the index of each field the record id. <input type="checkbox" name="fieldname[recordID]" /> The fieldname portion of that would be your $fieldName variable. The recordID would apparently be from $row['project'] Quote Link to comment https://forums.phpfreaks.com/topic/246488-handling-multiple-dynamic-forms-on-one-page/#findComment-1266047 Share on other sites More sharing options...
fishbaitfood Posted September 6, 2011 Author Share Posted September 6, 2011 Damn, I totally forgot about that post, as the thread went on. Okay, so I modified my code to that. But for the processing part, I need to link the fieldNames with the corresponding ID's. I have no idea how I would build a query upon that. The process script so far: <?php $checkboxFields = array( 'fact-sent', 'fact-payed', 'domain', 'content','design-sent', 'design-approved', 'design-sliced', 'temp-website', 'temp-website-sent', 'temp-website-approved', 'cms', 'seo', 'analytics', 'webmaster-tools', 'website', 'website-online'); if(isset($_POST['submit'])) { include_once('connection.inc.php'); $con = mysql_connect(MYSQL_SERVER, MYSQL_USERNAME, MYSQL_PASSWORD) or die ("Connection failed: " . mysql_error()); mysql_select_db("cre8") or die ("Can't open database: " . mysql_error()); foreach($checkboxFields as $fieldName) { $field = $_POST[$fieldName]; } // $field = array(20110001, 20110001, ..., 20110002, ...); CORRECT ? // How do I link this array with the $checkboxFields array, to build an update-query for each input? echo "<p>Hello</p>"; // The page remains blank ?! } ?> Also, after submitting the form, I can't echo anything out. What's up with that? I really appreciate your help man. Quote Link to comment https://forums.phpfreaks.com/topic/246488-handling-multiple-dynamic-forms-on-one-page/#findComment-1266064 Share on other sites More sharing options...
PFMaBiSmAd Posted September 6, 2011 Share Posted September 6, 2011 Basically, you could do this - <?php $checkboxFields = array( 'fact-sent', 'fact-payed', 'domain', 'content','design-sent', 'design-approved', 'design-sliced', 'temp-website', 'temp-website-sent', 'temp-website-approved', 'cms', 'seo', 'analytics', 'webmaster-tools', 'website', 'website-online'); echo "<pre>",print_r($_POST,true),"</pre>"; if(isset($_POST['submit'])) { $pivot = array(); // swap the columns and rows foreach($checkboxFields as $fieldName){ if(isset($_POST[$fieldName])){ // at least one checkbox is set foreach($_POST[$fieldName] as $project=>$dummy){ $pivot[$project][] = $fieldName; } } } echo "<pre>",print_r($pivot,true),"</pre>"; foreach($pivot as $project=>$array){ $fields = array(); foreach($checkboxFields as $fieldName){ if(in_array($fieldName,$array)){ $fields[] = "`$fieldName` = TRUE"; // whatever value you are using to indicate checked } else { $fields[] = "`$fieldName` = NULL"; // whatever value you are using to indicate not-checked } } $query = "UPDATE your_table SET " . implode(',',$fields) . " WHERE project = $project"; echo $query . "<br />"; // execute your query here } } Quote Link to comment https://forums.phpfreaks.com/topic/246488-handling-multiple-dynamic-forms-on-one-page/#findComment-1266159 Share on other sites More sharing options...
fishbaitfood Posted September 6, 2011 Author Share Posted September 6, 2011 Hmm, seems like a big connection of arrays! Could you please explain me in detail what the code is doing exactly? For instance, what's the $dummy used for? As of now, it echoes an empty array: Array ( ) Thank you for the great effort! Really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/246488-handling-multiple-dynamic-forms-on-one-page/#findComment-1266248 Share on other sites More sharing options...
PFMaBiSmAd Posted September 7, 2011 Share Posted September 7, 2011 Your form does not have a method="post" attribute, so the data is being sent via GET. Quote Link to comment https://forums.phpfreaks.com/topic/246488-handling-multiple-dynamic-forms-on-one-page/#findComment-1266255 Share on other sites More sharing options...
fishbaitfood Posted September 7, 2011 Author Share Posted September 7, 2011 Omg, how in the world could I forget that?! I feel ashamed you had to find out something as silly as that was missing. But wow.. that code is fantastic! It works like a charm now! Thank you soo much! Also for echoing out the print_r's, really helpful! I've already asked a lot, but, just out of curiosity.. Is it possible to NOT update a record when it isn't modified, since its last update? Again, thanks a lot man, really appreciate it! Guess I got to learn a lot about nested loops and arrays. Quote Link to comment https://forums.phpfreaks.com/topic/246488-handling-multiple-dynamic-forms-on-one-page/#findComment-1266264 Share on other sites More sharing options...
PFMaBiSmAd Posted September 7, 2011 Share Posted September 7, 2011 NOT update a record when it isn't modified You would need to retrieve the current data (or pass it through the form somehow), then compare the original values with the submitted values. Either method would all take extra time and more code. However, there's little point in doing that because if you try to UPDATE a value to the same value it already is, mysql doesn't actually go to the trouble of writing the value to the database. So, if none of the values in a row are changed, yes you would still be executing an UPDATE query, but mysql would short-circuit the process and not actually take the time to write the row back into the database. If you set a column to the value it currently has, MySQL notices this and does not update it. Quote Link to comment https://forums.phpfreaks.com/topic/246488-handling-multiple-dynamic-forms-on-one-page/#findComment-1266266 Share on other sites More sharing options...
fishbaitfood Posted September 7, 2011 Author Share Posted September 7, 2011 Thank you for clearing that out! Didn't know MySQL was that intuitive, nice. Again, many thanks! Quote Link to comment https://forums.phpfreaks.com/topic/246488-handling-multiple-dynamic-forms-on-one-page/#findComment-1266268 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.