Download presentation
Presentation is loading. Please wait.
Published byGeorge Chapman Modified over 9 years ago
1
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN 6710 - Section A – TR 9:30-10:45 CRN 10570 – Section B – TR 5:30-6:45
2
CodeIgniter Friendly URLs /application/config/config.php set $config[‘base_url’] = “http://subdomain.domain.com”; set $config[‘index_page’] = “”; /web.config Create this file using the content in web.config.txt on D2L /controllers/[each].php On each controller, added a call to the parent constructor public function __construct(){ parent::__construct(); }
3
CodeIgniter Friendly URLs http://subdomain.domain.com/controller/function/ /application/config/routes.php You can change the default controller here, which sets what will load on the base URL when no controller is in the path The index() function will be called by default if no function name is passed to the URL
4
CodeIgniter Database Connection /application/config/database.php ‘dsn’ = ‘mysql:host=[ip];dbname=[db]’ ‘username’ = ‘[user]’ ‘password’ = ‘[pwd]’ ‘dbdriver’ = ‘pdo’ /application/config/autoload.php $autoload[‘libraries’] = array(‘database’, ‘form_validation’); $autoload[‘helper’] = array(‘url’, ‘form’);
5
CodeIgniter Make a new public function in one of your controllers and add the following: $query = $this->db->query(“SELECT * FROM table”); $results = $query->results(); echo “ ”; print_r($results); echo “ ”; Go to the URL that will load this function url/controller/function
6
CodeIgniter Query Builder Library http://www.codeigniter.com/user_guide/database/quer y_builder.html $query = $this->db->get(“table”); same as SELECT * FROM table $query = $this->db->get(“table”, 5); same as SELECT * FROM table LIMIT 5
7
CodeIgniter Query Builder can be used to build your SQL query piece by piece $this->db->select(“field”); $this->db->from(“table”); $this->db->join(“table2”, “table1.field1 = table2.field2”); $this->db->where(“field”, $value); $this->db->order_by(“field”, “DESC”); $this->db->limit(5); $query = $this->db->get(); The above lines can be in any order as long as get() is called last
8
CodeIgniter We can also use the Query Builder to create insert/update/delete commands $this->db->set(“field”, $val); $this->db->insert(“table”); $this->db->insert_id(); $this->db->update(“table”); $this->db->affected_rows(); $this->db->delete(“table”);
9
CodeIgniter We have not been following the MVC structure while testing the database functionality Remember, our data should come from our Model So, lets plan out a small system and set up the correct MVC structure for the system Views: Form for collecting user data Page for displaying report about user data Model: Single model to hold methods for both
10
CodeIgniter Set up the template I will use our layout example as a starting point Create an assets folder with a css and js folder inside of it <link rel="stylesheet" type="text/css" href=" assets/css/styles.css“ /> assets/js/scripts.js">
11
CodeIgniter Create a new folder: /application/views/template Add a footer/header/menu page to this folder Load these views into your template $this->load->view(‘template/header’); Now we have a basic page layout set up
12
CodeIgniter Now we need two views, one for the form, and one for the report For now, I’ll just set up the form view and we will come back to the report We can go ahead and set up a function for the report view and add both links to our menu echo site_url(“controller/function”); For the form view, I am going to use some CI form helper functions
13
CodeIgniter The Form Helper has several functions to make building forms easier http://www.codeigniter.com/user_guide/helpers/form_ helper.html echo form_open(); echo form_open_multipart();//for file uploads echo form_input($attributeArray);//text field echo form_textarea($attributeArray);//text area echo form_dropdown($name, $options, $selected); echo form_multiselect($name, $options, $selectedArr); echo form_submit($attributeArray); echo form_close();
14
CodeIgniter Now that the form is set up, we can set up the controller The first thing we need to do is validate the data we received from the form We are going to use the Form Validation Library http://www.codeigniter.com/user_guide/libraries/form_ validation.html
15
CodeIgniter $this->form_validation->set_rules(“fieldName”, “Display Name”, “validation rules”); $this->form_validation->set_rules(“first”, “First Name”, “required”); $validate = $this->form_validation->run(); if($validate) //all fields completed Lots of rule choices and the ability to make custom rules
16
CodeIgniter Now that our form is ready, the fields are validated, we are ready to insert data into our form Time to create a model, and set up some functions class Test_Model extends CI_Model { function __construct(){ parent::__construct(); } } $this->load->model(‘test_model’); $this->test_model->function();
17
CodeIgniter Now we can set up some basic queries as model functions and call those in the report function of the controller Then we can set up the view to display all of the content
18
Reminders Lab 4 – MySQL – Due Oct 1 st Project 1 – Responsive Site – Due Oct 8 th I will assign Project 2 next week with specifications of what you will need to include in your CI project that will be due on Nov 5 th The Midterm project will be an in-class assignment
19
Next Time MVC/CodeIgniter More with Views/Controllers Database connection Set up our first model Next Week More with models Helpers/Libraries More built in CI libraries to make common tasks easier
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.