solarisuser Posted April 19, 2007 Share Posted April 19, 2007 Hi All, I am curious what everyone things would be the best way to tackle this... I have a book reservation system, and it paginates depending on the number of results. I let users check a checkbox next to a record, and they can do that to as many records listed on that page. Then they click on the submit button and it works. The problem is when the results are paginated, for example, listing 10 records out of 100 (so there are 10 pages), when the user selected a few checkboxes and clicks on another page, the previously checked records are forgotten. My goal is to have those previously checked records remembered so when the user finally clicks on the Submit button, all checked entries will be used. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/47779-checkboxes-pagination-woes/ Share on other sites More sharing options...
boo_lolly Posted April 19, 2007 Share Posted April 19, 2007 how do you want these checked boxes to be 'remembered'? would you like it to be remembered in a database, or in a session? Quote Link to comment https://forums.phpfreaks.com/topic/47779-checkboxes-pagination-woes/#findComment-233413 Share on other sites More sharing options...
rcorlew Posted April 19, 2007 Share Posted April 19, 2007 Try using and serializng sessions to store the selected items. That would probably be the easiest way to store selections across multiple pages then when the script is ended just pull the selected items back out of the sessions array by unserializing them. Quote Link to comment https://forums.phpfreaks.com/topic/47779-checkboxes-pagination-woes/#findComment-233416 Share on other sites More sharing options...
solarisuser Posted April 19, 2007 Author Share Posted April 19, 2007 how do you want these checked boxes to be 'remembered'? would you like it to be remembered in a database, or in a session? I'd say a session only because that way if someone checks 5 records and exists the browser, the sessions will go away..? Quote Link to comment https://forums.phpfreaks.com/topic/47779-checkboxes-pagination-woes/#findComment-233428 Share on other sites More sharing options...
solarisuser Posted April 19, 2007 Author Share Posted April 19, 2007 Try using and serializng sessions to store the selected items. That would probably be the easiest way to store selections across multiple pages then when the script is ended just pull the selected items back out of the sessions array by unserializing them. So I should look at saving all "checked" <input type=checkbox> fields's id numbers into sessions when a page number is selected or not selected, right? Then have the submit button's code use the sessions instead of $_GET/$_POST? Just trying to wrap my head around it. Quote Link to comment https://forums.phpfreaks.com/topic/47779-checkboxes-pagination-woes/#findComment-233442 Share on other sites More sharing options...
Ninjakreborn Posted April 19, 2007 Share Posted April 19, 2007 Pagination sucks. I have always had problems dealing with it as well. I have my own thing that works but now I need something fancy, I am looking for a pagination class, or I Am going to build one, that's one of the thorns in the side of a programmer, pagination and dealing with dates. Quote Link to comment https://forums.phpfreaks.com/topic/47779-checkboxes-pagination-woes/#findComment-233443 Share on other sites More sharing options...
Nameless12 Posted April 19, 2007 Share Posted April 19, 2007 Hi All, I am curious what everyone things would be the best way to tackle this... I have a book reservation system, and it paginates depending on the number of results. I let users check a checkbox next to a record, and they can do that to as many records listed on that page. Then they click on the submit button and it works. The problem is when the results are paginated, for example, listing 10 records out of 100 (so there are 10 pages), when the user selected a few checkboxes and clicks on another page, the previously checked records are forgotten. My goal is to have those previously checked records remembered so when the user finally clicks on the Submit button, all checked entries will be used. Thanks you are going to need to make a next page button part of the form so the selected data is submit, otherwise you will need to use ajax to send the selected items as soon as they click on whatever it is you want to record. Quote Link to comment https://forums.phpfreaks.com/topic/47779-checkboxes-pagination-woes/#findComment-233466 Share on other sites More sharing options...
boo_lolly Posted April 19, 2007 Share Posted April 19, 2007 how do you want these checked boxes to be 'remembered'? would you like it to be remembered in a database, or in a session? I'd say a session only because that way if someone checks 5 records and exists the browser, the sessions will go away..? that is correct, at least, after a few minutes they will go away. the reason why i asked is because they way you store the data will change, but the way you construct your checkbox form will not. that will be the same no matter how you store it. a quick example of how you would probably be constructing your checkbox form: <?php echo "<pre>". print_r($_POST, true) ."</pre>\n"; $sql = "SELECT * FROM your_table; $query = mysql_query($sql); echo "<form method=\"post\" action=\"\">\n"; while($row = mysql_fetch_array($query)){ echo "<input type=\"checkbox\" name=\"checked[]\" value=\"{$row['id']}\""; foreach($_POST['checked'] as $val){ echo (($row['id'] == $val) ? (" CHECKED") : ("")); } echo ">{$row['book_name']}\n"; } echo "<input type\"submit\" value=\"Submit\">\n"; echo "</form>\n"; ?> now, all the boxes that were checked will be stored in an array called $_POST['checked']. the print_r($_POST) will display all the checked boxes. you can then add them to your session, or you can add them to a database. it's up to you from there. if you need any more help, let us know. you are going to need to make a next page button part of the form so the selected data is submit, otherwise you will need to use ajax to send the selected items as soon as they click on whatever it is you want to record. that is not true. in my example i have set the form to post to itself. this way, the users can add books from the list and click submit. the page will reload with the books they want already checked. then, the user can choose to go to a different page. it is better to leave these two features independant of one another. Quote Link to comment https://forums.phpfreaks.com/topic/47779-checkboxes-pagination-woes/#findComment-233521 Share on other sites More sharing options...
solarisuser Posted April 19, 2007 Author Share Posted April 19, 2007 a quick example of how you would probably be constructing your checkbox form: <?php echo "<pre>". print_r($_POST, true) ."</pre>\n"; $sql = "SELECT * FROM your_table; $query = mysql_query($sql); echo "<form method=\"post\" action=\"\">\n"; while($row = mysql_fetch_array($query)){ echo "<input type=\"checkbox\" name=\"checked[]\" value=\"{$row['id']}\""; foreach($_POST['checked'] as $val){ echo (($row['id'] == $val) ? (" CHECKED") : ("")); } echo ">{$row['book_name']}\n"; } echo "<input type\"submit\" value=\"Submit\">\n"; echo "</form>\n"; ?> now, all the boxes that were checked will be stored in an array called $_POST['checked']. the print_r($_POST) will display all the checked boxes. you can then add them to your session, or you can add them to a database. it's up to you from there. if you need any more help, let us know. That's awesome. I've integrated it the way it needs to look and all is well except for one thing. I am trying to figure out where I would use sessions (obviously I need to) with the code above, so the checked boxes will be remembered. Currently, the page numbers at the bottom of the search are <A HREF> links with a whole of bunch of &page=123&blah=123&etc=123 dynamically generated for each A HREF. Since the id is stored in $_POST, at one point would I use sessions? Without the page numbers being part of the form and requiring the submit button, how could I pass $_POST? Ahhh I'm confused =( Quote Link to comment https://forums.phpfreaks.com/topic/47779-checkboxes-pagination-woes/#findComment-233593 Share on other sites More sharing options...
boo_lolly Posted April 19, 2007 Share Posted April 19, 2007 yeah this part will be a little tricky. the reason it will get tricky is because sometimes, people will go back to a page that they've already checked some books. but now, let's say they want to remove some of the original books and add some different ones. well, if you simply insert the entire $_POST array into the $_SESSION array without checking, you're going to get multiple books of the same type. you don't want that. so, the trick is that every time somebody wants to 'submit' new books, or a new set of books differing from their original set into the $_SESSION, you have to write a function that will compare this new list that wants to be added with the list that already exists in the $_SESSION array. if there are matches, it should not be added. however, that brings on the question, how would i remove the books that they 'uncheck' from the form? since $_POST only stores the ones that were checked, there's no way to know if they wanted to remove one... suddenly i'm reminded of a shopping cart. you can remove items, but only if you're at a page where you can see all the contents of your cart, and choose to remove them one by one... you cannot do this from the 'browsing' page. you can only do it from the 'your cart' page. hmmm. how is the rest of your site constructed around your browse/add/remove books feature? so, i'm thinking, you add books from the browse pages. and you can remove them from another page that shows what you have listed.... hmmm... what do you think? Quote Link to comment https://forums.phpfreaks.com/topic/47779-checkboxes-pagination-woes/#findComment-233615 Share on other sites More sharing options...
solarisuser Posted April 19, 2007 Author Share Posted April 19, 2007 ack, this was the feeling of despair I had originally when I realized it was shopping-cart style.. I have a table with some text fields or dropdown fields, and when a user narrows down the critereia, they click search. then the code in " if(isset($_POST['submit'])) " kicks in, and shows all results with pagination if over X amount of results. Currently, I have a dynamically generated link (e.g - checkout.php?id=123) next to each book title, but for someone checking out 20 books, it seems like a huge waste of time compared to the checkbox method. i guess the difference with this versus amazon.com's shopping cart is that amazon does not use checkboxes. this could be good for me because I can concentrate on figuring out how to get your code to work like we originally wanted to. one idea is to use javascript or something to append the checked entries id number in the URL of each page number or "next" links. (e.g - &checked=123&checked=456). The main problem is figuring out, once the initial page loads with the results and empty checkboxes, how to know what checkboxes have been selected if any once you click on a page number or next link. Any thoughts? BTW - I just had another idea - could I use onClick to put the value of "id=" in a session or something using JS for each checkbox field? For example, <input type=checkbox id=123 name=checkbox[] onClick="rememberCheckBox(this.form)">? I don't know any JS so I'm just guessing... yeah this part will be a little tricky. the reason it will get tricky is because sometimes, people will go back to a page that they've already checked some books. but now, let's say they want to remove some of the original books and add some different ones. well, if you simply insert the entire $_POST array into the $_SESSION array without checking, you're going to get multiple books of the same type. you don't want that. so, the trick is that every time somebody wants to 'submit' new books, or a new set of books differing from their original set into the $_SESSION, you have to write a function that will compare this new list that wants to be added with the list that already exists in the $_SESSION array. if there are matches, it should not be added. however, that brings on the question, how would i remove the books that they 'uncheck' from the form? since $_POST only stores the ones that were checked, there's no way to know if they wanted to remove one... suddenly i'm reminded of a shopping cart. you can remove items, but only if you're at a page where you can see all the contents of your cart, and choose to remove them one by one... you cannot do this from the 'browsing' page. you can only do it from the 'your cart' page. hmmm. how is the rest of your site constructed around your browse/add/remove books feature? so, i'm thinking, you add books from the browse pages. and you can remove them from another page that shows what you have listed.... hmmm... what do you think? Quote Link to comment https://forums.phpfreaks.com/topic/47779-checkboxes-pagination-woes/#findComment-233624 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.