简体   繁体   中英

Displaying the blog pagetitle as a url in codeigniter php

I am having a blog page where the blogs are inserted from admin panel and blog title will be inserted into new column as separated by " - " if there are spaces between the page titles.For example if the page title is " welcome to something " then in the database it is will be inserted into two columns. In once column it will be inserted as same and in other column it will be inserted as welcome-to-something.

when clicking on readmore button i need to display in url as (www.example.com/blob/article/welcome-to-something) in this format i need to display the url.

Here is the code:

Controller:

public function index()
    {
        $this->load->model('blogs_model');          
        $data["records2"] = $this->blogs_model->get_all_blogs($config["per_page"], $page);
        $data['mainpage'] = "blog";
        $this->load->view('templates/template',$data);
    }

    public function article()
    {
      $this->load->model('blogs_model');
      $data['records2']= $this->blogs_model->getblogsdata($this->uri->segment(3));                
      $data['mainpage']='blogs';
      $this->load->view('templates/templatess',$data);

    }

Model:

function get_all_blogs()
{
    $this->db->select('B.*');
    $this->db->from('blogs AS B');
    $this->db->where(array('B.status'=>1));
    $this->db->order_by("position", "asc");
    $q = $this->db->get();
    if($q->num_rows()>0)
    {
        return $q->result();
    }
    else 
    {
        return false;
    }
}

function getblogsdata($id)
{
    $this->db->select('blogs.*');       
    $this->db->from('blogs');
    $this->db->where(array('blogs.blog_id'=>$id));
    $q=$this->db->get();        
    if($q->num_rows()>0)
      {
    return $q->result();
        }
    else
    {
    return false;
    }
}

View:

  <div class="col-md-9 blogs"> 
            <?php if(isset($records2) && is_array($records2)):?>
            <?php foreach ($records2 as $r):?>          
                <div class="blog1">
                    <img src="<?php echo base_url();?>admin/images/blogimages/thumbs/<?php echo $r->image_path;?>" class="testimonials1"/>
                    <h3 class="heading1"><?php echo $r->blog_title;?></h3>
                    <div class="blogtext1 read">                        
                        <?php echo $r->description;?>
                    </div>
                    <a href="<?php echo base_url()?>blog/article/<?php echo $r ->blog_id ;?>" class="read_more7" target="_blank" >Read More</a>
                </div>

            <?php endforeach ;endif;?>
            <div class="pagination"><?php echo $links; ?></div> 
            </div>

blogs table

blog_id | blog_title | blogtitle 1 welcome to something welcome-to-something

Model:

function getblogsdata($id,$slug)
{
    $this->db->select('blogs.*');       
    $this->db->from('blogs');
    $this->db->where(array('blogs.blogtitle'=>$id));
    $this->db->where(array('blogs.blogtitle' => $slug));
    $this->db->order_by("ne_views", "asc");         
    $q=$this->db->get();

    if($q->num_rows()>0)
      {
    return $q->result();
        }
    else
    {
    return false;
    }
}

View:

<a href="<?php echo base_url()?>blog/article/<?php echo $r->blogtitle;?>" class="read_more7" target="_blank" >Read More</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM