Ganesh Mulay
Ganesh Mulay

Reputation: 171

How to linking detail page from landing page in codeigniter framework

I am now learning php codigniter framework.

I am making blog system.

I have done blog landing page which is 5 blogs on 1 page and i am now trying to when i click on any single blog it will go to respective blog detail page.

how should i give detail page link to respective blog.

here is my controller

Class News extends CI_Controller {

        public function Index(){
            $this->load->model("Newsmodel");
            $data['a'] = $this->Newsmodel->select();
            $this->load->view("news/news-landing",$data);                  
        }

        public function news_detail(){
            $this->load->model("Newsmodel");
            $data['a'] = $this->Newsmodel->select_single_row();
            $this->load->view("news/news-detail",$data);
        }

    }

here is my view for landing page

foreach ($a->result() as $single_result) {

    echo "<div class='blog-post'>";
            echo "<img src=".$single_result->image_path." alt='image is here'>";
            echo "<div class='post-content-text'>";
                echo "<h2><a href='".base_url('news/'.$single_result->id)."'>".$single_result->title."</a></h2>";
                echo "<span>".$single_result->date."</span>";
                echo "<p>".substr( $single_result->body, 0, 300 )."</p>";
                echo "<a href='".base_url('news/'.$single_result->id)."'>Continue reading...</a>";
            echo "</div>";
        echo "</div>";

}

please help and thanks in advance.

Upvotes: 0

Views: 774

Answers (2)

Yash
Yash

Reputation: 1436

You have to pass id to the news_detail function:

 public function news_detail($id){
   $data['a'] = $this->db->get_where('mytable', array('id' => $id))->row_array();
   $this->load->view("news/news-detail",$data);
 }

You need to change the view as only continue reading link:

echo "<a href='".base_url('news/news_detail/'.$single_result->id)."'>Continue reading...</a>";

Upvotes: 1

Ganesh Mulay
Ganesh Mulay

Reputation: 171

I replace this lines

$data['a'] = $this->Newsmodel->get_where('mytable', array('id' => $id))->row_array();

Into

$this->db->select('*');
$this->db->from('news');
$this->db->where('id', $id);
$data['a'] = $this->db->get();

Its Working, Thanks Yash.

Upvotes: 0

Related Questions