kleptik Posted December 8, 2006 Share Posted December 8, 2006 I am quite new to PHP and I appreciate any help here.I have searched the web, and this forum and am becoming overwhelmed with different approaches to this. :)Basically I'm creating an input form that will submit to a database. I need to then display that data in a table.I'm creating a page where a client can input information about a vehicle he's selling. The webend users will then see a formatted page with the information used to build it from the DB.I know how to do the textfields etc.. But the check boxes have me a bit lost.There will be maby 40-50 check boxes.An admin side example for creating the vehicle listing would include:Vehicle Options:Power Steering [ ]Heated Seats [ ]Power Windows [ ]etc..etc..When the web user views the page with all the vehicle listings I want them to see this information formatted like:Vehicle Options:-Power Steering -Heated Seats -Power WindowsShould I create a field in the DB for each option?One approach I heard was to have all the options in the DB as booleans. If the field has a true, then display it.. I donno.. I'm confused. ???Thanks for anyhelp, and I apologize for any vagueness.Edit:I can post some code from one approach I had. This takes all the options and adds them to one field for the record comma seperated.I got thinking on this and it might be difficult to read this field and create the userend table using this data. I also have an "Edit" php page where the admin can edit the listing, Not sure if using this method would create way more work then it should.[code] Please choose Vehicle Options:<br /> Power Windows:<input type="checkbox" value="Power Windows" name="vehcOptions[]"><br /> Heated Seats:<input type="checkbox" value="Heated Seats" name="vehcOptions[]"><br /> Power Steering:<input type="checkbox" value="Power Steering" name="vehcOptions[]"><br /><br>[/code]----- on submit[code] foreach ($vehcOptions as $f) { $queryvehcOptions = $queryvehcOptions.$f.","; }//The home type properties in this query is just from the original code I took this from... $query = "INSERT INTO ListingsTable (VirtualTourId,ListingTitle,Description,Image,Price,MLSNumber,Location,Bedrooms,Bathrooms,SqFeet,Sold,vehcOptions) VALUES ('".$VirtualTourId."','".$ListingTitle."','".$Description."','".$Image."','".$Price."','".$MLSNumber."','".$Location."','".$Bedrooms."','".$Bathrooms."','".$SqFeet."','".$Sold."','".$queryvehcOptions."')";[/code] Link to comment https://forums.phpfreaks.com/topic/29884-check-boxes-and-db-help/ Share on other sites More sharing options...
Mr_Pancakes Posted December 8, 2006 Share Posted December 8, 2006 [u]there are two ways to approach your problem:[/u]one somewhat troublesome solution you mentioned is to have a seperate boolean field (true or false) for each of your vehicle's features (like Power Steering, Heated Seats, etc.). so your table would look something like this:TABLE: [b]vehicles[/b][table][tr][td][u]vehicle_id |[/u][/td][td][u] power_steering |[/u][/td][td][u] heated_seats |[/u][/td][td][u] ...etc.[/u][/td][/tr][tr][td]1[/td][td]1[/td][td]1[/td][td]...[/td][/tr][tr][td]2[/td][td]1[/td][td]0[/td][td]...[/td][/tr][tr][td]3[/td][td]0[/td][td]0[/td][td]...[/td][/tr][tr][td]...[/td][td]...[/td][td]...[/td][td]...[/td][/tr][/table]whereas the "1" stands for "true" and "0" for "false", in each of the vehicle feature columns (or typically called "fields" instead of "columns"). the major problem with this solution is that it takes up WAY too much room in your database. think about it... if you have say 50 possible vehicle features, and car "x" only offers 2 of those features, you've wasted database space by storing 48 "falses". [i]you should only store features of a car that describe what it actualy is, and never what the car is not.[/i]a better approach that saves database space and improves efficiency is to set each of your checkboxes to a value of a "feature_id" number. then when a user submits the page, concatenate all values (feature_ids) of your checked boxes into a single comma seperated field and store in your database. for example, your table structure will look like this:TABLE: [b]vehicles[/b][table][tr][td][u]vehicle_id |[/u][/td][td][u]feature_ids[/u][/td][/tr][tr][td]1[/td][td]2,4,5,10,12[/td][/tr][tr][td]2[/td][td]1,2,3[/td][/tr][tr][td]3[/td][td]4,44,400[/td][/tr][tr][td]...[/td][td]...[/td][/tr][/table]TABLE: [b]features[/b][table][tr][td][u]feature_id |[/u][/td][td][u]feature_name[/u][/td][/tr][tr][td]1[/td][td]Power Steering[/td][/tr][tr][td]2[/td][td]Heated Seats[/td][/tr][tr][td]3[/td][td]Kegerator in Trunk[/td][/tr][tr][td]...[/td][td]...[/td][/tr][/table]each vehicle_id in the "vehicles" table describes what the id number stands for in the "features" table. then to display this info:[list][*]1.) query the table "vehicles" for a target "vehicle_id" and it's "feature_ids"[*]2.) query the table "features" for all "feature_id"s and "feature_name"s[*]3.) use the php function explode() to parse out each target feature_id from step 1 into an array[*]4.) use a foreach() loop to step through each feature_id in your exploded array from step 3[*]5.) in this loop, match your target feature_id (step 3) with it's parent feature_id stored in step 2[*]6.) display the corresponding feature_name to the page in your desired format[/list]your welcome.i have random sympathy for beginners b/c ive been there.cheers,steve Link to comment https://forums.phpfreaks.com/topic/29884-check-boxes-and-db-help/#findComment-137331 Share on other sites More sharing options...
kleptik Posted December 9, 2006 Author Share Posted December 9, 2006 Thanks for the reply. This makes allot of sense and I'm still craking away on it. I'll let ya know how it comes out. :D Link to comment https://forums.phpfreaks.com/topic/29884-check-boxes-and-db-help/#findComment-138190 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.