How to Create Dynamic Xml Sitemap in Codeigniter – onlinecode

How to Create Dynamic Xml Sitemap in Codeigniter – onlinecode

In this post we will give you information about How to Create Dynamic Xml Sitemap in Codeigniter – onlinecode. Hear we will give you detail about How to Create Dynamic Xml Sitemap in Codeigniter – onlinecodeAnd how to use it also give you demo for it if it is necessary.

A sitemap is described navigate of the website. users can easily search to navigate the site by sitemap. it is creating in the XML coding.

We have given below example of how to create dynamic XML sitemap using Codeigniter. Add the below code in your controller file and Get the data from the database you want to create sitemap URL and pass it in the view file.

So you can follow below steps.

1. Create a Sitemap controller file in your project folder.

Add the below code in your controller file and Get the data from the database you want to create sitemap URL and pass it in the view file.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');'

class Sitemap extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    public function index()
    {
		$que = 'SELECT * FROM product';
        $arrData['product_detail'] = $this->db->query($que)->result_array();
		
		$this->load->view('sitemap', $arrData);
    }

}
?>

 

2. Create sitemap view file in your project folder.

Add bellow code in this file. in this bellow code in product name wise create dynamic xml sitemap and when this code run at that time sitemap.xml file automatically uploaded on your root directory of project folder.

<?php

$xmlString = '<?xml version="1.0" encoding="UTF-8"?>
    <urlset
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
	xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
	xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
    <loc>http://www.example.com</loc>
</url>';
foreach ($product_detail as $pd) {
    $xmlString .=   '<url>';
	$xmlString .=  '<loc>'.base_url('product/'.$pd['name'].'</loc>';
    $xmlString .=  '</url>';
}

$xmlString .= '</urlset>';

$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xmlString);
if($dom->save($_SERVER["DOCUMENT_ROOT"].'/sitemap.xml')){
    echo "<h2>Site Map Created SuccessFully</h2>";
}else{
    echo "<h2>Site Map Created Failed</h2>";
}
?>

Please follow and like us:

Hope this code and post will helped you for implement How to Create Dynamic Xml Sitemap in Codeigniter – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  36  =  43

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US