best way to approach this? - php+mysql

Discussion in 'MySQL' started by roseplant, Mar 16, 2006.

  1. #1
    I don't have much experience with mysql so this one has me stumped. I'll try to describe the situation.

    I have a database of (call me a traditionalist:p ) Widgets.

    Table : widgets.
    Columns: widget_id,widget_name
    Sample data:
    1,SnazzyWidget
    2,SleepyWidget

    Table: widget_attributes
    Columns: attribute_id, attribute_name
    Sample data:
    1, Can fly
    2, Can talk
    3, Multi-coloured
    4, Executive class

    Table: attribute_map
    Columns: widget_id, attribute_jd
    Sample data:
    1,1
    1,4
    1,3
    2,3


    I hope you get the picture. attribute_map maps the two other tables together. A widget can have more than one attribute, as in the sample SnazzyWidget can fly, is multi-coloured and is Executive class.

    So in the widget product page I want to print a list of all the attributes the particular widget has according to the database. I can do this (yeah!:) ). So the next step is in the Admin pages, I want to print a list of checkboxes and the box is checked if the widget has that particular attribute.

    EG. for Snazzywidget the output would be:
    Can fly | CHECKED
    Can talk | NOT CHECKED
    Multi-coloured | CHECKED
    Executive class | CHECKED

    The user can then check or uncheck the boxes as needed and resubmit the form, updating the database.

    Could someone tell me how I can code this (PHP + mysql)?
     
    roseplant, Mar 16, 2006 IP
  2. sketch

    sketch Well-Known Member

    Messages:
    898
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    148
    #2
    Could you show us the code you're already using to show the attributes? If it's a function, it will be possible to add just a few lines of code and the function will serve both purposes!
     
    sketch, Mar 17, 2006 IP
  3. roseplant

    roseplant Peon

    Messages:
    253
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks sketch but I just figured out how to do it. I used a LEFT JOIN to join the attribute map, and selected map_attribute_id, attribute_name and attribute_id. So then the map_attribute_id is null when no mapped value exists for it.

    Then all I had to do was put in a statement like

    
    if(!map_attribute_id == null) 
    print('<input type=checkbox selected="selected" name=$attribute_id value=$attribute_name>')
    
    Code (markup):
    And it prints out the checkboxes with the appropriate values (checked/unchecked).
     
    roseplant, Mar 17, 2006 IP