icon for lab

CT310

Recitation 15 - SQL with... Fuel!

In this recitation, we will:


Before being able to use the DB class, we need to modify a specific configuration file.

The config file is located in /fuel/app/config/development/db.php Change the following

            
return array( 'default' => array( 'connection' => array( 'dsn' => 'mysql:host=faure.cs.colostate.edu;dbname={ EID }', 'username' => '{ EID }', 'password' => '{CSU ID}', ), ), );

If you can successfully access the MySQL server with these credentials, Fuel shouldn't have an issue doing the same.

In a previous lab we worked with the idea of a todo application. This application used a file in the model. Our new updated version will use a database.

The fuel database docs will be really helpful for learning how to set up fuel queries. You can find them here

  1. Set up your database
    • Update your database configuration in fuel/app/config/development/db.php
    • Add your username, password, host and database name
    • Log into your mysql database using ssh and create a table for your todo app
    • CREATE table todos(id int, description varchar(256), completed boolean, PRIMARY KEY(id))
  2. Download the files from here and place them in correct location like previous labs
  3. Look at the todomaincontent.php under todoviews.
    • Notice that the first php block is a loop that prints out some check boxes and labels
    • For each todo this loop creates a form input check box and a label that is the description of the todo
    • Notice that each check box also has the property name = 'todocheck[]'
    • Ending a name with [] allows us to access it as a group of items in an array later on.
    • Notice that the submit button "Cross of Todos" has the property " 'name' = delete"
    • Also notice the second form for creating a todo, with an input button the has the property " 'name' = add"
    • The differences in name properties between these two submit buttons is critical
  4. Look at the tododb.php under controllers.
    • Notice in the action_index function that we make calls to the model for getting the todos and getting the count of todos
    • Next inspect the post_index function and notice that first thing is an if statement checking if $_POST['add'] is set and if there is todo_text
    • What this does is check that the add submit button was clicked and not the cross off todos
    • The next if statement does a similiar thing but to see if todos were crossed off the list.
    • The for each loop iterates through all the checkboxes to see which ones are checked and gets those ids.
    • It is able to do this since we used that naming convention where the name ends with a [].
    • Then this block of called calls the model to delete todos, passing it an array of ids.
  5. Finally for your assignment you must complete the skeleton code that is in tododbmodel so that the delete and add behavior work properly.
    Read the docs referenced above for more info on how to use the fuel class DB. You can see the working example here.