codeigniter validation process for checking same data in one table - php

$sort_order=$this->input->post('sort_order','Sort Of Category','trim|required|max_length[128]|xss_clean|is_unique[categories.sort_order]');
Why is the is_unique function not working here?

It should be for set_rules on your form validation on controller
https://www.codeigniter.com/user_guide/libraries/form_validation.html
$this->form_validation->set_rules('sort_order','Sort Of Category','trim|required|max_length[128]|xss_clean|is_unique[categories.sort_order]');
https://www.codeigniter.com/user_guide/libraries/form_validation.html#setting-validation-rules
https://www.codeigniter.com/user_guide/libraries/form_validation.html#rule-reference
<?php
class Example extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
}
public function index() {
$data['title'] = 'Example';
$this->form_validation->set_rules('sort_order','Sort Of Category','trim|required|max_length[128]|is_unique[categories.sort_order]');
if ($this->form_validation->run() == false) {
$this->load->view('header', $data);
$this->load->view('example', $data);
$this->load->view('footer');
} else {
// You can put your update or insert model function here
redirect('success_controller');
}
}
}

$c_name=$this->input->post('c_name');
$sort_order=$this->input->post('sort_order');
$this->load->library('form_validation');
$this->form_validation->set_rules('c_name','Category Name','trim|required|max_length[128]|xss_clean');
$this->form_validation->set_rules('s_order','Sort Order','Sort Of Category','trim|required|xss_clean|is_unique[categories.sort_order]');

Related

codeigniter URL change

