Welcome Guest, Not a member yet? Register   Sign In
Dynamic Routes
#1

[eluser]Thiago Leao[/eluser]
I needed some help.
I am making a news system, and would like the url appear the title of the news.
How can I do this?

example, www.mypage.com/my-news-for-today

Actually, I do links instead of validating the ID, I would for the title.
It would be something.

thanks guys!
#2

[eluser]Tpojka[/eluser]
I am not quite sure what you are looking for.
I am asuming you want that clicking on:
Code:
http://www.mypage.com/my-news-for-today
you get page with let say 20 newest articles?
Is that case?
#3

[eluser]Thiago Leao[/eluser]
I want to validate the news for the title.
when I click on the news, you see a url with the news!!!

thanks
#4

[eluser]Tpojka[/eluser]
Can you show in what exact form you have URL of news now?
#5

[eluser]Thiago Leao[/eluser]
VIEW

Code:
<li>
                                        <div>
                                            [b]<a href="&lt;?php echo base_url()?&gt;news/visualize/&lt;?php echo $listar_noticias[$j]-&gt;title; ?&gt;">[/b]
                                             &lt;?php
            if($listar_noticias[$j]->imagem_lnk > 0){
                                                 echo img(array('src' => 'assets/uploads/noticias/'.$listar_noticias[$j]->imagem_lnk, 'width' =>'138', 'height' => '78', 'alt' => ''));
            }else{
             echo img(array('src' => 'assets/uploads/noticias/img_padrao.jpg', 'width' =>'138', 'height' => '78', 'alt' => ''));
            }
            ?&gt;
                                                
                                                <span>&lt;?php echo substr($listar_noticias[$j]->texto,0,65) . '...';?&gt;</span>
                                            </a>
                                        </div>      
                                    </li>

CONTROLLER

Code:
function visualizar($title)
{  
  $this->load->model('noticias_model');
  $data['view'] = $this->noticias_model->verNoticia($title);
  
  $this->load->view('visualizar', $data);
}

MODEL

Code:
function verNoticia($title)
  {
   $query = $this->db->get_where('noticias', array('title' => $title));
   return $query->result();
  }

I want to see the url with the title that is in the database!!

understand?
#6

[eluser]CroNiX[/eluser]
Use a wildcard route.
http://ellislab.com/codeigniter/user-gui...uting.html
#7

[eluser]Tpojka[/eluser]
Ok.
This like:
You need one more column in table `noticias` called slug. When making input of news you will make slug input
with your title using CI native function url_title(). You can check it on
URL helper link in user guide. Also, I am thinking about you would need one more CI native function called convert_accented_characters()
that is located in TExt helper
in user guide
. So for now you have stored slug in your DB table. When you calling data from table, you
won't call it by Title reference. You will do that now by slug reference. To avoid uri segments
news/visualize, you will need to make remaping in route.php file (config folder).
So in your case it could be something like:
route.php
Code:
$route['(:any)'] = "news/visualize/$1";
noticias_model.php
Code:
function verNoticia($title)
  {
   $query = $this->db->get_where('noticias', array('slug' => $slug));
   return $query->result();
  }
news.php #controller
Code:
function visualizar($slug)
{  
  $this->load->model('noticias_model');
  $data['view'] = $this->noticias_model->verNoticia($slug);
  
  $this->load->view('visualizar', $data);
}
view file
Code:
<li>
                                        <div>
                                            [b]<a href="&lt;?php echo base_url($listar_noticias[$j]-&gt;title)?&gt;">[/b]
                                             &lt;?php
            if($listar_noticias[$j]->imagem_lnk > 0){
                                                 echo img(array('src' => 'assets/uploads/noticias/'.$listar_noticias[$j]->imagem_lnk, 'width' =>'138', 'height' => '78', 'alt' => ''));
            }else{
             echo img(array('src' => 'assets/uploads/noticias/img_padrao.jpg', 'width' =>'138', 'height' => '78', 'alt' => ''));
            }
            ?&gt;
                                                
                                                <span>&lt;?php echo substr($listar_noticias[$j]->texto,0,65) . '...';?&gt;</span>
                                            </a>
                                        </div>      
                                    </li>
#8

[eluser]Tpojka[/eluser]
Edit:
Server is dissallowing me to edit post in anti-flood kind matter.
In model, argument of method should also be $slug.
And in view link should be base_url($listar_noticias[$j]->slug)

P.S. CroNiX was bit faster. Tongue
#9

[eluser]Thiago Leao[/eluser]
[quote author="CroNiX" date="1382657048"]Use a wildcard route.
http://ellislab.com/codeigniter/user-gui...uting.html[/quote]

Has somehow, without me having to create a new column in the table?
Since I already have enough record in the news!

thanks!
#10

[eluser]Tpojka[/eluser]
It is called DB planning before application is made.
There is no other way.
You can't get Pesca en el otoƱo from pesca-en-el-otono. Machine doesn't know where to put accent, what is capital letter etc.
Code:
function titles2slugs($titles){ //you will select all titles from DB table
    $slug = $array();
    foreach($titles as $title){
      $slug[] = url_title($title, '-', TRUE);
    }
    return $slug;
}
Now when you have $slug array with all slugs, import them into new column. I think it would be easiest from phpmyadmin SQL if available.
Also, it would be smart to make $slug array in localhost where you can increase php script execution time for this loop. Rest is up to you.




Theme © iAndrew 2016 - Forum software by © MyBB