阅读页面上的特定帖子

时间:2014-04-10 19:38:37

标签: php mysql sql codeigniter url

我的问题是我无法读取单个页面上的数据。例如,在首页上,我从数据库中取出了一些笑话;我希望能够点击一个笑话并将用户发送到一个网址,例如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 slug中的前55个字符,那将是辉煌的。

1 个答案:

答案 0 :(得分:0)

如果我正确理解了这个问题,你需要一个slug作为read()函数的参数。

你没有指定控制器名称让我们假设你希望它被调用&#34;阅读&#34;

最简单的方法是执行以下操作:

编辑routes.php如下:

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

行将网址视为以下内容: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是你的魔法slu((名字)