Best way to store this data with multiple attributes

Discussion in 'PHP' started by mrspeed, Aug 16, 2007.

  1. #1
    I'm working on an app and lets' say it's a database of cars.
    For each car I want to list the engines that came with it.

    I am thinking of creating two columns in a mysql table.
    The first column will be the car and the second column will be called engines.

    Because there could be one or more engines I was thinking of inserting the different sizes of engines delimited by a Pipe(|)
    2.5liter | 3.1liter | 3.9 liter

    When I display the car and engines I just will have to split on the | to read the engine sizes.

    Is this the normal way to handle when items have a variable amount of attributes ?
     
    mrspeed, Aug 16, 2007 IP
  2. jazz7620

    jazz7620 Banned

    Messages:
    357
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can do it this way but your database will not be normalized.

    What I would do?

    Create a table Cars
    id, car_name , model and other attributes etc

    Create Engine Table
    id, engine_desc and other attributes etc.

    Create Car-Engine table
    car_id, engine_id (from Car and Engine tables)

    Better way of doing relational db programming.
     
    jazz7620, Aug 16, 2007 IP
  3. ehlokoen

    ehlokoen Peon

    Messages:
    161
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If you use the explode("|", $datafromsql); function, you are doing it alright. It will create an array for you, and you can then easily read the engines. I use it often, the explode function.

    EDIT: you could take jazz's idea too
     
    ehlokoen, Aug 16, 2007 IP
  4. jazz7620

    jazz7620 Banned

    Messages:
    357
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This will help you in search pages, for example, user wants to search for all the cars with 2.3 engine..!!
    You can create many pages which will be good for SEO also (given that you are creating a website out of it...)
     
    jazz7620, Aug 16, 2007 IP
  5. MrX

    MrX Well-Known Member

    Messages:
    1,563
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    140
    #5
    Listen to jazz...that's the correct way of doing databases.
     
    MrX, Aug 16, 2007 IP
  6. mrspeed

    mrspeed Guest

    Messages:
    193
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yep Jazz Wins.

    Thanks !
     
    mrspeed, Aug 16, 2007 IP