2014-04-10 175 views
0

我的問題是我無法讀取單個頁面上的一段數據。例如,在頭版上,我從db中引入了一些笑話;我希望能夠點擊一個笑話並將用戶發送到諸如jokes.com/read/a-chicken-crossed-the-road之類的網址。目前,它發送給我的自定義404頁面的網址是jokes.com/read/1(1是joke_id),我一直無法解決這個問題,所以儘管我會試試這裏。閱讀頁面上的特定帖子

這裏是我的設置:

主要觀點:

<a href="<?php base_url()?>read/<?php echo $joke_id ?>"> <p class="joke-content"><?php echo $joke; ?></p></a> 

只讀視圖:

<?php 

foreach($results as $row){ 
echo "<li>$row->joke</li>"; 
echo "<li>$row->name</li>"; 
echo "<li>$row->date_added</li>"; 
} 

?> 

控制器:

//constructor class enables a function called to be used in any function within this controller 
function __construct(){ 
    // Call the Controller constructor 
    parent::__construct(); 
    $this->load->model('getjokes_m'); 
} 

public function index(){ 

    $this->read(); 

} 

//main jokes functions grabs all the jokes in the database and orders them in their correct category 
public function read(){ 
    $data['results'] = $this->getjokes_m->readJokes($this->uri->segment(3)); 

    $this->load->view('template/header'); 
    $this->load->view('template/sidebar'); 
    $this->load->view('content/read', $data); 
    $this->load->view('template/footer'); 


} 

,最後我的模型:

function readJokes() 
{ 
    $query = $this->db->query('SELECT j.*, c.name FROM jokes j LEFT JOIN category c ON c.category_id = j.category_id WHERE joke_id = ?'); 

    //displays the results if the table has data inside 
    return $query->result(); 

} 

路線:

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

$route['404_override'] = 'not_found'; 

$route['register'] = 'login/register'; 
$route['logout'] = 'login/logout'; 
$route['admin'] = 'admin/login'; 
$route['noaccess'] = 'login/noaccess'; 

我想可能是我使用的SQL語句,因爲它不返回任何數據。

如果有人能指出我正確的方向,爲什麼這不起作用,並獲得URL中的前55個字符,這將是輝煌的。

+2

您的「視圖」甚至沒有鏈接... –

+0

數據的鏈接位於主控制器中,我已將其編輯爲現在顯示 – steviem1986

+0

它應該是href ='<?回聲base_url() – aseferov

回答

0

如果我正確理解這個問題,你需要一個slu as作爲你的read()函數的參數。

你沒有指定控制器的名字讓我們假設你希望它被稱爲「讀」

最簡單的方法是做如下:如下

編輯routes.php

$routes['read/(:num)/(:any)'] = "read/read_it/$1"; 

上面的行取URL如下:server.ufo/read/1/my-super-joke並將其翻譯爲此server.ufo/read/read_it/{id}

讓有控制器的結構如下:

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

class Read extends CI_Controller { 

    public function __construct() 
    { 
     parent::__construct(); 
     //Do your magic here 
    } 

    public function index() 
    { 
     //leave empty or redirect somewhere 
    } 

    public function read_it($id = FALSE) 
    { 
     if ($id === FALSE) redirect(); //go to default controller 
     $data['results'] = $this->getjokes_m->readJokes($id); //id is NUMERICAL auto incremented value!! 

     $this->load->view('template/header'); 
     $this->load->view('template/sidebar'); 
     $this->load->view('content/read', $data); 
     $this->load->view('template/footer'); 

    } 

} 

/* End of file read.php */ 
/* Location: ./application/controllers/read.php */ 

,最後生成的鏈接很簡單:

<a href="<?= base_url('read/'.$joke_id.'/'.$joke_name)?>"> <p class="joke-content"><?php echo $joke; ?></p></a>

記得joke_id被自動增量ID,並joke_name是你的魔蛞蝓(名稱)

+0

感謝隊友,你救了我很多麻煩!它抓住了實際的joke_id和笑話,但仍然沒有鏈接到我的風格和JavaScript。它與路由有關 – steviem1986

+0

在原始問題中沒有關於樣式(CSS)或JavaScript的內容,請編輯您的問題,以便我知道什麼是「up」,並且還包括一些代碼和文件層次結構。 – Kyslik