Julian Posted January 10, 2007 Share Posted January 10, 2007 Hi Guys.Maybe you can help me with this problem I have:I have two tables, Packages and Tours, I need to associate tours with several packages. I found the solution to insert the data into the DB using implode (Worked great). The info on the DB look like this: (1,3,5)On the table Tours I have Checkboxes where you can choose your Packages as follows:<?php do {?><table width="530" border="0" cellspacing="0" cellpadding="5"><tr><td width="18"><input type="checkbox" name="id_packages[]" value="<?php echo $row_package['id_packages']?>"/></td><td><?php echo $row_package['name']?></td></tr></table><?php} while ($row_package = mysql_fetch_assoc($package));?>Here's the BIG question: I want to retrieve the information from the database and have the checkboxes "selected" with the info on the database. I tried explode but no luck. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/33665-retrieve-checkboxes-selected/ Share on other sites More sharing options...
magic2goodil Posted January 10, 2007 Share Posted January 10, 2007 That's some ugly code... ??? Quote Link to comment https://forums.phpfreaks.com/topic/33665-retrieve-checkboxes-selected/#findComment-157748 Share on other sites More sharing options...
Julian Posted January 10, 2007 Author Share Posted January 10, 2007 Sorry for the ugly code...Maybe you can help me with it! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/33665-retrieve-checkboxes-selected/#findComment-157751 Share on other sites More sharing options...
magic2goodil Posted January 10, 2007 Share Posted January 10, 2007 Lol, I was just givin you crap. I'm not sure what exactly you are trying to do though, but your code you have far should be like this i think:[code]<?php while ($row_package = mysql_fetch_assoc($package)) {<table width="530" border="0" cellspacing="0" cellpadding="5">foreach($row_package as $key => $row) {echo"<tr><td width=\"18\"><input type=\"checkbox\" name=\"".$key."\" value=\"" .$row['id_packages']."\"></td><td>$row_package['name']</td></tr>";}</tr></table>}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33665-retrieve-checkboxes-selected/#findComment-157755 Share on other sites More sharing options...
Daniel0 Posted January 10, 2007 Share Posted January 10, 2007 The ones that are checked will be stored in an array called $_POST['id_packages'] (or $_GET depending on the method). Just loop through it with a foreach loop. Quote Link to comment https://forums.phpfreaks.com/topic/33665-retrieve-checkboxes-selected/#findComment-157756 Share on other sites More sharing options...
ToonMariner Posted January 10, 2007 Share Posted January 10, 2007 its ok by me julian! I very much prefer to break out of php when formatting html - only keep html in echo statements when there is very small amounts of html to do.OKIts a little confusing what you wish to achive BUT just to be clear this is what I think you are trying to do.You show a table of tours and in that table you show which packages have that tour. The you want the users to select which packages they want.[code]<?php$tour = "SELECT * FROM `tours` WHERE `tour_id` = " . $_GET['tour_id'];$tour = mysql_query($tour);$tour = mysql_fetch_assoc($tour);$pack = "SELECT * FROM `packages` WHERE `package_id` IN (. $tour['packages'] .)";$pack = mysql_query($pack);?><table width="530" border="0" cellspacing="0" cellpadding="5"><tr> <td class="tourtitle"><?php echo $tour['title']; ?></td></tr><?phpwhile ($row_package = mysdql_fetch_assoc($pack)){?><tr> <td> <label> <input type="checkbox" name="id_packages[]" value="<?php echo $row_package['id_packages']?>" /> <?php echo $row_package['name']?> </label> </td></tr><?php}?></table>[/code]I think that should do the trick. Quote Link to comment https://forums.phpfreaks.com/topic/33665-retrieve-checkboxes-selected/#findComment-157766 Share on other sites More sharing options...
Julian Posted January 11, 2007 Author Share Posted January 11, 2007 Thanks for the big help ToonMariner, I'm getting this error:Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in...On this line:$pack = "SELECT * FROM `packages` WHERE `package_id` IN (. $tour['packages'] .)";Maybe something is missing. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/33665-retrieve-checkboxes-selected/#findComment-158363 Share on other sites More sharing options...
HuggieBear Posted January 11, 2007 Share Posted January 11, 2007 Try this:$pack = "SELECT * FROM `packages` WHERE `package_id` IN (". $tour['packages'] .")";And also, echo $tour['packages'] to a browser first of all to make sure that it's a comma separated list.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/33665-retrieve-checkboxes-selected/#findComment-158367 Share on other sites More sharing options...
Julian Posted January 11, 2007 Author Share Posted January 11, 2007 Thanks Huggie!I echo'ed the $tour['id_packages'] got 1,3I think this is working good. But I think that's is not what I'm looking for.Table Packages something like this:id_packages | name 1 | Beach Vacations 2 | Mountain 3 | CoastsTable Tours something like this:id_tour | id_packages | tour 1 | 1,3 | SurfingI inserted the array in the Table Tours, so good so far.I want to retrieve this array as checked boxes depends on the choice I made earlier. Thanks guys, you're the best.... Quote Link to comment https://forums.phpfreaks.com/topic/33665-retrieve-checkboxes-selected/#findComment-158384 Share on other sites More sharing options...
Julian Posted January 11, 2007 Author Share Posted January 11, 2007 Any help guys? Will be very appreciatted! Quote Link to comment https://forums.phpfreaks.com/topic/33665-retrieve-checkboxes-selected/#findComment-158536 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.