반응형

* config 에 설정 된 값을 가져올 때.

   - echo $this->config->item('base_url');

   - 존재하지 않는 설정값을 가져오려고 하면 FALSE가 return 됨.


* 새로운 config 파일을 만들 때.

 - config/development/goodgods.php

$config['is_dev'] = true; 


 - config/production/goodgods.php

$config['is_dev'] = false; 


 - config 파일 로드

$this->load->config('goodgods');  //goodgods.php 파일을 로드함.


* 개발 환경일 때만 출력

 ex. 특정값이 있을 때 개발/수정이 편하다고 할 때 그 값을 항상 보여주고 있으면 좋겠지?

     그러나 그 값은 서비스 환경에서 보여지면 안돼! 보기도 안좋고, 중요값이 있으면 클나잖어..

<? if($this->config->item('is_dev')) { ?>

    <div class="well"> This is DEV page!</div> //well은 bootstrap 라이브러리에서 제공되는 클래스

<? } ?>


* 의문이 드는 것이 개발환경과 서비스 환경을 구분 짓는다고 하여도 소스가 변경되면 같이 변경될 것 같은데..

  그런 의미에서 구분 짓는 것은 아니겄지!

반응형

'Programming > CodeIgniter' 카테고리의 다른 글

index.php  (0) 2015.09.08
/application/config 파일  (0) 2015.09.08
Model(5/5) - insert  (0) 2015.09.08
Library & form_validation, set_rules  (0) 2015.09.08
helper  (0) 2015.09.07
반응형

* localhost/index.php

define('ENVIRONMENT','development');


if(define('ENVIRONMENT'))

{

    switch (ENVIROMENT)

    {

        case 'development':

                  error_reporting(E_ALL);

        break;


        case 'testing':

        case 'production':

                  error_reporting(0);

        break;


       default:

               exit('The application environment is not set correctly.');

    }

} 


* define('ENVIRONMENT','development' | 'testing' | 'production'); 개발환경/테스트/서비스 환경 선택


* application/config에 development와 production 폴더를 각각 만들고,

  해당 환경설정 파일들을 각기 다르게 설정하여 각각 폴더에 넣어주면 위 환경변수에 따라서 다르게 작동한다.


* index.php를 dev.php로 복사하여 파일을 2개로 관리를 하면, 개발환경과 서비스 환경으로 각각 따로 작동을

  하기 때문에 개발을 하는데 있어서 약간 수월 해 질 수 있다.

   - localhost/index.php/topic : 서비스 환경, localhost/dev.php/topic : 개발 환경

반응형

'Programming > CodeIgniter' 카테고리의 다른 글

config 파일 생성  (0) 2015.09.09
/application/config 파일  (0) 2015.09.08
Model(5/5) - insert  (0) 2015.09.08
Library & form_validation, set_rules  (0) 2015.09.08
helper  (0) 2015.09.07
반응형

1. config.php 

 - 기본설정파일

 - https://opentutorials.org/course/697/3834(config.php 파일 해설 부분 또는 설정4 동영상 참고.)


2. database.php 

 - 데이터베이스 설정 파일

 - 유츌되지 않도록 철저한 보안을 유지해야 함.


3. autoload.php

 - 라이브러리,helper, 데이터베이스등을 항상 선언되도록.

 ex) $autoload['helper']=array('url')

      $autoload['libraries']=array('database')


4. hooks.php

 - system 폴더에 영향을 줘야할 때 사용.

 - system 폴더 : CI 코드, application 폴더 : 사용자가 정의한 코드


 - /system에 있는 소스들을 수정하는 것은 위험.(권장하지 않음)

 - 버전업을 했을 경우 데이터가 덮어씌어져 기존 코드와 충돌이 날 수도 있으며,

   복원하기도 힘들어지는 경우가 생긴다.

 - hooks.php를 통해서 수정이 가능함.


5. routes.php

 - uri path에 따라서 규칙대로 CI가 작동하는데, uri의 규칙을 다른게 변하고 싶을 때.

 ex) $route['topic/(:num)']="topic/get/$1";

      $route['default_controller']="login";

      $route['404_override']="errors/error404";

반응형

'Programming > CodeIgniter' 카테고리의 다른 글

config 파일 생성  (0) 2015.09.09
index.php  (0) 2015.09.08
Model(5/5) - insert  (0) 2015.09.08
Library & form_validation, set_rules  (0) 2015.09.08
helper  (0) 2015.09.07
반응형

* 입력된 폼 값을 DB에 insert 하기


- controllers/topic.php

function __construct() {

    parent::__construct();

    $this->load->database();

    $this->load->model('topic_model');

}


function add() {

    $this->head();


    $this->load->library('form_validation');  //form_validation 라이브러리를 사용하겠음.


    //title 과 content는 반드시 입력해라.

    $this->form_validation->set_rules('title','Errors_title','required');

    $this->form_validation->set_rules('content','Errors_content','required');


    if($this->form_validation->run() == FALSE){

        $this->load->view('add');  //입력 값이 FALSE이면 다시 add 페이지를 뿌려줘라.

    } else {

//       $this->load->model('add_model');  => 앞서 공통적으로 선언을 했기 때문에 생략


//인자값을 가져와 add 메소드를 사용하자

       $this->add_model->add($this->input->post('title'), $this->input->post('content'));


        echo "Perfect!";  //TRUE 이면 Perfect라는 문구를 출력하라.

    }


    $this->load->view('footer');


- models/topic_model.php

public function add($title,$content) {

    //insert array 문에 함수를 그냥 사용하게 되면 문자로 인식하여 입력이 되지 않아, 아래처럼 해야함.

    $this->db->set('date','NOW()',false);  //date 폴더에 now() 함수를 쓰고, 이것은 문자가아니가(false)

    return $this->db->insert('tablename',array('title'=>$title,'content'=>$content));


//    $this->db->last_query();  => 실행된 쿼리를 확인.

}


반응형

'Programming > CodeIgniter' 카테고리의 다른 글

index.php  (0) 2015.09.08
/application/config 파일  (0) 2015.09.08
Library & form_validation, set_rules  (0) 2015.09.08
helper  (0) 2015.09.07
URI Routing  (0) 2015.09.07
반응형

1. Library

 - 라이브러리마다 사용 법이 조금씩 다름.

 - 대표적인 예제로 form_validation을 사용하는 법을 다룸.


2. form_validation

 (1) form_validation

 - form 안에 입력된 값들이 부정확한 경우 어떻게 할 것인가


 (2) set_rules

 - 어떤 부분을 필히 입력이 되어야 하는지를 체크.

 - set_rules('form 안에 있는 태그들의 name','사용자가 알아볼수 있는 명칭','required');

 - required : 반드시 입력되어야 함.


 (3) validation_errors

 - 입력된 값이 잘못되었을 경우, 어떤 값이 잘못되었는지 에러메세지를 출력해 줌.


3. 예제

 - topic.php에 add라는 method를 추가.

 - views에 form으로 구성된 add.php를 생성.


 - views/get.php(add 페이지에 갈 수 있게 버튼을 생성함.)

<div>

    //링크로 되어 있으나, bootstrap 라이브러리에서 제공하는 btn 클래스를 이용하여 버튼처럼 만들어줌.

    <a href="index.php/topic/add" class="btn">ADD</a> //bootstrap button css description

</div>


 - controllers/topic.php

function add() {

    $this->head();


    $this->load->library('form_validation');  //form_validation 라이브러리를 사용하겠음.


    //title 과 content는 반드시 입력해라.

    $this->form_validation->set_rules('title','Errors_title','required');

    $this->form_validation->set_rules('content','Errors_content','required');


    if($this->form_validation->run() == FALSE){

        $this->load->view('add');  //입력 값이 FALSE이면 다시 add 페이지를 뿌려줘라.

    } else {

        echo "Perfect!";  //그렇지 않으면 Perfect라는 문구를 출력하라.

    }


    $this->load->view('footer');

}


 - views/add.php

<h1>This is add page!</h1>

<form method="post" action="index.php/topic/add">

    <?php echo validation_errors(); ?> //어떤 부분에서 에러가 났는지 알려줌.

    title : <input type="text" name="title" />

    content : <textarea name="content">/textarea>

    <input type="submit">

</form>


반응형

'Programming > CodeIgniter' 카테고리의 다른 글

/application/config 파일  (0) 2015.09.08
Model(5/5) - insert  (0) 2015.09.08
helper  (0) 2015.09.07
URI Routing  (0) 2015.09.07
Bootstrap 라이브러리  (0) 2015.09.07
반응형

helper 란?

 - 라이브러리와 비슷한 개념이긴 하나, 라이브러리는 객체기반이며, helper는 함수 기반이다.

 - helper를 선언한 후 다른 추가 작업 필요없이 바로 사용이 가능하다.

 - system/helper/*.php에서 소스를 확인 할 수 있다.


* helper 레퍼런스(http://codeigniter-kr.org/user_guide_2.1.0/)

- 레퍼런스를 참고하여 어떤 helper가 있고, 어떤 함수명을 사용해야 되는지 파악할 수 있다. 


1.전역 helper 사용.

 * application/config/autoload.php

 $autoload['helper'] = array('url','html');


2. 선언 helper

 * controller/topic.php

//파일명은 url_helper.php 이지만, 선언할 때에는 name만 써준다.

//helper(array('url','html')); => 여러가지 helper를 사용할 때.

$this->load->helper('url');

$this->load->view('get',array('topic'=>$topic));


 * views/get.php

<div><?=auto_load($topic->content)?></div>



* helper 만들기.

 - models/topic_model.php

public function get($topic_id){

    $this->db->select('id');

    $this->db->select('name');

    $this->db->select('extract(epoch from date) date');

    return $this->db->get_where('tablename',array('id'=>$topic_id))->row();


 - application/helpers/korean_helper.php

<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');

    if(!function_exists('kdate')){  //다른 함수중에 kdate라는 함수가 있는지를 우선 확인

        function kdate($stamp) {

            return date('o년 n월 j일 G시 i분 s초',$stamp);

        }

    }

?>


 - views/get.php

 <div><?=kdate($topic->date)?></div>


반응형

'Programming > CodeIgniter' 카테고리의 다른 글

Model(5/5) - insert  (0) 2015.09.08
Library & form_validation, set_rules  (0) 2015.09.08
URI Routing  (0) 2015.09.07
Bootstrap 라이브러리  (0) 2015.09.07
Model(4/5) - 각 리스트 정보 페이지마다 전체리스트 생성 구현  (0) 2015.09.07
반응형

* URI Routing : 사용자가 접근한 URI에 따라서 Controller의 메소드를 호출해 주는 기능.

* mapping : CI에서 URI에 따라서 호출되는 규칙,방법.

* config/routes.php는 mapping 방식을 CI 규칙과 다르게 설정하는 파일.(Remapping)


* application/config/routes.php 예제

//URI가 "topic/숫자"가 왔을 경우 내부적으로 topic/get/숫자와 같음.

$route['topic/(:num)'] = "topic/get/$1";


//URI가 "post/숫자"가 왔을 경우 내부적으로 topic/get/숫자와 같음.

$route['post/(:num)'] = "topic/get/$1";


//URI가 "topic/a부터z 중 문자 포함/a부터z중 문자 포함/숫자"일 경우 내부적으로 $1/$2/$3과 같음.

$route['topic/([a-z]+)/([a-z]+)/(\d+)'] = "$1/$2/$3";


//어떤 URI를 입력하지 않고 도메인주소만을 입력했을 login 페이지와 같음

$route['default_controller'] = "login";


//404에러가 발생했을 시 404페이지와 같음.

$route['404_override'] = "errors/error404";  //404 에러가 났을 경우 errors/error404를 호출.


* controller/errors.php

<?

    class Errors extends CI_Controller {

        public function error404() {

            $this->load->view('head');

            $this->load->view('error/404');

            $this->load->view('footer');

        }

    }
?>


* view/error/404.php

<?

    echo "Not Found Page!!!";

?>



* 결론

 1.routes.php에서는 에러 발생 시, 지정한 컨트롤러 파일(controller/errors.php)이 호출되도록 설정을 해줄 수 있음.

 2.errors.php 컨트롤러 파일안에 정의된 error404 메소드가 실행됨.

 3.view에 뿌려주는 값을 출력해줌.(views/error/404.php)

반응형
반응형

* 설치

 1. Bootstrap 다운로드

 2. 보통 css, js, img 폴더는 static/에 생성하며, bootstrap 라이브러리 동일 폴더에 lib라는 폴더를 생성하여, 관리함

     - static/lib/bootstrap

 3. 해당 사이트에 들어가서 다운로드 후 위 경로에 업로드/압축을 풀어준다.

 4. 설치 완료


* bootstrap include

<!DOCTYPE html>

<html>

  <head>

    <title>Bootstrap 101 Template</title>

    <meta name="viewport" content="width=device-width, initial-scale=1.0">  //모바일을 지원

    <!-- Bootstrap -->

    <link href="css/bootstrap.min.css" rel="stylesheet" media="screen">  //bootstrap css include

  </head>

  <body>

    <h1>Hello, world!</h1>

    <script src="http://code.jquery.com/jquery.js"></script>  //jquery include

    <script src="js/bootstrap.min.js"></script>  //bootstrap script include

  </body>

</html>


* 사용.

 1. scaffolding : 전체적인 layout을 설정(http://getbootstrap.com/2.3.2/scaffolding.html#layouts)

<div class="container-fluid">

  <div class="row-fluid">

    <div class="span2">

      <!--Sidebar content-->

    </div>

    <div class="span10">

      <!--Body content-->

    </div>

  </div>

</div> 

 

 2. Responsive design(http://getbootstrap.com/2.3.2/scaffolding.html#responsive)

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<link href="assets/css/bootstrap-responsive.css" rel="stylesheet"> 

  - head 에 위 부분을 추가적으로 선언해야 함.

  - viewport는 bootstrap 사용을 위해 선언해 주었기 때문에 다시 해줄 필요는 없음.

  - 반응형 디자인 : 창의 크기가 작아졌을 경우 스스로 창에 크기에 따라 전체적인 구도가 변화하는 것.

  - 모바일 기기에서 볼 때 많이 유용할 수 있음.


 3. component(메뉴)

  (1) navbar(상단메뉴) - http://getbootstrap.com/2.3.2/components.html#navbar

 - Responsive navbar sample

<div class="navbar nav-bar-fixed-top">

  <div class="navbar-inner">

    <div class="container">

 

      <!-- .btn-navbar is used as the toggle for collapsed navbar content -->

      <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">

        <span class="icon-bar"></span>

        <span class="icon-bar"></span>

        <span class="icon-bar"></span>

      </a>

 

      <!-- Be sure to leave the brand out there if you want it shown -->

      <a class="brand" href="#">Project name</a>

 

      <!-- Everything you want hidden at 940px or less, place within here -->

      <div class="nav-collapse collapse">

        <!-- .nav, .navbar-search, .navbar-form, etc -->

      </div>

 

    </div>

  </div>

</div>


 - Fixed to Top

<div class="navbar navbar-fixed-top">

  ...

</div>


 - views/head.php

<head>

    <title>Bootstrap 101 Template</title>

    <meta name="viewport" content="width=device-width, initial-scale=1.0">  //모바일을 지원

    <!-- Bootstrap -->

    <link href="css/bootstrap.min.css" rel="stylesheet" media="screen">  //bootstrap css include

    <style>body{padding-top:60px;}</style>

    <link href="assets/css/bootstrap-responsive.css" rel="stylesheet">

</head> 

  - Responsive navbar sample를 보면 navbar-fixed-top 클래스를 추가해 주면서 body안에 값들이

    메뉴바들에 의해 가려지게 되어 head에 bootstrap.min.css와 bootstrap-responsive.css 선언 사이에 style 

    body{padding-top:60px;}이라고 선언하여 구도를 맞춰주었음.


  (2) navs(사이드메뉴) - http://getbootstrap.com/2.3.2/components.html#navs

 -Stacked tabs sample

<ul class="nav nav-tabs nav-stacked">  //해당 클래스를 복사하여 원하는 곳에 copy 한다.

  ...

</ul> 

  -views/topic_list.php

<div class="span2">  //scaffolding 을 참조하여 적용한 Sidebar content

    <ul class="nav nav-tabs nav-stacked">  //copy

        <?

            foreach($topics as $entry) {

        ?>

            <li><a href="index.php/topic/get/<?=$entry->id?>"><?=$entry->id?></a></li>

        <?

            }

        ?>

    </ul>

</div>

 

 4. bootstrap 사이트 활용

  - 각 기능 들에 대한 샘플 소스들이 나와 있으며, 이것에 대한 클래스등을 잘 활용하여 UI에 변화를 줄 수 있음.

  - 원하는 class 및 소스를 가져와서 태그에 넣어주면 됨.

  - optional display variantions 를 보면 옵션으로 설정할 수 있는 클래스들이 나왔음.

반응형
반응형

리스트를 누르고 해당 정보를 보는 페이지마다 다시 리스트를 넣으려고 한다.


controller/topic.php

function __construct() {

    parent::__construct();

    $this->load->database();

    $this->load->model('topic_model');

}


function index() {

    $data=$this->topic_model->gets();  //topic_model.php 의 gets라는 method를 사용하여 $data에 넣겠다.

    $this->load->view('main',array('topics'=>$data));

}


function get($id) {

    $topic=$this->topic_model->get($id);

    $this->load->view('get',array('topic'=>$topic));  //기존 $id에서 $topic 값을 가져오는 것으로 수정.


views/main.php

This is Main page.

<?

    foreach($topics as $entry) {

?>

    <li><a href="index.php/topic/get/<?=$entry->id?>"><?=$entry->id?></a></li>

<?

    }

?> 


메인페이지에 리스트를 뿌려주는 view 파일을 각각 리스트 정보안에서도 활용 하기 때문에 중복이 된다.

위 빨간색 표시 부분을 잘라내기 하여 topic_list.php 파일로 변경하고 controller 파일에서 불러옴.


views/main.php

 This is Main page.


views/topic_list.php

<?

    foreach($topics as $entry) {

?>

    <li><a href="index.php/topic/get/<?=$entry->id?>"><?=$entry->id?></a></li>

<?

    }

?>


controller/topic.php

function __construct() {

    parent::__construct();

    $this->load->database();

    $this->load->model('topic_model');

}


function index() {

    $topic_list=$this->topic_model->gets();  //전체 리스트를 디비에서 가져옴.

    $this->load->view('main'); //main 페이지는 더이상 받을 인자가 없다.

    $this->load->view('topic_list',array('topics'=>$topic_list)); //전체 리스트를 뿌려줌.

}


function get($id) {  //전체 리스트에서 선택된 정보값을 출력해주는 부분(이곳에 전체리스트를 다시 가져옴.)

    $topic_list=$this->topic_model->gets();  //전체 리스트를 디비에서 가져옴.

    $topic=$this->topic_model->get($id);

    $this->load->view('get',array('topic'=>$topic));  //기존 $id에서 $topic 값을 가져오는 것으로 수정.

    $this->load->view('topic_list',array('topics'=>$topic_list));

// 정보를 보여준 후 전체 리스트를 뿌려준다.

}


반응형

'Programming > CodeIgniter' 카테고리의 다른 글

URI Routing  (0) 2015.09.07
Bootstrap 라이브러리  (0) 2015.09.07
Model(3/5) - 소스 중복제거  (0) 2015.09.07
Model(2/5) - 리스트와 각 리스트 정보 구현  (0) 2015.09.04
Model(1/5) - 설정 및 사용  (0) 2015.09.04
반응형

* Controller 파일의 중복소스 제거


1. __construct()

 - controller/topic.php (아래 소스의 빨간색 글씨로 되어 있는 부분이 중복되고 있다.)

function index() {

    $this->load->database();

    $this->load->model('topic_model');  //topic_model.php 파일을 불러오겠다.

    $data=$this->topic_model->gets();  //topic_model.php 의 gets라는 method를 사용하여 $data에 넣겠다.

    $this->load->view('main',array('topics'=>$data));

}


function get($id) {

    $this->load->database();

    $this->load->model('topic_model');

    $topic=$this->topic_model->get($id);


    $this->load->view('get',array('topic'=>$topic));  //기존 $id에서 $topic 값을 가져오는 것으로 수정.

}


 - 중복제거

function __construct() {

    parent::__construct();

    $this->load->database();

    $this->load->model('topic_model');

}


function index() {

    $data=$this->topic_model->gets();  //topic_model.php 의 gets라는 method를 사용하여 $data에 넣겠다.

    $this->load->view('main',array('topics'=>$data));

}


function get($id) {

    $topic=$this->topic_model->get($id);

    $this->load->view('get',array('topic'=>$topic));  //기존 $id에서 $topic 값을 가져오는 것으로 수정.

}


2. function

 - controller/topic.php (아래 소스의 빨간색 글씨로 되어 있는 부분이 중복되고 있다.)

function __construct() {

    parent::__construct();

    $this->load->database();

    $this->load->model('topic_model');

}


function index() {

    $this->load->view('head');

    $data=$this->topic_model->gets();

    $this->load->view('topic_list',array('topics'=>$topics));


    $this->load->view('main');


    $this->load->view('footer');

}


function get($id) {

    $this->load->view('head');

    $data=$this->topic_model->gets();

    $this->load->view('topic_list',array('topics'=>$topics));


    $topic=$this->topic_model->get($id);

    $this->load->view('get',array('topic'=>$topic));


    $this->load->view('footer');

}


 - 중복제거

function __construct() {

    parent::__construct();

    $this->load->database();

    $this->load->model('topic_model');

}


function index() {

    $this->head();


    $this->load->view('main');


    $this->load->view('footer');

}


function get($id) {

    $this->head();


    $topic=$this->topic_model->get($id);

    $this->load->view('get',array('topic'=>$topic));


    $this->load->view('footer');

}


function head(){

    $this->load->view('head');

    $data=$this->topic_model->gets();

    $this->load->view('topic_list',array('topics'=>$topics));

}


반응형
반응형

- 리스트를 보여주고 각 리스트에 대한 정보를 보여준다.


* models/topic_model.php

class Tablename_model extends CI_Model {

    function __construct() {   //method 를 초기화

        parent::__construct();

    }


   public function gets() {

       return $this->db->query('SELECT id,name from tablename')->result();

   }


   public function get($topic_id) {

        return $this->db->get_where('tablename',array('id'=>$topic_id))->row();

//    get_where는 active record라는 방식이라 함. 아래 쿼리와 동일하다고 함.

//    get_where 의 첫번째 인자는 테이블명, 두번째 인자의 id는 컬럼명임.

//    return $this->db->query('SELECT * from tablename where id = $topic_id')->row();

//    active record 방식의 장점은 DB의 인식성이 좋음.

   }


active record 방식을 사용하였을 때 특정 컬럼만 가져오고 싶다면, 아래와 같이 기술하면 된다.

$this->db->select('id');

$this->db->select('name');

$this->db->select(substr('chardate',1,4) chardate);

return $this->db_get_where('tablename',array('id'=>$topic_id))->row();



* controller/topic.php

function index() {

    $this->load->database();

    $this->load->model('topic_model');  //topic_model.php 파일을 불러오겠다.

    $data=$this->topic_model->gets();  //topic_model.php 의 gets라는 method를 사용하여 $data에 넣겠다.


    $this->load->view('main',array('topics'=>$data));

}


function get($id) {

    $this->load->database();

    $this->load->model('topic_model');

    $topic=$this->topic_model->get($id);


    $this->load->view('get',array('topic'=>$topic));  //기존 $id에서 $topic 값을 가져오는 것으로 수정.


}


* views/get.php

 This is get page.

<article>

    <h1><?=$topic->id?></h1>

    <div><?=$topic->name?></div>

</article>



다음에는 현재 구현한 소스를 가지고 간결화 하는 작업을 할 것이다.

model 수업을 들으면서 머리가 많이 복잡해지기 시작했다.


어떤 흐름인지는 알겠는데, 정리가 되지 않는 찝찝함?

동영상 강의를 반복적으로 듣다가 잊지 않기 위해 블로그에 글을 적어본다.

담주에 보게 되면 또 기억할 수 있을지가 의문이네 ㅠㅠ

반응형

'Programming > CodeIgniter' 카테고리의 다른 글

Model(4/5) - 각 리스트 정보 페이지마다 전체리스트 생성 구현  (0) 2015.09.07
Model(3/5) - 소스 중복제거  (0) 2015.09.07
Model(1/5) - 설정 및 사용  (0) 2015.09.04
View  (0) 2015.09.04
Controller  (0) 2015.09.04
반응형

* 데이터와 관련된 일을 한다. 즉, 데이터 베이스와 연관된 일을 하는 곳이다.

* CI에서 model 기능이 제대로 작동하려면 CI_Model 를 반드시 상속 받아야 한다.


* 실습을 위해서는 반드시 데이터 베이스와 연동이 되어 있어야 한다.

* 설정은 application/config/database.php 에서 설정을 할 수 있다.

 - 기본적으로 설정되어야 할 값

$db['default']['hostname'] = 'localhost'; // IP 또는 hostname

$db['default']['username'] = 'scott';

$db['default']['password'] = 'tiger';

$db['default']['database'] = 'PROD';

$db['default']['dbdriver'] = 'odbc'; 

 - 설정 해주는 라인 위쪽으로 올라가보면 주석처리로 어떤 것들이 지원이 되고 어떤 이름을 넣어야 하는지도 나와 있음.

    system/database/drivers 경로에 보면 지원하고 있는 dbdriver 파일들이 있음.

 - database.php 파일은 보안상으로 많은 문제를 발생 시킬 수 있기 때문에, 파일에 대한 권한을 좀 더 신경을 써야 함.

    git 같은 소스관리 툴을 사용할 때는 ignore가 되도록 해야하며, 여러모로 주의를 기울여야하는 파일임.


* 데이터베이스를 불러오는 방법.

1. application/config/autoload.php 파일에서 $autoload['libraries'] = array('database') 라고 설정.

   - 이럴경우 계속적으로 데이터베이스를 불러옴.(테스트는 안해봄)

   - 더이상 데이터베이스를 불러오지 않아 편할지는 모르겠으나, DB를 많이 사용하지 않는 페이지이거나, 전혀 사용되지 않        는 페이지들이 DB 사용률 보다 페이지가 더 많이 불러오게 되는 경우는 안좋을 수 있다.

     즉, 사용 할 때마나 DB에 접속하는 방법이 좋다. 매번 DB에 접속하는 것보다 사용 할 때만 접속하는 것이 속도에 좋고

         DB의 부담도 줄어 들 것이다.


2. 사용 할 때마나 DB에 접속.

   - application/models/ 폴더에 ***_model.php를 생성하고 controller 파일에서 제어를 하는 방법.

   - 일반적으로 모델 파일명 규칙은 사용될 데이터베이스의 테이블 이름을 사용하고 _model이라고 반드시 붙여준다.


* models/Tablename_model.php

class Tablename_model extends CI_Model {

    function __construct() {   //method 를 초기화

        parent::__construct();

       // 매번 초기화를 해야하는 method가 있으면 이곳에 추가를 해주면 됨.

    }


   public function gets() {

       //$this->db->query()->result(); 문법.

       return $this->db->query('SELECT id,name from tablename')->result();

       //객체 - result(), 배열 - result_array(), 값이 하나 일 경우 - row()

   }

 


* controller/topic.php

function index() {

    $this->load->database();

    $this->load->model('topic_model');  //topic_model.php 파일을 불러오겠다.

    $data=$this->topic_model->gets();  //topic_model.php 의 gets라는 method를 사용하여 $data에 넣겠다.


    $this->load->view('main',array('topics'=>$data));

}


function get($id) {


    $this->load->view('get',array('id'=>$id));


}


* views/main.php

<?

    foreach($topics as $entry) {

?>

    <li><a href="index.php/topic/get/<?=$entry->id?>"><?=$entry->id?></a></li>

<?

    }

?>


* views/get.php

 This is get page.<?=$id?>


반응형

'Programming > CodeIgniter' 카테고리의 다른 글

Model(3/5) - 소스 중복제거  (0) 2015.09.07
Model(2/5) - 리스트와 각 리스트 정보 구현  (0) 2015.09.04
View  (0) 2015.09.04
Controller  (0) 2015.09.04
CodeIgniter와의 규칙?  (0) 2015.09.04
반응형

화면 상에 출력하는 것, 시각적으로 표시하는 것들을 관리.

공통적으로 쓰는 코드/로직들을 통합하여, 수정/관리할때 수월해 짐.


공통적으로 사용되는 것 중 HTML의 소스가 대표적일 것이다.

모든페이지 마다 아래와 같은 html 소스가 들어가게 될 것인데, 불필요하게 사용되지 않게 view에서 관리할 수 있다.


* 공용 소스

<!DOCTYPE html>

<html>

    <head>

        <meta charset="utf-8">

    </head>

    <body>

    </body>

</html>


* views/_head.php 

<!DOCTYPE html>

<html>

    <head>

        <meta charset="utf-8">

    </head>

    <body>


* views/_footer.php

    </body>

</html>


* controller 파일에서 view를 불러 올 때 사용 예제

$this->load->view('_head');

$this->load->view('main',array('id'=>$id));

$this->load->view('_footer'); 


반응형

'Programming > CodeIgniter' 카테고리의 다른 글

Model(2/5) - 리스트와 각 리스트 정보 구현  (0) 2015.09.04
Model(1/5) - 설정 및 사용  (0) 2015.09.04
Controller  (0) 2015.09.04
CodeIgniter와의 규칙?  (0) 2015.09.04
CodeIgniter 설치  (0) 2015.09.04
반응형

* View와 Model을 제어하는 역할을 함.


* URL: localhost/index.php/welcome/input/test_name 참고


    index.php : 반드시 존재해야 하나 .htpaccess 파일로 제거할 수 있음.

    welcome : welcome.php 파일.

    input : welcome.php 파일에 input함수라고 하여 또 다른페이지를 불러 올 수 있음.(함수 또는 method 라 함.)

    test_name : welcome.php 파일에 input 함수의 인자


 - controller/welcome.php(echo 문은 설명을 위해 넣은 것이고, views에 포함되어야 하는 것이 맞음)

class Welcome extends CI_Controller {

    public function index() {

       $this->load->view('url');

       echo "URL에 welcome 뒤로 아무것도 붙지 않았을 경우 기본적으로 보여지는 내용";

    }

    public function input($id) {

       $this->load->view('input');

       echo "welcome/input 이 오면 input의 내용을 보여주도록..";


       $this->load->view('id');

       echo $id."$id는input 다음에 값이 올수 있는 인자(즉 파라미터라 할 수 있음)";

    }

}


반응형

'Programming > CodeIgniter' 카테고리의 다른 글

Model(1/5) - 설정 및 사용  (0) 2015.09.04
View  (0) 2015.09.04
CodeIgniter와의 규칙?  (0) 2015.09.04
CodeIgniter 설치  (0) 2015.09.04
CodeIgniter 관한 글을 작성하며..  (0) 2015.09.04
반응형

1. class명은 반드시 파일명과 동일해야 하며 첫글자는 대문자로 시작.


2. controller는 CI_Controller, Model은 CI_Model 을 반드시 상속받아야 함.(CodeIgniter가 제대로 작동하기 위함)


3. Model 파일은 반드시 "_model"이라고 명시를 해야 한다.

 ex. application/models/test.php         (X)

     application/models/test_model.php (O)


4. 문법

    $this->load->view('view_file');  => view파일 중 view_file.php를 불러옴.

    $this->load->database();       => DB에 연결

    $this->load->model('file_model');  => model파일 중 file_model.php를 불러옴.

반응형

'Programming > CodeIgniter' 카테고리의 다른 글

Model(1/5) - 설정 및 사용  (0) 2015.09.04
View  (0) 2015.09.04
Controller  (0) 2015.09.04
CodeIgniter 설치  (0) 2015.09.04
CodeIgniter 관한 글을 작성하며..  (0) 2015.09.04
반응형

홈페이지 및 다운로드 : http://www.codeigniter.com


파일을 다운로드 받고 DocumentRoot 경로에 압축을 풀면 끝!

확인은 DocumentRoot/index.php 을 누르면 아래와 같은 문구가 출력 된다.


Welcome to CodeIgniter!

The page you are looking at is being generated dynamically by CodeIgniter.

If you would like to edit this page you'll find it located at:

application/views/welcome_message.php

The corresponding controller for this page is found at:

application/controllers/welcome.php

If you are exploring CodeIgniter for the very first time, you should start by reading the User Guide.


반응형

'Programming > CodeIgniter' 카테고리의 다른 글

Model(1/5) - 설정 및 사용  (0) 2015.09.04
View  (0) 2015.09.04
Controller  (0) 2015.09.04
CodeIgniter와의 규칙?  (0) 2015.09.04
CodeIgniter 관한 글을 작성하며..  (0) 2015.09.04
반응형

생활코딩에서 항상 많은 도움을 받고 있는 것 같다.


CodeIgniter 에 관한 내용을 작성하게 된 계기는 프레임워크에 대해서 새롭게 알게 되었으며,

알게 된 이후 자주가는 opentutorials에서 동영상 강의 찾아 듣게 된 이후 부터이다.


정확하게 말하자면, model 수업을 본 이후 시점이다.

볼 때는 이해가 되었으나, 돌아서면 이해가 너무 되지 않아서 잊지 않기 위해 작성을 시작했다.


지금부터 열공 !!!!


CodeIgniter는 생활코딩에서 동영상 강의를 듣고 혼자만의 노트로 끄적끄적거려 볼란다.

무조건 내 중심으로 글을 적을 것이다.

반응형

'Programming > CodeIgniter' 카테고리의 다른 글

Model(1/5) - 설정 및 사용  (0) 2015.09.04
View  (0) 2015.09.04
Controller  (0) 2015.09.04
CodeIgniter와의 규칙?  (0) 2015.09.04
CodeIgniter 설치  (0) 2015.09.04

+ Recent posts