Maverickb7 Posted April 16, 2007 Share Posted April 16, 2007 alright.. you've problem seen me around here asking for help before as this is the place I always run to for help. I've been spending alot of time trying to learn PHP as I'm trying to develop my own personal CMS. I have a lot complete but I'm still having some trouble coming up with ideas on storing certain types of data. Let me give you an example. Let's say I wanted to run a video game website. The CMS would grab a list of genre's from a database and display them as checkboxes within a page on the site (where admins add games). Now some games are going to belong to more then one genre (ex. action/adventure) and i'm wondering how easy it would be to have a group of check box data added to one field within a table row. Perhaps each one seperated by a comma. Now, I don't think I'd have much trouble making that happen. What i'm wondering is how I would use that information on the page that edits the game details. For example, I put a game down as action, adventure and then add it to the database. It would then be added as "Action, Adventure" without the qoutes. Now when I pull that info on a page created for editing how would i check off the check boxes that have been selected. I've been dealing with code for hours and my mind is kinda fried so any help would be appreciated. OH and if anyone has an easier way to complete what I'm doing please point me in the right direction. Thanks in advance guys!! hope you can help. Sorry bout grammar and spelling. It's late here. Link to comment https://forums.phpfreaks.com/topic/47207-checkboxes/ Share on other sites More sharing options...
xenophobia Posted April 16, 2007 Share Posted April 16, 2007 I believe that you should have a table to store all the genre? Let's call it a 'genre' with below fields: - genre_id - genre_name From what you said, you probably have a table to store your games. Let's call it a 'games' with below fields: - game_id - game_name - game_genre - .... more.... For the game_genre, you used separator ',' to indicate the genre. E.g.: 1,5. (1-for action; 5-adventure) So you will split these string into an array, since you mentioned that you don't have any trouble in that, so I won't discuss it. So now you going to parse all the genre's checkbox with appropriated game's genre. So what you can do is loop through all the data in 'genre' database, print out all the checkbox with a value of the 'genre_id' in these checkbox's name attribute: <input type="checkbox" name="check_<?php echo $genre_id; ?>" value="<?php echo $genre_name; ?>" /> So the checkbox with a name="check_5" will indicated the adventure checkbox. Within the loop, put a if-else statement to match with the array you had split from the fields 'game_genre'. If it does match with the array, add a attribute (checked) to the HTML checkbox element: for(//loop for the genre){ $checkflag = ""; foreach($genrearray as $arry){ if($arry == $genre_id){ $checkflag = "checked"; break; //break the loop } } echo "<input type=\"checkbox\" name=\"check_$genre_id\" value=\"$genre_name;\" $checkflag />"; } So when user clicked on the post button, let's say it will post to a receive.php with "POST" method. <?PHP $genre = ""; //used to store the string. eg: "1,5" for(//loop your genre table to retrieve all the genre_id){ $genre_id = //the genre's id if(isset($_POST["check_" . $genre_id])){ //it means that user had checked the check box. Do add to your string with a comma ','. } } ?> There might be quite messy of my explanation. PM or reply on this topic, I will try to explain to you further. Hope this helps you. Link to comment https://forums.phpfreaks.com/topic/47207-checkboxes/#findComment-230239 Share on other sites More sharing options...
Maverickb7 Posted April 17, 2007 Author Share Posted April 17, 2007 this is how i'm adding the genre numbers as one field in the database. I know there is an easy way.. but i'm still learning so I haven't found it. if ($_POST['check_1'] == 1) { $check1 = $_POST['check_1'].","; } if ($_POST['check_2'] == 2) { $check2 = $_POST['check_2'].","; } if ($_POST['check_3'] == 3) { $check3 = $_POST['check_3'].","; } if ($_POST['check_4'] == 4) { $check4 = $_POST['check_4'].","; } if ($_POST['check_5'] == 5) { $check5 = $_POST['check_5'].","; } if ($_POST['check_6'] == 6) { $check6 = $_POST['check_6'].","; } if ($_POST['check_7'] == 7) { $check7 = $_POST['check_7'].","; } if ($_POST['check_8'] == { $check8 = $_POST['check_8'].","; } if ($_POST['check_9'] == 9) { $check9 = $_POST['check_9'].","; } if ($_POST['check_10'] == 10) { $check10 = $_POST['check_10'].","; } if ($_POST['check_11'] == 11) { $check11 = $_POST['check_11'].","; } if ($_POST['check_12'] == 12) { $check12 = $_POST['check_12'].","; } if ($_POST['check_13'] == 13) { $check13 = $_POST['check_13'].","; } $genre = $check1.$check2.$check3.$check4.$check5.$check6.$check7.$check8.$check9.$check10.$check11.$check12.$check13; Link to comment https://forums.phpfreaks.com/topic/47207-checkboxes/#findComment-230912 Share on other sites More sharing options...
kenrbnsn Posted April 17, 2007 Share Posted April 17, 2007 An easier way would be to set up your checkbox as an array: <?php echo '<input type="checkbox" name="check[' . $genre_id .']" value="' . $genre_name . '" ' . $checkflag . ' />'; ?> Then your other code could be something like this: <?php $tmp = array(); for ($i=1;$i<14;$i++) if ($_POST['check'][$i] == $i) $tmp[] = $_POST['check'][$i]; $genre = implode(',',$tmp); ?> Ken Link to comment https://forums.phpfreaks.com/topic/47207-checkboxes/#findComment-230922 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.