CI 묻고 답하기

제목 초보자입니다. 제가 이해하고있는 개념이 맞는 것인지... 조언부탁드립니다 !
카테고리 CI 2, 3
글쓴이 초보라서열공중 작성시각 2018/11/30 20:00:38
댓글 : 2 추천 : 0 스크랩 : 0 조회수 : 11320   RSS

controllers/topic.php

 

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

class Topic extends CI_Controller {

  //이부분은 무조건적으로 호출해야하는 건지 궁금합니다.

    function __construct()

    {

        parent::__construct();

        $this->load->database();

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

    }

 

    function index(){

      //view 폴더의 head.php를 부른다.   //view 폴더의 head.php를 부른다.

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

        //변수 $topics를 만들어서 모듈 topic_model의 gets함수를 호출해서 저장한다.

        //return 되는 값은 $this->db->query('SELECT * FROM topic')->result();

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

        //저장된 result()값이 $topics에 있는 상황(데이터베이스에 있는 정보들)

        //view 폴더의 topic_list.php에 $topics를 'topics'로 넘겨준다        

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

        //view 폴더의 main.php를 부른다.

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

    //view 폴더의 footer.php를 부른다.

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

    }

    

    //http://localhost/index.php/topic/get/[id값]

    function get($id){

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

        //변수 topics를 만들어서모듈 topic_model의 gets함수를 호출해서 저장한다.

        //return 되는 값은 $this->db->query('SELECT * FROM topic')->result();

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

        //저장된 result값이 $topics에 있는 상황(데이터베이스에 있는 정보들)

        //view 폴더의 topic_list.php에 $topics를 'topics'로 넘겨준다.

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

        

        //변수 topic을 만들어서 모듈 topic_mode의 get(id값)함수를 호출해서 저장한다.

        //return되는 값은  $this->db->get_where('topic', array('id'=>$topic_id))->row();

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

        //저장된 result값이 $topic에 있는 상황(데이터베이스에 있는 정보들)

        //view 폴더의 get.php에 $topic(한줄데이터...왜냐하면 row니까)를 'topic'로 넘겨준다.

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

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

    }

}

?>

 

models/topic_model.php

 

<?php

class Topic_model extends CI_Model {

    function __construct(){

        parent::__construct();

    }

    

    public function gets(){

        //정보 전체를 가져와서 result()를 리턴한다.

        return $this->db->query('SELECT * FROM topic')->result();

    }

    

    public function get($topic_id){

        

        //id가 $topic_id(받아온 id값)에 해당하는 데이터베이스를 한줄(row())만 가져온다!

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

        /* return $this->db->get_where('SELECT * FROM topic WHERE id='.$topic_id); */

    }

    

}

 

?>

 

views/get.php

<article>

<h1><?=htmlspecialchars($topic->title)?></h1>

<div>

<?=htmlspecialchars($topic->description)?>

</div>

</article>

 

views/topic_list.php

 

<ul>

<?php

foreach($topics as $entry){

?>

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

<?php

}

?>

</ul>

 

이렇게 됩니다..

 

제가 주석처리 한부분들이 제가 이해하고있는 개념인데.

혹시 틀리다면, 저에게 가르쳐주시면 감사하겠습니다...

혼자이해하는데 어려움이 있기에, 고수분들의 도움을 받고자 질문합니다 ! 감사합니다

 

 

 다음글 로그인시 비밀번호 비교중 문제(해결후 방법 첨부) (3)
 이전글 CI 내에서 서버에 따라 segment 호출위치를 변경... (10)

댓글

한대승(불의회상) / 2018/12/03 09:40:21 / 추천 0

뭘 알고 뭘 모르는지 모르겠네요.

궁금한 부분을 구체화하여 나누어 질문해 주세요.

kaido / 2018/12/03 09:45:35 / 추천 0

일단 $_GET 값은 function 메소드로 받는게 아니라 $this->input->get() 형태로 받습니다.

[물론 메소드로 받는 방법도 있지만 우선 기초부터 시작 정리하고 해도 늦지 않습니다.]

 

__construct 생성자는 무조건 호출해야 합니다.

 

일단 의심을 버리시고 메뉴얼 부터 하나씩 따라서 만들어 보시다 보면 궁금증이 해결되실 것입니다.