Best Jquery ,javascript, HTML5 CSS3 tutorial with jquery examples, php script , demos for beginners, web designers and web developers.

Jquery

Jquery Training

Jquery training blog

Make dynamic sitemap in codeigniter

How to make dynamic sitemap in codeigniter We need sitemap for our website to tell the search engines about the content that we have on our website and how often it is updated. XML Sitemaps are two types. 1) Index Sitemap 2) URL Sitemap There are several ways to generates sitemap. We can make our Continue Reading


How to make dynamic sitemap in codeigniter

We need sitemap for our website to tell the search engines about the content that we have on our website and how often it is updated.

XML Sitemaps are two types.

1) Index Sitemap
2) URL Sitemap

dynamic sitemap in codeigniter

There are several ways to generates sitemap. We can make our sitemap just writing url’s manuaaly in xml file.
Sometime we need to create dynamic sitemap mainly for dynamic web sites
where so many links and number of links in unknown.

A tipical example of sitemap

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>www.mysite.com</loc>
<priority>1.0</priority>
</url>
<url>
<loc>www.mysite.com/moniter</loc>
<lastmod>2017-08-02</lastmod>
<changefreq>monthly</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>www.mysite.com/printer</loc>
<lastmod>2017-08-02</lastmod>
<changefreq>monthly</changefreq>
<priority>0.5</priority>
</url>

</urlset>

Here we will learn how to create dynamic sitemap in codeigniter.

 

The codeigniter model for our sitemap

<?php

class Sitemap_model extends CI_Model{

public function __construct(){

}
public function getCategories(){

$query=$this->db->get('category_table');
return $query->result();
}
}
?>

The codeigniter  controller for our sitemap

<?php

Class Sitemap extends CI_Controller {

public function __construct(){
parent::__construct();
$this->load->model('sitemap_model');
}

function sitemap()
{

$data['categories'] = $this->sitemap_model->getCategories();
$this->load->view("sitemap-view",$data);
}
}

?>

The codeigniter  view for our sitemap

<?php echo'<?xml version="1.0" encoding="UTF-8" ?>' ?>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc><?php echo base_url();?></loc>
<priority>1.0</priority>
</url>


<?php foreach($categories as $url) { ?>
<url>
<loc><?php echo base_url()."/".$url->category_name?></loc>
<lastmod><?php echo date('Y-m-d');?></lastmod>
<changefreq>monthly</changefreq>
<priority>0.5</priority>
</url>
<?php } ?>

</urlset>

Now we have to change the route to use the sitemap as the sitemap.xml extension

Go to application and then in config folder. Now open the route.php file in text editor or any other editor
Place the following code

[‘sitemap.xml’]=’sitemap’;

Note: All the URLs must use the same protocol which are listed in the Sitemap and the sitemap must reside on the same host as the Sitemap. The sitemap must be placed in the root directory of web server.

If any issue or suggestion please do comment in the comment box below.

Please follow and like us:






About the author

Joydeb Choudhury holds a Master's degree in Computer Application from I.G.N.O.U. After working as a web developer in various companies he has started bloging in 2016.

Recent Posts of blog

Downalod Free Php Scripts