?
Solved

Multiple Options Database Storage PHP/MYSQL

Posted on 2011-09-07
10
Medium Priority
?
322 Views
Last Modified: 2012-08-14
Good Morning Experts,
I am currently working on a redesign of our website from when I put it together 3 years ago.
So a bit of history:
I put the website together in the matter of about 3 weeks in a rush.
To store the options into the database as quick as possible I used the php explode and implode commands to store the database.
All of the information is stored into a single field in the database.
It makes updating / changing vehicle options terribly inefficient.
It is also not good database practice to store dynamic information into a flat file.

Thoughts on a possible solution:

There should be an options list table in the database.  This table would contain the fields similar to the following:
opt_id – unique identified for each option
opt_name – the name of the option
opt_type – separate options that are available only for trucks/suvs/vans etc.
opt_group – group based on interior/exterior/electronics/etc
opt_order – the order you want them to show up in the list
Then from that list create the list of available options on the vehicle input screen.
Then when the vehicle information is submit the options will be stored to a separate options table where the options specific to the vehicle are stored.
This is where I am coming into the problem on how to store the data, I believe something like the following should work.
vehicle_id – the id of the vehicle
option_id – the option / this is where I have the issue of whether to store the name or the otpion
option_value – 0 = no 1 = yes

So any input would be greatly appreciated.
0
Comment
Question by:shdwmage
  • 6
  • 4
10 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36497533
If I get you correctly, the vehicle_id might be a VIN number.  That describes a specific vehicle.  Each option might be linked to the VIN number via a key to the option.  The option row can have all kinds of interesting information, but you would only need one key for each particular option.

You can tie these tables together with a "junction table" that has only two columns - one for the VIN and one for the option.  This is a many_to-many table that establishes the relationship between the two tables.  With a middle table like this you can answer questions like, "What options are most popular in my fleet?" and "What options does this vehicle have?"
0
 
LVL 2

Author Comment

by:shdwmage
ID: 36497689
Yes the vehicle id is the VIN.  But I label it in the database as vehicle ID because I have to display the word vin in to many places.

The point of the options list isn't so much to be able to search them or use them for anything but displaying to the customer.  It's more to allow for easier changing of options in the case that one of my employees puts something in incorrectly.

My thought process was to set it up so when I do the join and bring the data to the live page it'd be "WHERE OPTION_VALUE = 1" and only selecting the active values.

The problem I was running to in my mind is that if I store the value of the option versus the name of the option it'll either make for a lot more complicated SQL statement, or a whole heck of a lot of checks in the php end.

I only consider myself intermediate at PHP programming at best.  My background in programming is more from FileMaker, VB, Access.  

I don't have a lot of SQL experience, I understand the point of relational databases, but each program is different enough on which is the best way to do it.

What I know is adding more options to a list is a lot easier when the list is stored into a table, and then displayed from the values of that table.

I also know the value of removing as much redundant data as possible.
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1500 total points
ID: 36498253
With a programming background of any kind, this book will be a breeze for you and it will get you up to speed very quickly on the PHP and MySQL part of things.  Very readable with excellent code examples.
http://www.sitepoint.com/books/phpmysql4/

The process of removing redundant data is called "normalization."  This link has some interesting, if slightly philosophical, results.
http://lmgtfy.com/?q=Should+I+Normalize+My+Database

I would store each option in its own row, something like this:

opt_id INT NOT NULL PRIMARY KEY
opt_name VARCHAR(64)

And I would store each vehicle in its own row, something like this:

veh_id INT NOT NULL PRIMARY KEY
veh_name VARCHAR(64)

The junction table would look something like this:

opt_id INT NOT NULL
veh_id INT NOT NULL

Does this make sense to you?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Author Comment

by:shdwmage
ID: 36498756
Ray,
That was the first php/mysql book I read several years ago.  As a matter of fact my current website is built off of the knowledge I got from this book.

I understand the point of the junction table, but I'm not sure that is the best solution here. I feel like it would over complicate code.

Why should I use a junction table in this case?

Also note I will be linking images to the car in a one to many realationship.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1500 total points
ID: 36499132
I think the junction table would actually simplify maintenance without complicating the code.  I might also consider a junction table for the images, however if there is only one set of images per vehicle, you could carry the veh_id in the image row and it would be easy to select the images associated with any given vehicle.
0
 
LVL 2

Author Comment

by:shdwmage
ID: 36504197
Ray, what would the code to retrieve that information look like?  Just rough, doesn't have to be super specific.
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1500 total points
ID: 36506034
Probably something like this to pick vehicle number 237 (refer to ID:36498253):

$sql = "SELECT veh_id, veh_name, opt_id, opt_name FROM veh_table, opt_table, junction_table WHERE veh_id = opt_id AND veh_id = 237";
0
 
LVL 2

Author Comment

by:shdwmage
ID: 36512124
I won't be able to try this until Monday.  I'll take a look and get back with you.
0
 
LVL 2

Author Comment

by:shdwmage
ID: 36550799
Ya Ray that worked, I probably won't use it, but it worked.
0
 
LVL 2

Author Closing Comment

by:shdwmage
ID: 36550809
It's not exactly what I was looking for, but its the best alternative to what I was going to do.  Thanks again Ray.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month15 days, 5 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question