Codeigniter Form Dropdown Error After Validation - php

I'm trying to adapt the form_dropdown to my form in codeigniter. When i'm adding a new page, it doesn't show any errors in parent dropdown section, but after form validation, for instance if user forgets to enter the title label, if its empty or, after validation it gives foreach error in the dropdown section.
I've read that, the second variable must be array and so on for the form_dropdown. But mine is array and not giving any errors in the page before validation. So i can't figure the problem out.
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: helpers/form_helper.php
Line Number: 331
MY_Controller :
class MY_Controller extends CI_Controller{
public $data = array();
function __construct(){
parent::__construct();
}
}
Admin Controller :
class Admin_Controller extends MY_Controller{
function __construct(){
parent::__construct();
$this->data['meta_title'] = 'My Website Control Panel';
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters(' <div class=" alert alert-danger alert-block" ><span class="glyphicon glyphicon glyphicon-minus-sign"></span> ', '<span class="close" data-dismiss="alert">×</span></div>');
$this->load->library('session');
$this->load->model('user_m');
$exceptions = array('admin/user/login', 'admin/user/logout');
if(in_array(uri_string(),$exceptions) == FALSE){
if($this->user_m->loggedin() == FALSE){
redirect('admin/user/login');
}
}
}
}
Controller :
class Page extends Admin_Controller{
public function __construct(){
parent::__construct();
$this->load->model('page_m');
}
public function index(){
$this->data['pages'] = $this->page_m->get_with_parent();
$this->data['subview'] = "admin/page/index";
$this->load->view('admin/_layout_main',$this->data);
}
public function edit ($id = NULL)
{
// Fetch a page or set a new one
if ($id) {
$this->data['page'] = $this->page_m->get($id);
count($this->data['page']) || $this->data['errors'][] = 'Not found.';
}
else {
$this->data['page'] = $this->page_m->get_new();
}
// Pages for dropdown
$this->data['pages_no_parents'] = $this->page_m->get_no_parents();
// Set up the form
$rules = $this->page_m->rules;
$this->form_validation->set_rules($rules);
// Process the form
if ($this->form_validation->run() == TRUE) {
$data = $this->page_m->array_from_post(array('title', 'slug', 'body','keywords','description','parent_id'));
$this->page_m->save($data, $id);
redirect('admin/page');
}
// Load the view
$this->data['subview'] = 'admin/page/edit';
$this->load->view('admin/_layout_main', $this->data);
}
}
Model:
public function get_no_parents ()
{
// Fetch pages without parents
$this->db->select('id, title');
$this->db->where('parent_id', 0);
$pages = parent::get();
// Return key => value pair array
$array = array(
0 => ''
);
if (count($pages)) {
foreach ($pages as $page) {
$array[$page->id] = $page->title;
}
}
return $array;
}
View :
<?=form_dropdown('parent_id', $pages_no_parents, set_value('parent_id', $pages_no_parents, $this->input->post('parent_id') ? $this->input->post('parent_id') : $page->parent_id);?>

In the if condition $this->form_validation->run == false write:
$this->data['pages_no_parents'] = $this->page_m->get_no_parents();

you can try something like this
if($this->form_validation->run() == FALSE)
{
redirect('your_controller/function','refresh');
}
UPDATE
so instead of redirect do load view like
$this->load->view('admin/_layout_main', $this->data);
and in view page do one thing
<?php echo validation_errors(); ?>
please let me know if you face any problem.

As ahmad mentioned, i think i was overwriting something, and after removing set value, and changed view to this, it's now okay.
<?php echo form_dropdown('parent_id', $pages_no_parents, $this->input->post('parent_id') ? $this->input->post('parent_id') : $page->parent_id); ?>
Thanks for all for the comments and answers.

Related

CodeIgniter - Unable to access an error message corresponding to your field name

I'm creating a library to add new users.
class Register
{
private $CI;
public function add_new_user()
{
$CI =& get_instance();
$CI->form_validation->set_rules('email', 'Email', 'required|callback_is_email_exist');
if ($CI->form_validation->run() == TRUE)
{
$email = $_POST['email'];
$insert_data = array('email' => $email);
$CI->new_data->add_user($insert_data);
}
}
private function is_email_exist($email)
{
$CI =& get_instance();
$email_result = '';
$query = $CI->check->find_email($email);
foreach ($query->result_array() as $row)
{
$email_result = $row['email'];
}
if ($email_result == $email)
{
$CI->form_validation->set_message('is_email_exist', 'Such email already exist!');
return FALSE;
}
else
{
return TRUE;
}
}
}
I add form_validation and models check, new_data to the autoload. When I submit a form instead of an error (if it should be there) I get Unable to access an error message corresponding to your field name Username.(is_username_exist). What should i do to get right error?
Codeigniter form validation is for the controller so when submit form it goes there
https://www.codeigniter.com/user_guide/libraries/form_validation.html#the-controller
Controller Way
<?php
class Register extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->model('some_model');
}
public function index() {
$this->form_validation->set_rules('email', 'Email', 'required|callback_is_email_exist');
if ($this->form_validation->run()) {
$insert_data = array(
'email' => $this->input->post('email')
);
$this->some_model->new_data->add_user($insert_data);
}
$data['title'] = 'Welcome To Codeigniter';
$this->load->view('header', $data);
$this->load->view('someview', $data);
$this->load->view('footer');
}
// Check user email
public function is_email_exist() {
// Single input check
$this->db->where('email', $this->input->post('email'));
$query = $this->db->get('user');
// If user email greater than 0
if ($query->num_rows() > 0) {
return TRUE;
} else {
$this->form_validation->set_message('is_email_exist', 'Such email already exist!');
return FALSE;
}
}
}
replace 'is_email_exist' to __FUNCTION__ work for me,
https://stackoverflow.com/a/38950446/2420302

