2012-05-21 3 views
2

10672712 내가 추측 질문 ID입니다 위의 링크에서 다음 링크 구조된답니다 URL : 다음 링크를 확인하는 경우 때문에 URL에서 ID와 기사 제목을 표시하는 방법

http://stackoverflow.com/questions/10672712/voting-system-like-stackoverflow 

를 참조하십시오 위와 같은 위치로 얻을 것이다 :

http://stackoverflow.com/questions/10672712 

을 이제 질문 ID와 그 뒤에 다음 링크가 자동으로 (슬러그로) 질문 제목을 추가하는 브라우저의 주소 표시 줄에있는 것을 알 수 위의 링크를 사용하는 경우 위의 첫 번째 링크처럼 나타납니다.

Codeigniter를 사용하여 기사 기반 웹 사이트를 만들려고합니다.

function articles(){ 


    $id=$this->uri->segment(3); 

    $this->load->model('mod_articles'); 
    $data['records']=$this->mod_articles->list_articles($id); 
    $this->load->view('view_article',$data); 
    } 

내 모델 : 특정 기사를 표시하려면 나는 다음과 같은 컨트롤러를 가지고 당신이 다음>localhost/my_base_url/my_controller/my_funtion/article_id this- 충돌하는 경우

function list_articles($id) 

     $this->db->select('*'); 
     $this->db->from('cx_article'); 
     $this->db->join('cx_author', 'cx_author.author_id = cx_article.author_id'); 
     $this->db->where('article_id', $id); 
     $query = $this->db->get(); 

     if ($query->num_rows() > 0) 
     { return $query->row_array(); 
     } 
     else {return NULL;} 

    } 

내 문서에서는 보여줍니다. 이제 내가 성취하려고하는 것은 누군가 타격을받는 경우입니다. article_id 바로 뒤에 슬러그로 기사 제목을 자동으로 추가하고 싶습니다. (위 예제와 마찬가지로)

그 방법을 알려주시겠습니까? ? (: 투표 시스템과 같은-유래 예) 내 DB 테이블에서

감사합니다 :)

P.S은 내가 슬러그 나의 기사 제목을 저장하는 article_slug라는 열을 가지고있다.

답변

9

컨트롤러/my_controller.php

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

class My_controller extends CI_Controller { 

    function my_function($int_id = NULL, $str_slug = '') { 

     $row = $this->db->get_where('cx_article', array('article_id' => $int_id))->row(); 

     if ($row and ! $str_slug) { 

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

      $str_slug = url_title($row->title, 'dash', TRUE); 
      redirect("my_controller/my_function/{$int_id}/{$str_slug}"); 

     } 

     // Run the rest of the code here 

    } 

} 

이 당신이 당신의 슬러그를 기반으로 아무것도 확인하지 않기 때문에 데이터베이스의 슬러그 열을 가지고 아무 소용이 없으며,이 유일하다.

+0

감사합니다 :) Brilliant –

+0

좋아, 나는 슬러그 열을 제거합니다. 감사 :) –