Skip to content
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos

Home » Codeigniter » How to Create Dynamic Xml Sitemap in Codeigniter

How To Create Dynamic Xml Sitemap In Codeigniter

How to Create Dynamic Xml Sitemap in Codeigniter

Posted on June 2, 2018July 3, 2022 By XpertPhp 11 Comments on How to Create Dynamic Xml Sitemap in Codeigniter

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.

Read Also:
How to get last record from the mysql table in codeigniter

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.

Table of Contents

  • Create Sitemap Controller
  • Create XML File.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?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.

1
http://your-project-dir/sitemap.xml

Please follow and like us:
error
fb-share-icon
Tweet
fb-share-icon

Recommended Posts:

  • Codeigniter 4 Import CSV/Excel to MySQL Database
  • create pagination using the codeigniter pagination library
  • Codeigniter 4 CRUD Operation With Ajax Example
  • How To Install Codeigniter 4 Using Manual, Composer, Git
  • Laravel 6 Import Export Excel & Csv file example tutorial
Codeigniter, MySql Tags:Codeigniter sitemap generator, codeigniter tutorial, create sitemap, how to create a sitemap, sitemap example, sitemap generator, sitemap maker, xml sitemap, xml sitemap generator

Post navigation

Previous Post: How to get last record from the mysql table in codeigniter
Next Post: How to Remove Special Character from String in PHP

Latest Posts

  • Laravel Check If Foreach is Empty Example
  • Laravel Check Array Empty in Blade
  • PHP Datatables CRUD Example
  • How to Convert Date and Time from one timezone to another in php
  • how to get current date and time in php
  • Drag and Drop Reorder Items with jQuery, PHP & MySQL
  • Laravel 9 Toastr Notifications Example Tutorial
  • Laravel 9 CRUD Operation Example Using Google Firebase
  • Laravel 9 CKeditor Image Upload With Example
  • Laravel 9 Summernote Image Upload With Example

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tricks jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch Laravel Socialite laravel social login nodejs pagination payment gateway php with mysql react js tutorial rewrite rule send mail validation wysiwyg editor

Copyright © 2018 - 2022,

All Rights Reserved Powered by XpertPhp.com