Checkboxes

Hello guys.

I’m currently in the planning part of my newest website, and i’m planning to use checkboxes. The point of the website is it will offer some data on books.

When creating a book on the website, the user will have to select which genres the book is (it is possible to select multiple genres) through checkboxes.

The problem however is, i’m unsure of how this should be made both on server- and client-side.

My initial thoughts:

  • The database will contain a column for each possible genres (boolean values)

  • If the user checks the checkbox, the value will be set to 1, else it will be set to 0

  • In the view page, if the value of the genre is 1, the name of the genre will be displayed, else it will not.

Is this the right method to use, and if so, how do i create the last point? I mean, what should i place in the view, the controller and the model-file (i’ve not understood MCV fully yet, but i’m learning by trying :) )?

Thanks

You will need to read The Guide and understand it, then jump into action…

Your initial thoughts are correct in my opinion though.

Your solution doesn’t offer much flexibility.

When you need to add another genre, you must create a new column in your database and then add another input to your form.

It may be better to use some kind of tagging system. Here is an extension that might be useful:

http://www.yiiframework.com/extension/ptags

It is possible to do it as you are planning to, but for a good design (keyword: normalisation of a database) use three tables - one with your books, one with all possible genres and one with the bookid’s and the genreid’s. It is a little more difficult in the beginning (also the yii part) but will save you trouble in the end!

books:




bookid|title

     1|War & Peace

     2|1984



genres:




genreid|genre

      1|historic novel

      2|social novel

      3|dystopia

      4|science fiction



book-genres:




bookid|genreid

     1|1

     1|2

     2|2

     2|3

     2|4



Like that you can have a preset amount of genres which is easily extendible and you won’t run into database limits on columns.

Thanks for your answers, it’s very much appreciated

I’ve looked at the tagging extension, and i think I’m gonna stick with the method klammeraffe suggested. That way it’s possible to look up all books with a certain genre, without the chance of missing some because of misspelling or another word of the same genre.