In this tutorial, we will explain to you how to create dynamic xml sitemap in codeigniter. It helps us to we can easily submit XML sitemap into the google console. so our website can easily rank on google.
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 an example of how to generate a 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 the below steps.
Create Sitemap Controller
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.
application/controllers/Sitemap.php
<?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); } } ?>
Create XML File.
Add the below code in this file. in this below code in the product name wise create a dynamic XML sitemap and when this code run at that time sitemap.xml file is automatically uploaded to your root directory of the project folder.
application/views/sitemap.php
<?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>"; } ?>
you can run on following URL for submit XML sitemap into the google console.
http://your-project-dir/sitemap.xml
Very interesting subject, thank you for posting.
I just like the valuable information you provide for your articles. I will bookmark your blog and check again here frequently. I’m quite certain I’ll be informed many new stuff right right here! Good luck for the next!
Fantastic beat ! I wish to apprentice at the same time as you amend your site, how could i subscribe for a weblog website? The account helped me a appropriate deal. I had been tiny bit familiar of this your broadcast provided vivid clear concept
Google’da en üst sıralara Dopinger SEO ekibiyle koşun…
web site indexletme.
Hi to all, how is everything, I think every one is getting more from this website, and your views are good in support of new viewers.
Hi there, just turned into alert to your blog via Google, and found that it is truly informative. I am gonna be careful for brussels. I will appreciate if you happen to proceed this in future. A lot of folks can be benefited out of your writing. Cheers!
I have been browsing on-line more than three hours nowadays, but I
never found any fascinating article like yours. It’s lovely value sufficient for me.
In my view, if all web owners and bloggers made excellent
content material as you probably did, the internet shall be much more useful than ever before.
It’s fantastic that you are getting thoughts from this article as well as from our discussion made at this place.
Like!! I blog quite often and I genuinely thank you for your information. The article has truly peaked my interest.
It is really a nice and useful piece of information. I am glad that you shared this helpful information with us. Please keep us informed like this. Thank you for sharing.