阅读页面上的特定帖子

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.

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)