Codeigniter admin route [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am stuck with this problem.
I get 404 error when try to access admin pages in codeigniter, I can only access the fronted pages.
Here you have my route.php file:
$route['default_controller'] = "page";
$route['404_override'] = '';
$route[':any'] = "page/index/$1"; **WORKS**
$route['admin/page'] = "admin/page"; **WORKS**
$route['admin/page/order'] = "admin/page/order"; **NOT WORKING** <!-- this i my issue -->
$route['admin/page/edit'] = "admin/page/edit"; **WORKS**
$route['admin/page/edit/(:num)'] = "admin/page/edit/$1"; **NOT WORKING** <!-- this i my issue -->
Admin Controller
class Admin_Controller extends MY_Controller
{
function __construct ()
{
parent::__construct();
$this->data['meta_title'] = 'My awesome CMS';
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->library('session');
$this->load->model('user_m');
// Login check
$exception_uris = array(
'admin/user/login',
'admin/user/logout'
);
if (in_array(uri_string(), $exception_uris) == FALSE) {
if ($this->user_m->loggedin() == FALSE) {
redirect('admin/user/login');
}
}
}
}
Frontend pages format
http://www.my_site.com/index.php/about
Backend format
http://www.my_site.com/admin/page
http://www.my_site.com/index.php/admin/page/edit/1
Please, can anyone help me?
Thanks in advance.
Guys thanks for the reply
Here you have my Page Admin Controller and also I have amended the route.php above.
<?php
class Page extends Admin_Controller
{
public function __construct ()
{
parent::__construct();
$this->load->model('page_m');
}
public function index ()
{
// Fetch all pages
$this->data['pages'] = $this->page_m->get_with_parent();
// Load view
$this->data['subview'] = 'admin/page/index';
$this->load->view('admin/_layout_main', $this->data);
}
public function order ()
{
$this->data['sortable'] = TRUE;
$this->data['subview'] = 'admin/page/order';
$this->load->view('admin/_layout_main', $this->data);
}
public function order_ajax ()
{
// Save order from ajax call
if (isset($_POST['sortable'])) {
$this->page_m->save_order($_POST['sortable']);
}
// Fetch all pages
$this->data['pages'] = $this->page_m->get_nested();
// Load view
$this->load->view('admin/page/order_ajax', $this->data);
}
public function edit ($id = NULL)
{
// Fetch a page or set a new one
if ($id) {
$this->data['page'] = $this->page_m->get($id);
count($this->data['page']) || $this->data['errors'][] = 'page could not be found';
}
else {
$this->data['page'] = $this->page_m->get_new();
}
// Pages for dropdown
$this->data['pages_no_parents'] = $this->page_m->get_no_parents();
// Set up the form
$rules = $this->page_m->rules;
$this->form_validation->set_rules($rules);
// Process the form
if ($this->form_validation->run() == TRUE) {
$data = $this->page_m->array_from_post(array(
'title',
'slug',
'body',
'template',
'parent_id'
));
$this->page_m->save($data, $id);
redirect('admin/page');
}
// Load the view
$this->data['subview'] = 'admin/page/edit';
$this->load->view('admin/_layout_main', $this->data);
}
public function delete ($id)
{
$this->page_m->delete($id);
redirect('admin/page');
}
public function _unique_slug ($str)
{
// Do NOT validate if slug already exists
// UNLESS it's the slug for the current page
$id = $this->uri->segment(4);
$this->db->where('slug', $this->input->post('slug'));
! $id || $this->db->where('id !=', $id);
$page = $this->page_m->get();
if (count($page)) {
$this->form_validation->set_message('_unique_slug', '%s should be unique');
return FALSE;
}
return TRUE;
}
}
Front End Page Controller
<?php
class Page extends Frontend_Controller{
public function __construct(){
parent::__construct();
$this->load->model('page_m');
}
public function index(){
// Fetch the page template
$this->data['page'] = $this->page_m->get_by(array('slug' => (string) $this->uri->segment(1)), TRUE);
count($this->data['page']) || show_404(current_url());
// Fetch the page data
$method = '_' . $this->data['page']->template;
if (method_exists($this, $method)) {
$this->$method();
}
else {
log_message('error', 'Could not load template ' . $method .' in file ' . __FILE__ . ' at line ' . __LINE__);
show_error('Could not load template ' . $method);
}
//Fetch the view
$this->data['subview'] = $this->data['page']->template;
$this->load->view('_main_layout', $this->data);
}
private function _page(){
dump('welcome from the page template');
}
private function _homepage(){
$this->load->model('article_m');
$this->db->where('pubdate <=', date('Y-m-d'));
$this->db->limit(6);
$this->data['articles'] = $this->article_m->get();
}
private function _news_archive(){
$this->load->model('article_m');
// Count all articles
$this->db->where('pubdate <=', date('Y-m-d'));
$count = $this->db->count_all_results('articles');
// Set up pagination
$perpage = 4;
if ($count > $perpage) {
$this->load->library('pagination');
$config['base_url'] = site_url($this->uri->segment(1) . '/');
$config['total_rows'] = $count;
$config['per_page'] = $perpage;
$config['uri_segment'] = 2;
$this->pagination->initialize($config);
$this->data['pagination'] = $this->pagination->create_links();
$offset = $this->uri->segment(2);
}
else {
$this->data['pagination'] = '';
$offset = 0;
}
// Fetch articles
$this->db->where('pubdate <=', date('Y-m-d'));
$this->db->limit($perpage, $offset);
$this->data['articles'] = $this->article_m->get();
}
}
Hope you can have help me
Thanks
I dont see any routes nor URLs that point to your Admin_Controller . If a user types http://.../admin/page Codeigniter will try to look for a controller named Admin not Admin_controller unless you have routes set up.
Also, you have this route: $route[':any'] = "page/index/$1"; This route will take any URL given and redirect the user to the Page controller (which you have not provided any code on). I would get rid of it, or what is its function?
What you need to decide is whether your admin URL should be this: http://www.my_site.com/admin/page
or this: http://www.my_site.com/admin_controller/page
Personally I would choose the first one since it looks cleaner. Which then means you have to decide whether your controller should stay named as Admin_Controller or just Admin. I would chose Admin so that you dont have to deal with routes.
The final result should be this:
Your admin URL: http://www.my_site.com/admin/page
Your Controller: application/controllers/Admin.php
class Admin extends MY_Controller {
public function __construct() {
//... your code here
}
public function page() {
//... your code here
}
}
You don't seem to have
function index()
in your admin controller.

codeigniter : user profile page

Hello i have problem with the user profile page in codeigniter .
So here is my user controller :
<?php
class User extends CI_Controller {
function users() {
parent::user();
}
function index($id = null) {
if($id == null) {
redirect('/', 'refresh');
}
else {
$data['title'] = 'User Page';
$data['result'] = $this->users_model->get_user_info();
$data['id'] = $id;
$data['main_content'] = "main/profile_view" ;
$this->load->view('home',$data);
}
}
}
?>
And this is the function in the model :
public function get_user_info(){
$this->db->where('id' , 'id');
$q = $this->db->get('users');
if ($q->num_rows > 0) {
return $q->result();
} else {
return false;
}
}
And this is the routes file :
$route['user/(:any)'] = "user/index/$1";
i get this error in localhost/cc/user/1
Fatal error: Call to a member function get_user_info() on null in C:\xampp\htdocs\cc\application\controllers\user.php on line 12
And i want to know how to display the user data in the view
in your controller i think this line:
$data['result'] = $this->users_model->get_user_info();
should be:
$data['result'] = $this->users_model->get_user_info($id);
and in your model this:
public function get_user_info(){
$this->db->where('id' , 'id');
should be this:
public function get_user_info($id){
$this->db->where('id' , $id);
// ================= next question
i tried it , but when i enter a non exist id after user/. , it displays the profile vue
yeah i was going to wrap it in an IF but then got lazy -- here's one way
check if the result did not come back -- so after you check to see if $id is NULL -- if $data['result'] did not come back from the model then go to a method like showUserNotFound($id).
elseif ( ! $data['result'] = $this->users_model->get_user_info($id) ){
$this->showUserNotFound($id); }
else { $data['title'] = 'User Page';
$data['id'] = $id;
$data['main_content'] = "main/profile_view" ;
$this->load->view('home',$data); }

How do I display active record in codeigniter with multiple controllers?

I am building my comment module and trying to get the active records to display on view. Seems simple however I have a lot going on and using a separate model and controller then what the view is being generated from.
So I have a profile page ( where I'm trying to get the results to display )
Controller:
public function profile()
{
$this->load->helper('url'); //Include this line
$this->load->helper('date');
$this->load->library('session');
$this->load->library('form_validation');
$session_id = $this->session->userdata('id'); //Ensure that this session is valid
//TRYING TO MAKE A VARIABLE WITHT THE $_GET VALUE
$user_get = $this->uri->segment(3); // Modify this line
// LOAD THE CORRECT USER
$this->load->model('account_model');
$user = $this->account_model->user($user_get); //(suggestion) you want to pass the id here to filter your record
$data['user'] = $user;
$data['session_id'] = $session_id;
if($user_get != $user['id'] || !$user_get)
{
$data['main_content'] = 'account/notfound';
$this->load->view('includes/templates/profile_template', $data);
}
else
{
if($user_get == $session_id) //Modify this line also
{
$data['profile_icon'] = 'edit';
}
else
{
$data['profile_icon'] = 'profile';
}
$sharpie = $this->sharpie($user);
$data['sharpie'] = $sharpie;
$data['main_content'] = 'account/profile';
$this->load->view('includes/templates/profile_template', $data);
}
}
Now I have a new controller for my comments I'm trying to display:
public function airwave() {
$this->load->helper('date');
$this->load->library('session');
$airwave_get = $this->uri->segment(3);
$this->load->model('community_model');
$airwave = $this->coummunity_model->airwave($airwave_get);
$data['airwave'] = $airwave;
$data['main_content'] = 'account/profile';
$this->load->view('includes/templates/main_page_template', $data);
}
with this model:
public function airwave($id=null)
{
if(is_null($id)) {
$session = $this->session->userdata('is_logged_in');
$commenter_id = $this->session->userdata('id');
}else{
$commenter_id = intval($id);
}
$query = $this->db->query("SELECT * FROM airwaves_comments WHERE from_id=$commenter_id");
if($query->num_rows()==1)
{
$data = $query->result_array();
return $data[0];
//above returns the single row you need to the controller as an array.
//to the $data['user'] variable.
}
}
and trying to display it on this view ( which is generated by the profile function )
<div class="profile_airwave_comment_text">
<?php echo $airwave['comment'];?>
</div>
I just can't seem to get it to pass the array variable I've created properly to that view?
thanks in advance.
The first glaring thing I see wrong is in you model's airwave function:
if($query->num_rows()==1)
{
$data = $query->result_array();
return $data[0];
You are assuming that there will only be one result in your query, so if there are more or less than one, your $data array will bever be set.
Also, even then you are only returning the first element in the array. I am not seeing anywhere in your question that you want to return only one.
Finally, in your view, if you now return the full array, and not only one element, you will have to do a foreach statement.
EDIT: please see suggested solution.
In your model, don't worry about the check for the amount of data in the result array. This will come later. Simply do:
return $query->result_array();
Now, keep your controller as is, but adapt your view:
<div class="profile_airwave_comment_text">
<?php
if (isset($airwave) && is_array($airwave) && !empty($airwave))
{
foreach ($airwave as $wave)
{
echo $wave['comment'];
}
}
else
{
echo 'No comments found...';
}
?>
</div>
This should work, and if not, at least let you know that no comments were fond, and thus check your database.
HTH!
If your using HMVC, simply call the module from inside the profile view, ie:
echo Modules::run('comments/profileComments', $user->id);
Else:
You would need to create a new method in the loader class, to load in the controller seperate from routing.
If your really after a quick fix until you come up with a better alternative, you could(depending if both controllers live in the same directory) just include the 'comments' controller in your 'profile' controller and try something like this:
{Cons: CI Pagination wont work!}
application/controllers/profile.php
include 'Comments' . EXT;
class Profile extends CI_Controller{
public function __construct ()
{
parent::__construct () ;
}
public function index( $user_id ){
$commentsPartialView = call_user_func_array(array(new Comments(), "getProfileComments"), array($user_id));
return $this->load->view($this->template, array(
'view' => 'profiles/_index',
'comments' => $commentsPartialView
));
}
}
-
application/controllers/Comments.php
class Comments extends CI_Controller{
public function __construct ()
{
parent::__construct () ;
}
public function getProfileComments( $user_id ){
$user_comments = ''; //pull from DB
//set third param to TRUE for partial view, without main template
return $this->load->view('comments/show', array(
'user_comments' => $user_comments
), TRUE);
}
}

Getting ID from previous page to field on next page (CodeIgniter)

Basically all I want to do is from my view page which is displaying a specific article then having a delete link to a page with a field and a button for deleting that article with the id displayed in that field.
I have had a go at this for the last couple of days trying to put the id in the URL link i.e "delete?id=20" and trying to access it with a $_GET and then I tried "delete/20" and URI segments.
Then I attempted at using sessions etc but i am not sure which is best as I haven't got any of them working.
I have decided to show my untouched code and start from scratch here is my code:
view.php
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo '<p>'.$news_item['text'].'</p>';
?><br><br>
<a href="http://website.com/CodeIgniter/index.php/news">
Go to latest news</a>
Delete<br>
delete.php
<h2>Delete a news item</h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/delete') ?>
<form>
<label for="delete">Article Number</label><br>
<input name="id" class="resizedTitlebox" value="id" /><br>
<br>
<input type="submit" name="submit" value="Delete news item" /></form>
news.php (controller)
<?php
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
public function view($slug)
{
$data['news_item'] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
public function delete() {
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Delete news item';
$this->form_validation->set_rules('id', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/delete');
$this->load->view('templates/footer');
}
else
{
$data['id'] = $this->news_model->delete('id');
$this->load->view('news/success');
}
}
}
news_model.php (model)
<?php
class News_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_news($slug = FALSE){
$this->load->helper('text');
if ($slug === FALSE){
$this->db->order_by('id', 'desc');
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
public function set_news(){
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'id' => $this->input->post('id'),
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text'));
return $this->db->insert('news', $data);
}
public function delete ($id) {
$this->db->where('id',$this->input->post('id'));
$this->db->delete('news');
}
}
routes.php (config)
$route['news/(:any)'] = 'news/view/$1';
$route['news/delete'] = 'news/delete';
$route['news'] = 'news';
$route['default_controller'] = 'news';
$route['404_override'] = '';
Thanks in advance for any help given!
#jeroen answer
"You are not passing any value in the delete link; you should add an ID either to the path or the query string" - I assume you mean like
Delete
So using article_id. Then I can define article_id in the delete controller? I am not sure how this can be done.
answer:
$this->input->get(article_id)
Your delete procedure seems strange and has some errors:
You are no specifying a method in your form, so that will default to GET but in your delete function you use $this->input->post('id'). You should change your form to <form action="" method="POST">;
You are sending a variable to your delete function that you don't use: $id. This is a string in your controller although the name in the model seems to suggest an ID. But you are not using it anyway...
You are expecting a return value from the delete function when you call it in your controller but you don't return anything from your function.
You are not passing any value in the delete link; you should add an ID either to the path or the query string, but be careful not to use the same name $id if you use the same controller as the form will validate without the confirmation;
You are not getting any value for the ID in the delete controller and passing it to the view to fill the value of your ID field.
By the way, there is no input type id but that should not cause any problems as it defaults to text.
I think their is no need of delete view page.You can directly delete news item from your news list page
Delete<br>
instead of this line use this
<a href = "<?php echo site_url("news/delete/".$news_item['id']); ?>Delete</a>
And in your controller
public function delete($id){
$this->news_model->delete($id);
$this->load->view('news/success');
}
And in your model use this
function Delete($id){
$this->db->where('id', $id);
if($this->db->delete("news"))
return true;
else
return false;
}

Categories