jasonlive Posted October 3, 2013 Share Posted October 3, 2013 i have a php page that is opened by a form. when opened it displays 100's of rows of data from a database. what i need to do is have the user click on some rows, using either a link or a checkbox or something and then submit to the database that "that row" is now "checked" or "flagged" or something. i added a column to the database "flag" with value 0 or 1. also it is preferable to remain on same page after the database update. what would be the best way to do this? -i have tried putting a checkbox in each row, but then i dont know how to build the query at the end to submit multiple rows (how to access each row's name/value) -i then considered making each row a form itself...but seems bulky. (as i write this and wait for advice, im going to try this method it actually seems the easiest) -i considered making each row a link....but then how do i submit a query from a link(using javascript?!) Quote Link to comment https://forums.phpfreaks.com/topic/282694-codingalgorithmbest-practisedesign-help-required/ Share on other sites More sharing options...
Solution mac_gyver Posted October 3, 2013 Solution Share Posted October 3, 2013 you would have one form that encompasses all the checkboxes. the name= ' ... ' attribute of the checkbox form field would be an array, with a name that holds meaning for that field and the array index would be the id of the row from your database table - name='field[234]' when the form is submitted, all the checked checkboxes will exist in the $_POST['field'] array, with the keys being the list of id's. to set the database field value to a 1 for the checked checkboxes, you would use something like - $ids = implode(',',array_keys($_POST['field'])); $query = "UPDATE your_table SET field = 1 WHERE id IN($ids)"; to set the database field value to a 0 for the unchecked checkboxes, you would use something like - $ids = implode(',',array_keys($_POST['field'])); $query = "UPDATE your_table SET field = 0 WHERE id NOT IN($ids)"; Quote Link to comment https://forums.phpfreaks.com/topic/282694-codingalgorithmbest-practisedesign-help-required/#findComment-1452495 Share on other sites More sharing options...
.josh Posted October 3, 2013 Share Posted October 3, 2013 To stay on the same page, you could either have the form action point to the same page or else you can have a separate script handle the form post/processing and then make an ajax call instead of submitting the form. Quote Link to comment https://forums.phpfreaks.com/topic/282694-codingalgorithmbest-practisedesign-help-required/#findComment-1452496 Share on other sites More sharing options...
jasonlive Posted October 9, 2013 Author Share Posted October 9, 2013 (edited) thankyou! its alive . nothing beats that feeling of when code starts working! Edited October 9, 2013 by jasonlive Quote Link to comment https://forums.phpfreaks.com/topic/282694-codingalgorithmbest-practisedesign-help-required/#findComment-1453201 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.