I am creating eCommerce in Codeigniter. Here is the controller file for registration:
class Register extends CI_Controller {
public function index()
{
$this->load->view('common/header');
$this->load->view('register');
$this->load->view('common/footer');
}
public function home(){
$this->load->view('common/header');
$this->load->view('common/slider');
$this->load->view('home');
$this->load->view('common/footer');
}
public function signup(){
$username = $this->input->post("username");
$password = $this->input->post("password");
$email = $this->input->post("email");
$this->form_validation->set_rules('username','User Name','required|is_unique[user.name]|min_length[4]');
$this->form_validation->set_rules('password','Password','required|min_length[5]');
$this->form_validation->set_rules('email','Email address','required|valid_email');
$this->form_validation->set_error_delimiters('<div class="text-danger">', '</div>');
if($this->form_validation->run() == true){
$this->load->model('register_model');
if($this->register_model->register_user($username,$password,$email)){
$this->home();
}else{
$this->index();
}
}else{
$this->index();
}
}
}
On form submit the page is redirecting to the home page but the URL on the browser is (http://localhost:8080/codignitor_projects/shophere/register/signup) But I want to change the URL to this (http://localhost:8080/codignitor_projects/shophere/)
I got your problem. I don't know your CI version. I hope you are using the latest one and I am also giving my answer according to the latest version CI-4.
Please Write
return redirect()->to('index');
or
return $this->response->redirect(base_url('YourControllerName/index'));
instead of
$this->index();
To clearify this problem I would like to give you an example. Let, I have a Test controller and there are two functions such as abc() and def()
class Test extends Controller
{
function abc()
{
echo "Hi ! I am ABC";
}
function def()
{
$this->abc();//URL will be: http://localhost/your_project_name/public/test/def
//return redirect()->to('abc'); //URL will be: http://localhost/your_project_name/public/test/abc
//return $this->response->redirect(base_url('Test/abc')); //URL will be:http://localhost/your_project_name/public/Test/abc
}
}
There are three lines inside the def() function that give you the same result but the URL from first one to others will be different which is commented.

Getting a drop down list from a database in CodeIgniter

I am new to CodeIgniter and I have been trying to populate the drop down list on the view page with data from the database with no success. I tried using the recomendations from this question but the drop down is still empty (display data from database to dropdown CodeIgniter)
Here is my view:
<label>City</label>
<select class="form-control>
<option value="">All</option>
<?php
foreach($groups as $city)
{
echo '<option value="'.$city['cityidd'].'">'.$city['city'].'</option>';
}
?>
</select> <br/>
Here is my controller:
<?php
class Main_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->database();
}
public function index()
{
$this->load->helper('form');
$this->load->view('supplier_add');
}
}
Here is my model:
class Site_model extends CI_Model
{
public function __construct()
{
/* Call the Model constructor */
parent::__construct();
}
function getAllGroups()
{
$query = $this->db->query('SELECT city FROM citys');
return $query->result();
}
}
The table name is "citys" then the corresponding column heads are "cityidd" and "city"
There are several issue found there. Make changes as below
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load_model('Site_model');
$this->load->database();
}
public function index(){
$this->load->helper('form');
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
Finally model
function getAllGroups(){
$query = $this->db->query('SELECT cityidd,city FROM citys');
return $query->result_array();
}
and now test
First change your SELECT query as
SELECT cityidd, city FROM citys
In the index() of controller change the code as
public function index()
{
$this->load->helper('form');
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
you have to call your model method and pass it to view in the controller, code:
public function index()
{
$this->load->helper('form');
$this->load->model('site_model');
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
if you are working on linux dont forget upper and lowercase names!
class Main_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->database();
$this->load->model('Site_model');
}
public function index()
{
$this->load->helper('form');
$data['groups']=$this->Site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
}
Here is my model:
class Site_model extends CI_Model
{
public function __construct()
{
/* Call the Model constructor */
parent::__construct();
}
function getAllGroups()
{
$query = $this->db->query('SELECT * FROM citys');
return $query->result();
}
}
Try like this
Make these changes
IN Model convert data to array as you are using it as array in view so change to this
function getAllGroups()
{
$query = $this->db->query('SELECT * FROM citys');
return $query->result_array();
}
In Controller
public function index()
{
$this->load->helper('form');
$this->load->model('site_model');
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
You are not loading model load the Model
for example:-
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('Site_model')
$this->load->database();
}
public function index()
{
$this->load->helper('form');
$data['record']=$this->Site_model->getAllGroups()
$this->load->view('supplier_add', $data);
}
}
Model:-
class Site_model extends CI_Model
{
public function __construct()
{
/* Call the Model constructor */
parent::__construct();
}
function getAllGroups()
{
$query = $this->db->query('SELECT city FROM citys');
return $query->result();
}
}

Message: Undefined property: News::$news_model in Codeigniter

I am learning Codeigniter from Codeigniter mannual step by step. Therefore i used as it is code from the manual.
The Controller class is :
<?php
class News extends CI_Controller{
public function _construct()
{
parent::_construct();
$this->load->model('news_model');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header',$data);
$this->load->view('news/index',$data);
$this->load->view('templates/footer');
}
?>
The Model Class is:
class News_model extends CI_Model{
public function _construct()
{
$this->load->database();
}
public function get_news($slug = FALSE)
{
if($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news',array('slug'=>$slug));
return $query->row_array();
}
?>
Please help me to fix this error. I've Tried every possible solution on internet but couldn't find any mistake.
CodeIgniter uses __construct with two _.
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
The above code should work.
As a side note, you can call your models like this:
$this->load->model('news_model', 'news');
and then you can call it like this:
$this->news->get_news();
But your method works fine, just makes it a bit easier.

CodeIgniter, Call to a member function post() on a non-object

i am new in CodeIgniter (v.2.1.4). I have a problem with getting post from model.
I have a contoller:
class Login extends CI_Controller{
public function index(){
$data['main_view'] = 'login_form';
$this->load->view('login/include/template', $data);
}
public function validate(){
$this->load->library('form_validation');
$this->form_validation->set_rules('user', 'Име', 'trim|required|min_length[3]');
$this->form_validation->set_rules('pass', 'Парола', 'trim|required|min_length[4]');
if($this->form_validation->run()){
$this->load->model('users_model');
if($this->users_model->validate_login($username, $password)){
//valid user
}
else{
//not vlaid user
}
}
else{
$this->index();
}
}
}
and a model:
class Users_model extends CI_Model{
function __construct() {
parent::__construct();
}
function validate_login(){
$username = $this->input->post('user');
$password = md5($this->input->post('pass'));
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get('users');
if($query->num_rows() == 1){
return TRUE;
}
else return FALSE;
}
}
When send via form (post) valid pair (username and password) nothing happens, but in apache2 log appears this:
PHP Fatal error: Call to a member function where() on a non-object in /var/www/ci/application/models/users_model.php
What is wrong?
Add a constructor to the Login class
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
}
}
Do you have database loaded? You need to load the database first.
You can either add it to /config/autoload.php to autoload database function:
$autoload['libraries'] = array('database');
Or call on demand like this:
$this->load->database();
More details here
Do Not Use post in model instead use this in controller like this
controller login.php
public function validate(){
$this->load->library('form_validation');
$this->form_validation->set_rules('user', 'Име', 'trim|required|min_length[3]');
$this->form_validation->set_rules('pass', 'Парола', 'trim|required|min_length[4]');
if($this->form_validation->run()){
$this->load->model('users_model');
$post = $this->input->post();
$data['username'] = $post['username'];
$data['password'] = md5($post['password']);
if($this->users_model->validate_login($data)){
//valid user
}else{
//not vlaid user
}
}
else{
$this->index();
}
}
Model
function validate_login($where){
$this->db->where($where);
$query = $this->db->get('users');
if($query->num_rows() == 1){
return TRUE;
}
return FALSE;
}
This should work. Remember calling post somehow doesn't work in model. I dont exactly know they reason.

How to use global variable in PHP Codeigniter

I have implemented the login logic in an MVC application; I want to see if the user has filled the username and passowrd incorrectly and if so I want to show a notifictaion in the view; so I'm passing this information via $data['er']; but for some reason it is not catching this data:
Please let me know if my question is clear or not; and if any clarification is needed, please let me know which part is ambiguous
My code:
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
$GLOBALS['er'] = False;
}
public function index() {
$data['er']=$GLOBALS['er'];
$data['main_content'] = 'login_form';
$this->load->view('includes/template', $data);
}
public function validate_credentials() {
$this->load->model('user_model');
$query = $this->user_model->validate();
if ($query) {
$data = array(
'username' => $this->input->post('username'),
);
$this->session->set_userdata($data);
redirect('project/members_area');
} else {
$GLOBALS['er'] = TRUE;
$this->index();
}
}
}
Don't use GLOBALS you can just use a private variable in your class.
Create the variable above your __construct function like private $er
In your __contruct function set the default value
Set and get in your public function using $this->er
Implemented in your code:
class Login extends CI_Controller {
private $er;
public function __construct() {
parent::__construct();
$this->er = FALSE;
}
public function index() {
$data['er']= $this->er;
$data['main_content'] = 'login_form';
$this->load->view('includes/template', $data);
}
public function validate_credentials() {
$this->load->model('user_model');
$query = $this->user_model->validate();
if ($query) {
$data = array(
'username' => $this->input->post('username'),
);
$this->session->set_userdata($data);
redirect('pmpBulletin/members_area');
//die(here);
} else {
$this->er = TRUE;
$this->index();
}
}
}

Categories