php - 阅读页面上的特定帖子

标签 php mysql sql codeigniter url

我的问题是我无法读取单个页面上的数据。例如,在首页上,我从数据库中提取了一些笑话;我希望能够点击一个笑话并将用户发送到一个网址,例如joks.com/read/a-chicken-crossed-the-road。目前,它会将我发送到我的自定义 404 页面,网址为笑话.com/read/1(1 为笑话_id),我有一段时间无法解决这个问题,所以我想我会在这里试试。

这是我的设置:

主视图:

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

阅读 View :

<?php

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

?>

Controller :

//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 个字符,那就太棒了。

最佳答案

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

您没有指定 Controller 名称,让我们假设您希望将其称为“读取”

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

编辑routes.php如下:

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

上面一行采用如下 URL:server.ufo/read/1/my-super-joke并将其翻译为 server.ufo/read/read_it/{id}

让 Controller 结构如下:

<?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是你的魔法鼻涕虫(名字)

关于php - 阅读页面上的特定帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22997460/

相关文章:

php - Mysql加入多于2个表

mysql - 在 mysql shell 中显示没有表格行的查询结果(非表格输出)

SQL 语句 t1、t2 等

javascript - 如何使用循环提取数组中的json数据?

mysql - 按月和年输入选择前 12 个月

SQL 选择其中只有特定值的行

sql - 与 UNION 相比,INTERSECT 的优先级是否更高?

php - 在 wordpress 站点中使用 SSL

php - 大型用户电子邮件/消息系统的良好设计模式或开源软件?

php - 选择时将不存在的字段设置为值