86Stang Posted June 23, 2008 Share Posted June 23, 2008 Hi All, I've got a dilemma that I'm hoping someone out there can help me out with. I'm showing a list of classified ads pulling from a table - easy enough. What I'd like to do is have a checkbox next to each ad for printing. See the attached image for clarification. When the "Print Checked" link is clicked, it would generate a new page listing the checked ads. I'm hoping someone has come across this before and will be willing to share their knowledge. Thanks so much! [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/111520-printing-selected-items/ Share on other sites More sharing options...
ober Posted June 23, 2008 Share Posted June 23, 2008 Your checkboxes should all be named the same: "box1234" or something similar. When you submit the form, you search for anything in the $_POST array that begins with "box" (use substr) and get the number portion after it. The number portion would be the ID of the ad in your database. Then you simply build a query out of the string of IDs you just gathered and throw that in a new query: "SELECT X, Y, Z FROM table WHERE ad_id IN ($string_of_ids)". Quote Link to comment https://forums.phpfreaks.com/topic/111520-printing-selected-items/#findComment-572358 Share on other sites More sharing options...
dannyb785 Posted June 23, 2008 Share Posted June 23, 2008 provided each input checkbox have a unique name(I usually do it something like name="p$ad_id" that way every add has a different name): use the current() and next() functions to loop through the selected items. For example: while(current($_POST) != "") { $id = current($_POST); print_ad_info($id); // this will be whatever you want to display for the given ad id next($_POST); } OR to avoid a possible infinite loop, I always add a hidden input field at the end of the checkboxes with a value that the ads would never have(like -1000). So I'd add <input value="-1000" name="checker"> so then the while statement would change to: while(current($_POST) != "-1000") { same code as before } Quote Link to comment https://forums.phpfreaks.com/topic/111520-printing-selected-items/#findComment-572360 Share on other sites More sharing options...
86Stang Posted June 23, 2008 Author Share Posted June 23, 2008 I'm just not wrapping my head around this. I've got $ad_id as the auto_incrementing primary in the database. I've got this in the list page: <form action="print.php" method="post"> $result = mysql_query("select * from ad_table"); while ($row = mysql_fetch_array($result)) { echo $ad_description . "<br />Print:" . <input type=\"checkbox\" value=\"ad_id\" name=\"$ad_id\"> . "<br />"; } <input type="submit" value="Print Selected" /> </form> I'm not even sure this is pulling the info into _POST. Is there a way to check that? More to the point though is what would I put in the print page? Thanks again for any help! Quote Link to comment https://forums.phpfreaks.com/topic/111520-printing-selected-items/#findComment-572457 Share on other sites More sharing options...
dannyb785 Posted June 23, 2008 Share Posted June 23, 2008 checkboxes gave me trouble when i first used them bc I didnt realize that something that wasnt checked never was stored into the $_POST variable. So if you have 10 checkboxes but only 2 were checked, you'll only have 2 $_POST variables. Since you have no idea which ones were checked, doing a query of all ads displayed wont do anything for you. Instead, the only solution i know of is to step through the $_POST variable. If you want to(a good way to visualize it) is setup the form action page to do "print_r($_POST)" that way you can see what variables were created based on what items were checked. Quote Link to comment https://forums.phpfreaks.com/topic/111520-printing-selected-items/#findComment-572553 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.