dongzi3434 2014-04-10 19:38
浏览 48

阅读页面上的特定帖子

My problem is that I can't read a piece of data on an individual page. For example, on the front page, I have a number of jokes pulled in from the db; I want to be able to click on a joke and send the user to a url such as jokes.com/read/a-chicken-crossed-the-road. At the moment, it sends me to my custom 404 page with the url being jokes.com/read/1 (1 being the joke_id) and I haven't been able to get past this problem for a while, so I though I would try here.

Here is my setup :

main view:

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

read 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');


}

and finally my model:

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();

}

routes:

$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';

I think it might be the sql statement I am using, because it doesn't return any data.

If somebody could point me in the right direction as to why this is not working and to get the first 55 characters in the URL slug, it would be brilliant.

  • 写回答

1条回答 默认 最新

  • douli7841 2014-04-11 06:32
    关注

    If I understand this problem correctly you want a slug as a parameter of your read() function.

    you did not specify controllers name lets assume you want it to be call "read"

    The easiest way is to do following:

    edit routes.php as following:

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

    line above takes URL as following: server.ufo/read/1/my-super-joke and translates it to this server.ufo/read/read_it/{id}

    lets have controller structure as following:

    <?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 */
    

    and lastly generation of links is simple:

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

    remember joke_id is autoincremented ID, and joke_name is your magic slug (name)

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行