mysql php drop down

Discussion in 'PHP' started by xbat, Jun 26, 2012.

  1. #1
    I'm looking to build a mysql php drop down... I have seen things like ajax and what not.. but I'm not sure what the best way to go about this is.

    Pretty much I have Make > Models for cars, like ebay or auto trader

    Make

    Models

    Make will match up with with whats in the model table

    make is listed in the same row as the model...

    So when you select ford

    Ford : f150 - will show
    ford : mustang will show

    etc...
     
    xbat, Jun 26, 2012 IP
  2. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #2
    What Cars Makes/Models database are you using?
     
    NetStar, Jun 26, 2012 IP
  3. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #3
    every kind you can think of..
     
    xbat, Jun 27, 2012 IP
  4. akhileshbc

    akhileshbc Active Member

    Messages:
    98
    Likes Received:
    1
    Best Answers:
    5
    Trophy Points:
    75
    #4
    Store the Make and Models in separate tables(in db).

    A demo schema would look like:
    
    tblMake
    --------
    id
    make_name
    
    
    tblModel
    --------
    id
    model_name
    make_id (primary key of tblMake)
    
    Code (markup):
    In this way, the db would be in normalized form.

    Now for populating the "Make" combobox, just query the tblMake for all records. Then echo it enclosing the <option> tag inside a <select>, which will create the comboboxes.

    And then, using javascript, add a listener for the change event of the combobox(Make's). So that, whenever user changes the selection, it should do an ajax query to a php file with that particular make_id and this php script would return all the models that matches this make_id, from db. So, make use of this resulting list to dynamically populate the Model's combobox(using javascript).

    I hope you got the idea. :)
     
    akhileshbc, Jun 27, 2012 IP
  5. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #5
    I don't think you understood my question. I'm looking for the format of the Tables and Columns so I can give you an answer to your question. Without knowing what your database structure is, we would have to make a hundred and one assumptions.
     
    NetStar, Jun 27, 2012 IP
  6. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #6
    oh sorry..

    i have table1 with make

    id - make

    then table 2 with model

    id make model
     
    xbat, Jun 27, 2012 IP
  7. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #7

    I have the idea.. its more along the idea of getting the drop downs to work correctly...

    make> then Model >
     
    xbat, Jun 27, 2012 IP
  8. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #8
    That's still not clear. But let's see...

    Most Make/Model databases have a single Table holding all the relative info (Id, Make, Model, Trim, ..., etc). I'm going to assume your database has separate tables holding the data for Models and Makes with using the "id" as the key between the two.

    *IF* my assumption is correct you would run 2 queries.

    Query 1: SELECT id FROM Makes WHERE make = "Ford";
    Query 2: SELECT model FROM Models WHERE id = "The_Id_You_Just_Fetched_Here";

    -OR you can wrap it up with a single query using a JOIN... In this case there really isn't much of a performance difference.


    Edit: Just re-read your original post and saw you were asking how to load the data in to the dropdowns. You probably already have the SQL code so just ignore my above post.
     
    NetStar, Jun 27, 2012 IP
  9. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #9
    netstar what is not clear?

    ----------------------------

    I can get both to load right now.. but all the chevy models show up under the ford models... I just got to the point now where I think im going to need some type of javascript, or ajax.. and now thats the part i am stuck on.
     
    xbat, Jun 28, 2012 IP
  10. akhileshbc

    akhileshbc Active Member

    Messages:
    98
    Likes Received:
    1
    Best Answers:
    5
    Trophy Points:
    75
    #10
    So, did you got it implemented or waiting for more ideas ?
     
    akhileshbc, Jun 28, 2012 IP
  11. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #11
    nope no more ideas... I have done it before.. but I just have been so rusty... The Java or ajax part is what I am having a problem with i think... I mean its the only way to make something like that work correct?
     
    xbat, Jun 28, 2012 IP
  12. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #12
    something like this - http://www.usedcars.com/ where the one section is blurred out untill you select the make. but with just make model.
     
    xbat, Jun 28, 2012 IP
  13. akhileshbc

    akhileshbc Active Member

    Messages:
    98
    Likes Received:
    1
    Best Answers:
    5
    Trophy Points:
    75
    #13
    I think, ajax would be the easy way. If you could use jQuery(which is a pretty easy JavaScript), there is get(), post(), ajax() methods. You could use one of them.

    Another way would be to store all makes and models in JavaScript arrays(just echo it in PHP) and make use of the data from these to dynamically populate them at client side. The advantage is, this doesn't need the server's help to get the list of models specific to each make. Because we are not contacting the server and making it execute a query to fetch all models of a particular make and using it(via ajax). But I think, if you have a large of data in your db tables, then this might not be a good approach.
     
    akhileshbc, Jun 28, 2012 IP
  14. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #14
    I will have to brush up on the java and ajax i guess.. the model table has to pull from the database no matter what.. they want they want to add models without doing any code editing.. thank you though
     
    xbat, Jun 28, 2012 IP
  15. burlesk

    burlesk Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Just use an event handler, that will be triggered when the "make" select changes it's value. The event handler should then pull of the corresponding models from the server with a simple GET ajax request and place those values in the "model" select. That's it.
     
    burlesk, Jun 28, 2012 IP
  16. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #16

    Thank you... Is this stuff hard? do you have any suggestions for a newbie like me?
     
    xbat, Jun 28, 2012 IP