Codeigniter Tutorials
- What is codeigniter?
- Application_Architecture
- MVC Architecture
- HMVC Architecture
- Codeigniter Configuration
- Remove index.php from url in codeigniter
- MVC Concept
- View
- Alternate PHP Syntax for View Files
- Routing
- Codeigniter URL
- Get Current URL
- Previous page URL get
- Seo Friendly URL
- Slug Create in codeigniter
- What is _remap() function
- Remove controller name from url in codeigniter
- Codeigniter Controller Class
- Class Constructor
- GET $ POST method in Codeigniter
- Models
- Basepath, Apppath, FCPATH
- URI Segment
- Page Redirect
- Helper class
- Custom Helper class
- Form Helper
- Common Helper Functions
- Common Function
- Array Problems
- Call controller in Helper
- Add active class to menu using Helper class
- Custom Library
- Custom Library Example
- when to use get_instance()
- Codeigniter Hook
- how to work inline css in codeigniter
- Custom 404 page
- 404 custom error page
- Create custom config file in codeigniter
- How to set and get config item value
- How to Speed Up CodeIgniter App?
- Codeigniter Functions
- Session
- cookies
- How to Set & Get Tempdata in Codeigniter
- flash messages in Codeigniter
- Flashdata
- Encryption and Decryption In CodeIgniter
- Codeigniter security
- csrf token form security
- Password Hashing
- Form Validation
- Custom Validation
- Registration Form with validation
- Server Side Form Validation
- Validate Select Option Field
- Date Format Validation
- Date Format change in codeigniter
- Date Functions
- DOB Validation
- CI CRUD
- User SignUp
- User Login
- User Logout
- Login Account
- Login form with RememberMe
- Login Form with session
- User change password
- Change Password with Callback Validation to Check Old Password
- Forgot password
- Reset password
- Insert data in database
- Fetch data from database
- Update data in database
- Delete data in database
- File Upload
- Image Upload with resize Image
- Upload Multiple file and images
- Upload Multiple images with CRUD
- File and image update
- Upload Image Using Ajax.
- Email Send
- Email Send Using Email library
- Email Send Using SMTP Gmail
- Notification send
- store data in json format in DB
- Json parse
- Fetch data Using Ajax with Json data
- How to Show data Using Ajax with Json parse
- Get JSON Data from PHP Script using jQuery Ajax
- Insert data Using Ajax
- Submit data Using Ajax with form validation
- How to show data Using Ajax in codeigniter
- Insert & Update Using Ajax
- Registration Form With Validation Using Ajax in codeigniter
- Delete data Using Ajax Confirmation
- Delete All data Using checkbox selection
- Ajax CSRF Token
- Ajax Post
- Ajax serverside form validation
- Contact form using AJAX with form validation
- DataTable Using Ajax dynamically
- DataTables pagination using AJAX with Custom filter
- DataTables AJAX Pagination with Search and Sort in codeigniter
- DataTables in Codeigniter using Ajax
- Ajax Custom Serarch
- Ajax Live Data Search using Jquery PHP MySql
- Ajax Custom Serarch and sorting in datatable
- Dynamic Search Using Ajax
- Autocomplete using jquery ajax
- Jquery Ajax Autocomplete Search using Typeahead
- Dynamic Dependent Dropdown Using Ajax
- Dynamic Dependent Dropdown list Using Ajax
- Dynamic Dependent Dropdown in codeigniter using Ajax
- ajax username/email availability check using JQuery
- Check Email Availability Using Ajax
- Data Load on mouse scroll
- Ajax CI Pagination
- Pagination in codeigniter
- Ajax Codeigniter Pagination
- email exists or not using ajax with json
- CRUD using AJAX With Modal popup in CI
- Add / Show Data on modal popup using Ajax
- Modal popup Validation using Ajax
- Data show on Modal popup Using Ajax
- Add / Remove text field dynamically using jquery ajax
- How to Add/Delete Multiple HTML Rows using JavaScript
- Delete Multiple Rows using Checkbox
- Multiple Checkbox value
- Form submit using jquery Example
- REST & SOAP API
- Multi-Language implementation in CodeIgniter
- How to pass multiple array in view
- Captcha
- create zip file and download
- PhpOffice PhpSpreadsheet Library (Export data in excel sheet)
- data export in excel sheet
- Excel File generate in Codeigniter using PHPExcel
- Dompdf library
- tcpdf library
- Html table to Excel & docs download
- CI Database Query
- Database Query
- SQL Injection Prevention
- Auth Model
- Join Mysql
- Tree View in dropdown option list
- OTP Integration in codeigniter
- curl post
- download file using curl
- Sweet Alert
- Sweet alert Delete & Success
- Log Message in Codeigniter
- Menu & Submenu show dynamically
- Set Default value in input box
- Cron Jobs
- Stored Procedure
- Display Loading Image when AJAX call is in Progress
- Send SMS
- IP Address
- Codeigniter Tutorialspoint
- Website Link
- How To Create Dynamic Xml Sitemap In Codeigniter
- Paypal Payment Integration
- Get Latitude and Longitude From Address in Codeigniter Using google map API
- How To Create Simple Bar Chart In Codeigniter Using AmCharts?
- dynamic Highcharts in Codeigniter
- Barcode in Codeigniter
- Codeigniter Interview Questions
- Project
Home » Codeigniter »
How to create seo firendly url in codeigniter.
File Name :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Posts extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('Auth_model');
$this->load->helper('Common');
$this->load->library('form_validation');
}
public function index(){
$data = array();
//get the posts data
$data['posts'] = $this->Auth_model->getRows(array('limit'=>10));
//load the view
$this->load->view('post_view', $data);
}
public function details($url_slug){
//echo $url_slug;
//exit;
$data = array();
//get the post data
$data['post'] = $this->Auth_model->getRows(array('url_slug'=>$url_slug));
//load the view
$this->load->view('details', $data);
}
public function add(){
$data = array();
$postData = array();
if($this->input->post('submitBtn')){
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('content', 'Content', 'required');
$postData = array(
'title' => strip_tags($this->input->post('title')),
'content' => strip_tags($this->input->post('content'))
);
if($this->form_validation->run() == true) {
/*
* Generate SEO friendly URL
*/
$title = strip_tags($this->input->post('title'));
$titleURL = strtolower(url_title($title));
if(isUrlExists('posts',$titleURL)){
$titleURL = $titleURL.'-'.time();
}
$postData['url_slug'] = $titleURL;
//Insert post data to database
$insert = $this->Auth_model->insert($postData);
if($insert){
$postData = array();
$data['success_msg'] = 'Post data inserted successfully.';
} else {
$data['error_msg'] = 'Some problems occured, please try again.';
}
}
}
$data['post'] = $postData;
//load the view
$this->load->view('add', $data, false);
}
}
post_view
<!DOCTYPE html>
<html lang="en">
<head>
<title>Generate SEO Friendly URL in CodeIgniter</title>
</head>
<body>
<div class="container">
<h1>Post List</h1>
<div class="row">
<ul class="post-list">
<?php if(!empty($posts)): foreach($posts as $post): ?>
<li><a href="<?php echo base_url($post['url_slug']); ?>"><?php echo $post['title']; ?></a></li>
<?php endforeach; else: ?>
<p>Post(s) not available.</p>
<?php endif; ?>
</ul>
</div>
</div>
</body>
</html>
add
<!DOCTYPE html>
<html lang="en">
<head>
<title>Generate SEO Friendly URL in CodeIgniter</title>
</head>
<body>
<div class="container">
<h1>Add Post</h1>
<div class="row">
<?php echo !empty($success_msg)?'<p>'.$success_msg.'</p>':''; ?>
<?php echo !empty($error_msg)?'<p>'.$error_msg.'</p>':''; ?>
<form method="post" action="<?php echo base_url()?>posts/add">
<p>Post Title: <input type="text" name="title" value="<?php echo !empty($post['title'])?$post['title']:''; ?>"></p>
<?php echo form_error('title','<p class="help-block error">','</p>'); ?>
<p>Post Content: <textarea name="content"><?php echo !empty($post['content'])?$post['content']:''; ?></textarea></p>
<?php echo form_error('content','<p class="help-block error">','</p>'); ?>
<input type="submit" name="submitBtn" value="Add">
</form>
</div>
</div>
</body>
</html>
details
<!DOCTYPE html>
<html lang="en">
<head>
<title>Generate SEO Friendly URL in CodeIgniter</title>
</head>
<body>
<div class="container">
<?php echo "hello url";?>
<h1><?php echo $post['title']; ?></h1>
<p><?php echo $post['content']; ?></p>
</div>
</body>
</html>
Add
<?php
defined('BASEPATH') OR exit('no direct script access allowed');
class Add extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('add');
}
}
routes
$route['default_controller'] = 'posts';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['(:any)'] = 'posts/details/$1';
$route['posts/(:any)'] = 'posts/details/$1';
Auth_model
<?php
defined('BASEPATH') OR exit ('no direct script access allowed');
class Auth_model extends CI_Model
{
public function __construct()
{
parent::__construct();
}
function getRows($params = array()){
$this->db->select('*');
$this->db->from('posts');
//set start and limit
if(array_key_exists("start",$params) && array_key_exists("limit",$params)){
$this->db->limit($params['limit'],$params['start']);
}elseif(!array_key_exists("start",$params) && array_key_exists("limit",$params)){
$this->db->limit($params['limit']);
}
if(array_key_exists("url_slug", $params)){
$this->db->where('url_slug', $params['url_slug']);
$query = $this->db->get();
$result = ($query->num_rows() > 0)?$query->row_array():FALSE;
}else{
$query = $this->db->get();
$result = ($query->num_rows() > 0)?$query->result_array():FALSE;
}
//return fetched data
return $result;
}
/*
* Insert post data to table
*/
public function insert($data = array()) {
if(!array_key_exists("created", $data)){
$data['created'] = date("Y-m-d H:i:s");
}
if(!array_key_exists("modified", $data)){
$data['modified'] = date("Y-m-d H:i:s");
}
//insert data
$insert = $this->db->insert('posts', $data);
if($insert){
return $this->db->insert_id();;
}else{
return false;
}
}
}
-- phpMyAdmin SQL Dump
-- version 4.8.3
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Apr 15, 2019 at 09:17 PM
-- Server version: 10.1.37-MariaDB
-- PHP Version: 7.2.12
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `db_test`
--
-- --------------------------------------------------------
--
-- Table structure for table `posts`
--
CREATE TABLE `posts` (
`id` int(11) NOT NULL,
`url_slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`content` text COLLATE utf8_unicode_ci NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1=Active, 0=Inactive'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Dumping data for table `posts`
--
INSERT INTO `posts` (`id`, `url_slug`, `title`, `content`, `created`, `modified`, `status`) VALUES
(1, 'socha-nahi-thaa-takdeer-yahan-layegi', 'socha nahi thaa', 'lovely song', '2019-02-16 00:00:00', '2019-02-16 00:00:00', 1),
(2, 'kabhi-kabhi-kabhi-gum', 'kabhi', 'hjgh gjhkj hj jjh', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 1);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `posts`
--
ALTER TABLE `posts`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `posts`
--
ALTER TABLE `posts`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;