CodeIgniter Forums
display id and title in url Codeigniter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: display id and title in url Codeigniter (/showthread.php?tid=58742)

Pages: 1 2


display id and title in url Codeigniter - El Forum - 07-14-2013

[eluser]johanriyan[/eluser]
hello guys,

i am newbie in codeigniter.

i have problem in url,

i want display id and title in url like this :

www.example.com/articel/12345/example-news


help me please.

thks.



display id and title in url Codeigniter - El Forum - 07-14-2013

[eluser]JoostV[/eluser]
Code:
$id = 12345;
$title = 'Example news';

// URL
echo site_url('articel/' . $id . '/' . url_title($title));

// Link
echo anchor('articel/' . $id . '/' . url_title($title));



display id and title in url Codeigniter - El Forum - 07-14-2013

[eluser]johanriyan[/eluser]
Hello joostv,

thks for respone.

but i have route like this :

Code:
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';

i am try not work.

my controller :

Code:
public function index()
{
  $data['news'] = $this->m_admin->get_news();
  $data['title']="Administrator";  
  $this->load->view('include/header',$data);
  $this->load->view('include/menu');
  $this->load->view('v_news',$data);
  $this->load->view('include/footer');

}

public function view($slug)
{
  
  $data['news_item'] = $this->m_admin->get_news($slug);

  if (empty($data['news_item']))
  {
   show_404();
  }

  $data['title'] = $data['news_item']['title'];
  $this->load->view('include/header',$data);
  $this->load->view('include/menu');
  $this->load->view('v_news_detail',$data);
  $this->load->view('include/footer');

}

This is view :

Code:
<?php foreach ($news as $news_item): ?>

    <h2>&lt;?php echo $news_item['title'] ?&gt;</h2>
    <div id="main">
        &lt;?php echo $news_item['text'] ?&gt;
    </div>
    
    <p><a href="&lt;?php echo $news_item['slug'] ?&gt;">View article</a></p>

&lt;?php endforeach ?&gt;

and i am try using your code :

Code:
&lt;?php foreach ($news as $news_item): ?&gt;

    <h2>&lt;?php echo $news_item['title'] ?&gt;</h2>
    <div id="main">
        &lt;?php echo $news_item['text'] ?&gt;
    </div>
    
    <p>&lt;?php echo anchor(url_title($news_item['slug'].'/'.$news_item['id']),'View'); ?&gt;</p>

&lt;?php endforeach ?&gt;

and this is detail view :
Code:
<div class="container">

&lt;?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
?&gt;


     </div>


this is my model :
Code:
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
  $query = $this->db->get('news');
  return $query->result_array();
}

$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}



display id and title in url Codeigniter - El Forum - 07-15-2013

[eluser]JoostV[/eluser]
In your view, do:
Code:
<p>&lt;?php echo anchor('news/' . $news_item['slug'], 'View article') ?&gt;</p>




display id and title in url Codeigniter - El Forum - 07-15-2013

[eluser]johanriyan[/eluser]
nothing id.

www.example.com/news/12345/example-news

just appear :

www.example.com/news/example-news

how to handle in controller.


display id and title in url Codeigniter - El Forum - 07-15-2013

[eluser]JoostV[/eluser]
The reason I did not include the ID is because the ID is not used in your controller or model. For the code you posted you do not need to include the id.

If you want to include the ID you will need to rewrite the rest of the code:

Controller:
Code:
public function view($id, $slug)
{
  $data['news_item'] = $this->m_admin->get_news($id, $slug);
  // ... rest of the code
}

Model:
Code:
public function get_news($id = 0, $slug = FALSE)
{
  if ($id === 0 && $slug === FALSE)
  {
    $query = $this->db->get('news');
    return $query->result_array();
  }

  $query = $this->db->get_where('news', array('id' => $id, 'slug' => $slug));
  return $query->row_array();
}



display id and title in url Codeigniter - El Forum - 07-15-2013

[eluser]johanriyan[/eluser]
wauuwww...

great.


thks JoostV for fix it.


display id and title in url Codeigniter - El Forum - 07-15-2013

[eluser]johanriyan[/eluser]
thk u master JoostV


display id and title in url Codeigniter - El Forum - 07-15-2013

[eluser]johanriyan[/eluser]
master i want anything url like wordpress :

www.example.com/2013/07/15/show-articel

how to make it?


hehe.... sorry master.





display id and title in url Codeigniter - El Forum - 07-16-2013

[eluser]Pert[/eluser]
Code:
public function view($id = null, $slug = null)
...

This is better (added default values). If you don't add default values to your controller method, following link would throw PHP error.

<b>mydomain.com/view/</b>

Because required method attributes are missing.