public function set_news($id = 0)
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$name=$_FILES["myimage"]["name"];
$folder="./uploads/";
$imageFileType = strtolower(pathinfo($name,PATHINFO_EXTENSION));
$extensions_arr = array("jpg","jpeg","png");
if( in_array($imageFileType, $extensions_arr) )
{
$data = array(
'title' => $this->input->post('title'), // $this->db->escape($this->input->post('title'))
'date' => $this->input->post('date'),
'slug' => $slug,
'text' => $this->input->post('text'),
'myimage' => $name,
'user_id' => $this->input->post('user_id'),
);
move_uploaded_file($_FILES["myimage"]["tmp_name"], "$folder".$_FILES["myimage"]["name"]);
}
// else
// {
// echo "<script>alert('Image Error');window.history.back(); </script>";
// }
if ($id == 0) {
//$this->db->query('YOUR QUERY HERE');
return $this->db->insert('news', $data);
} else {
$this->db->where('id', $id);
return $this->db->update('news', $data);
}
}
this is my controller file
on uploading image it showing error as follows
A PHP Error was encountered
Severity: Notice
Message: Undefined index: myimage
Filename: models/News_model.php
Line Number: 48
Backtrace:
File: C:\xampp\htdocs\web\codeigniter\application\models\News_model.php
Line: 48
Function: _error_handler
File: C:\xampp\htdocs\web\codeigniter\application\controllers\News.php
Line: 123
Function: set_news
File: C:\xampp\htdocs\web\codeigniter\index.php
Line: 315
Function: require_once
Seems you missed to set the input name as "myimage". Here is the complete codeigniter image upload code(As Mention on their Documentation).
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
Controller Code:
class Upload extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$uploadData= array(
'title' => TITLE,
'date' => date("Y-m-d H:i:s"),
'slug' => SLUG,
'text' => TEXT,
'myimage' => FILE_NAME,
'user_id' => USER_ID,
);
$this->db->insert('mytable', $uploadData);
$this->load->view('upload_success', $data);
}
}
}
Related
i create image module and i edit image more then 1mb then can not show errormsg.
i used codigniter fremwork.
controller:
public function edit($id) {
$this->edit_status_check($id);
$this->form_validation->set_rules('agent_name', 'Agent Name', 'required');
$this->form_validation->set_rules('mobile', 'Mobile No.', 'required');
$this->form_validation->set_rules('agent_vehicle', 'Agent Vehicle', 'required');
if ($this->form_validation->run() == FALSE) {
$data = array(
'page_title' => 'Edit Agent',
'page_name' => 'agent/edit',
'result' => $this->agent_model->select_id($id),
'result_vehicle' => $this->vehicle_model->list_all(),
'error' => validation_errors(),
'id' => $id
);
$this->load->view('template', $data);
} else {
$config['upload_path'] = '../uploads/agent/';
$config['allowed_types'] = 'jpg|jpeg';
$config['encrypt_name'] = TRUE;
$config['max_size'] = 1000; // 1 mb
$this->load->library('upload', $config);
if (!empty($_FILES['agent_image']['name'])) {
if ($this->upload->do_upload('agent_image')) {
$_POST['agent_img_url'] = 'uploads/agent/' . $this->upload->data('file_name');
} else {
$data = array(
'page_title' => 'Edit Agent',
'page_name' => 'agent/edit',
'result' => $this->agent_model->select_id($id),
'result_vehicle' => $this->vehicle_model->list_all(),
'error' => $this->upload->display_errors(),
'id' => $id
);
$this->load->view('template', $data);
}
}
$this->agent_model->update($_POST, $id);
alert('Update', $_POST['agent_name']);
redirect('agent');
}
}
Model:
public function update($data, $id) {
$updatedata = array(
'name' => $data['agent_name'],
'mobile' => $data['mobile'],
'password' => sha1($data['password']),
'vehicle' => $data['agent_vehicle'],
'address' => $data['agent_address'],
'category' => $data['category'],
'created_on' => date('Y-m-d h:i:sa')
);
if (!empty($data['agent_img_url'])) {
$updatedata['img_url'] = $data['agent_img_url'];
}
$this->db->where('id', $id);
$this->db->update('agent', $updatedata);
}
View:
<div class="form-group">
<img src="/<?= $result['img_url']; ?>" class="img-responsive" name="old_agent_image" width="133" height="100">
</div>
<div class="form-group">
<label>Agent Image</label>
<input type="file" name="agent_image">
</div>
MY question: I edit image for particular user then image uploaded,but if image size more then 1mb ,then image can not upload and display error message.
so my question how to show errormsg.
$uploaded = $this->upload->do_upload('file'); //'file' is input field name
if($uploaded) {
$upload_data = $this->upload->data();
// do database stuff
} else {
$data['errors'] = array("error" => $this->upload->display_errors());
}
now i'm trying user images(user_pic,background_image) upoload.
but my codes not working.
should i be doing something different? Not really sure what's going wrong.
Here my html code
<form action="<?=base_url();?>edit/up_profile/" method="post" enctype="multipart/form-data">
<input type="file" name="pic_file">
<input type="file" name="pic_bgfile" />
<button type="submit">
my controller code is
if($this->form_validation->run() === false){
}else{
$config = array(
'upload_path' => "./images/u/photo",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,
'max_size' => "2048000",
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload',$config);
if($this->upload->do_upload('pic_file')){
$data['profile_image'] = $this->upload->data();
}else if($this->upload->do_upload('pic_bgfile')){
$data['pic_bgfile'] = $this->upload->data();
}
$this->load->model('user_model');
$data = array(
'user_id' => $this->session->userdata('user_id'),
'info_tit' => $this->input->post('info_tit'),
'scope' => $this->input->post('scope')
);
$this->user_model->p_update($data);
//redirect
redirect('/edit/profile/', 'location', 301);
}
You should use CodeIgniter's own upload class instead of using html code.
In the view, or rather, the .html file:
<?php
echo form_open_multipart('user/upload_picture');
?>
<input type="file" name="userfile" />
<br /><br />
<input type="submit" value="submit" />
</form>
Noted that in 'user/upload_picture', 'user' is the name of the controller, 'upload_picture' is the method in 'user'.
In the 'upload_picture' method:
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg';
$config['max_size'] = 0;
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['file_name'] = 'Apink.jpg';
$this->load->library('upload', $config);
$this->upload->do_upload();
The View :
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?> <!-- Error Message will show up here -->
<?php echo form_open_multipart('upload_controller/do_upload');?>
<?php echo "<input type='file' name='userfile' size='20' />"; ?>
<?php echo "<input type='submit' name='submit' value='upload' /> ";?>
<?php echo "</form>"?>
</body>
</html>
The Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Upload_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function file_view(){
$this->load->view('file_view', array('error' => ' ' ));
}
public function do_upload(){
$config = array(
'upload_path' => "./uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,
'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
if($this->upload->do_upload())
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success',$data);
}
else
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('file_view', $error);
}
}
}
?>
Note : You can change preferences depending upon the file you wish to upload.
The Page to show uploaded file:
<html>
<head>
<title>Upload File Specification</title>
</head>
<body>
<h3>Your file was successfully uploaded!</h3>
<!-- Uploaded file specification will show up here -->
<ul>
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
<p><?php echo anchor('upload_controller/file_view', 'Upload Another File!'); ?></p>
</body>
</html>
You can use clear function after first image upload
unset($config);
$this->image_lib->clear();
See CI Manual
https://codeigniter.com/user_guide/libraries/image_lib.html
I am working on a project where volunteers have to fill in registration form.
I want volunteers to be able to upload a profile image and save image name to database as well as the post data on the same form.
How can I achieve that?
In the success part of upload form could do a if statement like below for update
Update
$user_info = $this->get_user();
if ($user_info) {
$image_info = $this->upload->data();
$data = array(
'username' => $user_info['username'],
'image' => $image_info['file_name']
);
// Only updates password if post value true
if(isset($_POST['password'])) {
$data = array(
'password' => $this->input->post('password');
);
}
$this->db->where('id', $user_info['id']);
$this->db->update('tablename', $data);
}
Else In the success part of upload form could do insert like below
Insert
$image_info = $this->upload->data();
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password')
'image' => $image_info['file_name']
);
$this->db->insert('tablename', $data);
Upload Controller
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0'; // Unlimited
$config['max_width'] = '0'; // Unlimited
$config['max_height'] = '0'; // Unlimited
$this->load->library('upload', $config);
// Alternately you can set preferences by calling the initialize function. Useful if you auto-load the class:
$this->upload->initialize($config);
$input_name = "userfile";
if ( ! $this->upload->do_upload($input_name))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$user_info = $this->get_user();
if ($user_info) {
$image_info = $this->upload_data();
$data = array(
'username' => $user_info['username'],
'image' => $image_info['file_name']
);
$this->db->where('id', $user_info['id']);
$this->db->update('tablename', $data);
}
$this->load->view('upload_success', $data);
}
}
public function get_user() {
// your user model data
}
}
?>
View
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="text" name="name"/>
<input type="password" name="password"/>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
Upload library
Codeigniter 2 http://www.codeigniter.com/userguide2/libraries/file_uploading.html
Codeigniter 3 http://www.codeigniter.com/user_guide/libraries/file_uploading.html
your question is not clear ,
provide ,view ,controller as well.
anyways see this post it will help you.
click here
or
use <?php echo form_open_multipart('yourcontroler/add');?>
in your view file when dealing with image and text data.
in controller ,the best practice is to define two function one for image upload the other to validate ,and filter data.Like
public function do_upload() {
$path = '_assets/images/';
$fileName = 'userpic.png';
$config = array(
'allowed_types' => 'jpg|png|gif',
'upload_path' => $path,
'max_size' => '200',
'overwrite' => true,
'file_name' => $fileName,
'max_height' => "300",
'max_width' => "300",
);
$this->load->library('upload', $config);
if (!$this->upload->do_upload('logo')) {
echo "error ";
die;
} else {
$imageData = $this->upload->data();
}
}
and for data storing use simple function you usually do with form input.
I had looked for many examples about it and I did something as I can do.
I will build a registration form. There will be five text fields and also a photo upload section.
what I did
The text fields are stored in the database. In here only one field is shown. (successfully)
The photo is stored in the folder after the form submitted (successfuly)
I need you to help me
The name of the photo is not stored in the database. it is stored on the folder.
The codes below.
Model
public function registration($post)
{
$this->db->insert('registration', $post);
}
Controller
function do_upload()
{
$post = $this->input->post();
$this->form_validation->set_rules('Name','Name','trim|required|xss_clean');
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('example/registration_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$result = $this->register_model->registration($post);
$this->load->view('upload_success', $data, $result);
}
}
View
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" />
<br />
<input type="text" name="name" class="form-control" placeholder="Your name">
<input type="submit" value="upload" />
</form>
It is photo_name on the database. It will be written on controller or model? How do I do in basically?
You need to use a variable for $this->do_upload->data() if you would like to insert content into database.
Example: $data_file = $this->do_upload->data();
Example: $data_file['file_name']
$data = array(
'file_name' => $data_file['file_name'],
'file_type' => $data_file['file_type'],
'full_path' => $data_file['full_path'],
'raw_name' => $data_file['raw_name'],
'orig_name' => $data_file['orig_name'],
'client_name' => $data_file['client_name'],
'file_ext' => $data_file['file_ext'],
'file_size' => $data_file['file_size'],
'is_image' => $data_file['is_image'],
'image_width' => $data_file['image_width'],
'image_height' => $data_file['image_height'],
'image_type' => $data_file['image_type'],
'image_size_str' => $data_file['image_size_str']
);
$this->db->where('whatever', $whatever);
$this->db->update('tablename', $data);
Or
$data = array(
'file_name' => $data_file['file_name'],
'file_type' => $data_file['file_type'],
'full_path' => $data_file['full_path'],
'raw_name' => $data_file['raw_name'],
'orig_name' => $data_file['orig_name'],
'client_name' => $data_file['client_name'],
'file_ext' => $data_file['file_ext'],
'file_size' => $data_file['file_size'],
'is_image' => $data_file['is_image'],
'image_width' => $data_file['image_width'],
'image_height' => $data_file['image_height'],
'image_type' => $data_file['image_type'],
'image_size_str' => $data_file['image_size_str']
);
$this->db->insert('tablename', $data);
I have a form for insert pages on the database,the trouble is that I can't detected in form if a URL is duplicated in my database,because I should to configure a callback function.The error is only on the callback function because I can insert,delete and create pages with this form.
This is my Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Create_Pages extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('url');
/*$this->load->library('pagination');*/
$this->load->model('Create_Pages_Model');
}
public function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
}
else
{
$this->load->view('view_error');
}
}
public function add_new_page()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
$this->load->view('view_admin_content_panel', $data);
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('Create_Pages_Model');
$data_pages = array(
'TitlePage' => $this->input->post('TitlePage'),
'SectionPage' => $this->input->post('SectionPage'),
'CategoryPage' => $this->input->post('CategoryPage'),
'NamePage' => $this->input->post('NamePage'),
'DescriptionPage' => $this->input->post('DescriptionPage'),
'BodyPage' => $this->input->post('BodyPage')
);
if($this->input->post())
{
$this->form_validation->set_rules('TitlePage','Title of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('SectionPage','Section of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('CategoryPage','Category of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('NamePage','Name of the Page','required|min_length[5]|callback_check_namepage[$str]|xss_clean|url_title');
$this->form_validation->set_rules('DescriptionPage','Description of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('BodyPage','Content of the Page','required|min_length[5]');
if($this->form_validation->run() == TRUE)
{
$page_inserted = $this->Create_Pages_Model->addPage();
redirect('dashboard', 'refresh');
}
else
{
$this->load->view('admin/user/add_new_page_view');
}
function check_namepage($str)
{
$PageName = $this->db->where('NamePage', $NamePage);
$query = $this->db->select('pages', array('NamePage' => $NamePage));
if($query == $PageName){
$this->form_validation->set_message('check_namepage', '%Should be Unique Name Page because we can\'t duplicate the URL of the page');
return FALSE;
}
elseif($NamePage != $query)
{
return TRUE;
}
}
}
else
{
$this->load->view('admin/user/add_new_page_view');
}
}
else
{
$this->load->view('view_error');
}
}
}
This is my model code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Create_Pages_Model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('view_error');
}
public function addPage(){
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
$this->load->database();
$add_page = $this->input->post();
unset($add_page['insert']);
$this->db->insert('pages',$add_page);
return $this->db->insert_id();
}
else
{
$this->load->view('view_error');
}
}
}
And this this is my my view page,for insert the pages into the database:
<body>
<div id="main">
<div class="container">
<div class="page-header">
<h1>Create a New Page</h1>
</div>
<div class="row">
<div class="col-sm-12">
<p>Please enter your details below to form.</p>
<?php
$title_page = array(
'name' => 'TitlePage',
'id' => 'TitlePage',
'maxlength' => '490',
'size' => '90',
'type' => 'text',
'style' => 'position:relative;min-height:40px;width:100%;',
'value' => set_value('TitlePage'),
);
$section_page = array(
'name' => 'SectionPage',
'id' => 'SectionPage',
'maxlength' => '390',
'type' => 'text',
'size' => '90',
'style' => 'position:relative;min-height:40px;width:100%;',
'value' => set_value('SectionPage'),
);
$category_page = array(
'name' => 'CategoryPage',
'id' => 'CategoryPage',
'maxlength' => '290',
'type' => 'text',
'size' => '90',
'style' => 'position:relative;min-height:40px;width:100%;',
'value' => set_value('CategoryPage'),
);
$name_page = array(
'name' => 'NamePage',
'id' => 'NamePage',
'maxlength' => '290',
'type' => 'text',
'size' => '90',
'style' => 'position:relative;min-height:40px;width:100%;',
'value' => set_value('NamePage'),
);
$description_page = array(
'name' => 'DescriptionPage',
'id' => 'DescriptionPage',
'maxlength' => '290',
'type' => 'text',
'size' => '90',
'style' => 'position:relative;min-height:40px;width:100%;',
'value' => set_value('DescriptionPage'),
);
$body_page = array(
'name' => 'BodyPage',
'id' => 'BodyPage',
'maxlength' => '290',
'type' => 'text',
'size' => '90',
'style' => 'position:relative;min-height:320px;width:100%;',
'value' => set_value('BodyPage'),
'rows' => '15',
'cols' => '80',
);
?><?php echo form_open();?>
<?php echo form_label('Title of the Page:','lbl_titlepage');?><br/>
<?php echo form_input($title_page);?><?php echo form_error('TitlePage'); ?><br/>
<?php echo form_label('Section of the Page:','lbl_sectionpage');?><br/>
<?php echo form_input($section_page);?><?php echo form_error('SectionPage'); ?><br/>
<?php echo form_label('Category of the Page:','lbl_categorypage');?><br/>
<?php echo form_input($category_page);?><?php echo form_error('CategoryPage'); ?><br/>
<?php echo form_label('Name of the Page (URL):','lbl_namepage');?><br/>
<?php echo form_input($name_page);?><?php echo form_error('NamePage'); ?><br/>
<?php echo form_label('Description of the Page:','lbl_descriptionpage');?><br/>
<?php echo form_input($description_page);?><?php echo form_error('DescriptionPage'); ?><br/>
<?php echo form_label('Contentent of the Page:','lbl_BodyPage');?><br/>
<?php echo form_textarea($body_page);?><?php echo form_error('BodyPage'); ?><br/><br/>
<?php echo form_submit('insert','Create Page')?><br/>
<?php echo form_close();?>
</div>
</div>
</div>
</div>
</body>
</html>
And this is my database:
CREATE TABLE `pages` (
`ID_Page` int(11) NOT NULL,
`TitlePage` varchar(255) COLLATE utf8_spanish_ci NOT NULL,
`SectionPage` varchar(255) COLLATE utf8_spanish_ci NOT NULL,
`CategoryPage` varchar(255) COLLATE utf8_spanish_ci NOT NULL,
`NamePage` varchar(255) COLLATE utf8_spanish_ci NOT NULL,
`BodyPage` varchar(255) COLLATE utf8_spanish_ci NOT NULL,
`DescriptionPage` varchar(255) COLLATE utf8_spanish_ci NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;
ALTER TABLE `pages`
ADD PRIMARY KEY (`ID_Page`);
ALTER TABLE `pages`
MODIFY `ID_Page` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=10;
Move check_namepage() method outside of add_new_page() method.
and you do not need to pass $str in callback_check_namepage[$str]. It should be just callback_check_namepage because CI will automatically pass the user provided field data to the callback function. So, in the callback check_namepage($str), $str will be available automatically.
So, your Controller will be like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Create_Pages extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('url');
/*$this->load->library('pagination');*/
$this->load->model('Create_Pages_Model');
}
public function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
}
else
{
$this->load->view('view_error');
}
}
public function add_new_page()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
$this->load->view('view_admin_content_panel', $data);
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('Create_Pages_Model');
$data_pages = array(
'TitlePage' => $this->input->post('TitlePage'),
'SectionPage' => $this->input->post('SectionPage'),
'CategoryPage' => $this->input->post('CategoryPage'),
'NamePage' => $this->input->post('NamePage'),
'DescriptionPage' => $this->input->post('DescriptionPage'),
'BodyPage' => $this->input->post('BodyPage')
);
if($this->input->post())
{
$this->form_validation->set_rules('TitlePage','Title of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('SectionPage','Section of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('CategoryPage','Category of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('NamePage','Name of the Page','required|min_length[5]|callback_check_namepage|xss_clean|url_title'); // [$str] is removed
$this->form_validation->set_rules('DescriptionPage','Description of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('BodyPage','Content of the Page','required|min_length[5]');
if($this->form_validation->run() == TRUE)
{
// moved below
$page_inserted = $this->Create_Pages_Model->addPage();
redirect('dashboard', 'refresh');
}
else
{
$this->load->view('admin/user/add_new_page_view');
}
}
else
{
$this->load->view('admin/user/add_new_page_view');
}
}
else
{
$this->load->view('view_error');
}
}
private function check_namepage($str)
{
// you have error here, solve it
$PageName = $this->db->where('NamePage', $NamePage);
$query = $this->db->select('pages', array('NamePage' => $NamePage));
if($query == $PageName){
$this->form_validation->set_message('check_namepage', '%Should be Unique Name Page because we can\'t duplicate the URL of the page');
return FALSE;
}
elseif($NamePage != $query)
{
return TRUE;
}
}
}