updating database on form submit codeigniter - php

I am having trouble updating my value in my database on form submit. I use codeigniter 2.20
I am getting a error Fatal error: Call to undefined method Model_setting::updateTheme() in E:\Xampp\htdocs\codeigniter-theme\admin\controllers\setting\setting.php on line 8
What I am trying to archive is on once have selected theme in form it will update the setting table value is where it gets posted to. It's not changing on form submit either.
I have autoloaded form_validation lib and form helper.
Model
<?php
class Model_setting extends CI_Model {
public function updateTheme() {
$this->db->select('*');
$this->db->where('group', 'config');
$this->db->where('key', 'config_template');
$this->db->where('value', $this->input->post('config_template')); // Need to update theme row
$query = $this->db->update('setting');
}
}
view
<form method="post" action="<?php echo $action;?>" role="form" class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label" for="input-template"><?php echo $entry_template; ?></label>
<div class="col-sm-10">
<select name="config_template" id="input-template" class="form-control">
<?php foreach ($templates as $template) { ?>
<?php if ($template == $config_template) { ?>
<option value="<?php echo $template; ?>" selected="selected"><?php echo $template; ?></option>
<?php } else { ?>
<option value="<?php echo $template; ?>"><?php echo $template; ?></option>
<?php } ?>
<?php } ?>
</select>
<br />
<img src="" alt="" id="template" class="img-thumbnail" />
</div>
</div>
<button type="submit" class="btn btn-md btn-primary">Save</button>
</form>
Controller
public function index() {
$this->load->model('setting/model_setting');
$this->model_setting->updateTheme();
if(null !==($this->input->post('config_template'))) {
$data['config_template'] = $this->input->post('config_template');
} else {
$data['config_template'] = $this->theme->get('value'); // Auto loaded Library Theme
}
$data['templates'] = array();
$directories = glob(DIR_CATALOG . 'views/theme/*', GLOB_ONLYDIR);
foreach ($directories as $directory) {
$data['templates'][] = basename($directory);
}
$this->form_validation->set_rules('config_template', '', 'callback_validate');
if($this->form_validation->run()) {
redirect('setting/store');
} else {
$this->lang->load('setting/setting', 'english');
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->lang->line('text_home'),
'href' => site_url('common/dashboard')
);
$data['breadcrumbs'][] = array(
'text' => $this->lang->line('heading_title'),
'href' => site_url('setting/setting')
);
$data['action'] = site_url('setting/setting');
$data['title'] = "Settings";
$data['entry_template'] = $this->lang->line('entry_template');
$data['header'] = $this->header($data);
$data['footer'] = $this->footer($data);
$this->load->view('setting/setting', $data);
}
}

You should read some basic CI first:
function index() {
$this->load->model('setting/model_setting'); //load model
if( $this->input->post(null) ){ //detect if form is submitted
if(null !==($this->input->post('config_template'))) {
$data['config_template'] = $this->input->post('config_template');
} else {
$data['config_template'] = $this->theme->get('value'); // Auto loaded Library Theme
}
if($this->form_validation->run()) {
$this->model_setting->updateTheme();
}
redirect('setting/store');
}else{ // load view only
$data['templates'] = array();
$directories = glob(DIR_CATALOG . 'views/theme/*', GLOB_ONLYDIR);
foreach ($directories as $directory) {
$data['templates'][] = basename($directory);
}
$this->form_validation->set_rules('config_template', '', 'callback_validate');
$this->lang->load('setting/setting', 'english');
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->lang->line('text_home'),
'href' => site_url('common/dashboard')
);
$data['breadcrumbs'][] = array(
'text' => $this->lang->line('heading_title'),
'href' => site_url('setting/setting')
);
$data['action'] = site_url('setting/setting');
$data['title'] = "Settings";
$data['entry_template'] = $this->lang->line('entry_template');
$data['header'] = $this->header($data);
$data['footer'] = $this->footer($data);
$this->load->view('setting/setting', $data);
}
}
You should first check if there is any post request, if there is update and redirect else show the form.

I have got it working.
Model
<?php
class Model_setting extends CI_Model {
public function update_theme() {
$this->db->where('key', 'config_template');
$this->db->set('value', $this->input->post('config_template'));
$this->db->update('setting');
}
}
controller
public function index() {
//$this->load->model('setting/model_setting');
$data['templates'] = array();
$directories = glob(DIR_CATALOG . 'views/theme/*', GLOB_ONLYDIR);
foreach ($directories as $directory) {
$data['templates'][] = basename($directory);
}
$this->form_validation->set_rules('config_template', '', 'callback_validate');
if($this->form_validation->run() == true) {
$this->load->model('setting/model_setting');
if($this->model_setting->update_theme()) {
$data['config_template'] = $this->input->post('config_template');
} else {
$data['config_template'] = $this->input->get('config_template');
}
redirect('setting/store');
} else {
$this->lang->load('setting/setting', 'english');
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->lang->line('text_home'),
'href' => site_url('common/dashboard')
);
$data['breadcrumbs'][] = array(
'text' => $this->lang->line('heading_title'),
'href' => site_url('setting/setting')
);
$data['action'] = site_url('setting/setting');
$data['title'] = "Settings";
$data['entry_template'] = $this->lang->line('entry_template');
$data['header'] = $this->header($data);
$data['footer'] = $this->footer($data);
$this->load->view('setting/setting', $data);
}
}

Related

PHP Codeigniter giving error: An Error Was Encountered The action you have requested is not allowed

I am getting the following error when trying to add a new function to my existing product. I am new to php codeIgniter and have been stuck on this for a while. Any help will be appreciated and thanks in advance. If any further file is needed, let me know and I will update the post. When I try to submit the search_add_view form, I get the following error:
Error Was Encountered The action you have requested is not allowed.
my profile controller profile.php is:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Profile extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->helper('download');
$this->load->library('form_validation');
$this->load->library('table');
$this->load->model('searches_model');
$this->searches = $this->searches_model->get_searches();
$this->sites = $this->searches_model->get_sites();
$this->load->driver('scraper', array_column($this->sites, 'driver'));
//check auth
if (!is_admin() && !is_user()) {
redirect(base_url());
}
}
public function index()
{
if (user()->qr_code == '') {
$this->generate_qucode(user()->slug);
}
$data = array();
$data['page_title'] = 'Profile';
$data['user'] = $this->admin_model->get_user_info();
$data['payment'] = $this->admin_model->get_my_payment();
$data['features'] = $this->admin_model->active_features($data['payment']->package_id);
$data['fonts'] = $this->admin_model->select('google_fonts');
$data['countries'] = $this->admin_model->select('country');
$data['main_content'] = $this->load->view('admin/user/profile', $data, TRUE);
$this->load->view('admin/index', $data);
}
public function search_index()
{
$this->form_validation->set_rules('search[name]', 'Search Name', 'required');
$this->form_validation->set_rules('search[site_id]', 'Site', 'required');
$this->form_validation->set_rules('search[url]', 'Search URL', 'required');
if ($this->form_validation->run())
{
$data = $this->input->post('search', TRUE);
$search_id = $this->searches_model->add_search($data);
redirect();
}
$data = array(
'subview' => 'search_add_view',
'sites' => $this->sites,
'searches' => $this->searches
);
$data['page_title'] = 'Search View';
$data['subview'] = $this->load->view('admin/scraper/search_add_view', $data, TRUE);
$data['sites'] = $this->sites;
$data['searches'] = $this->searches;
$data['main_content'] = $this->load->view('admin/scraper/searches_view', $data, TRUE);
$this->load->view('admin/index', $data);
}
public function edit($search_id)
{
$search = $this->searches_model->get_search($search_id);
$this->form_validation->set_rules('search[name]', 'Search Name', 'required');
$this->form_validation->set_rules('search[site_id]', 'Site', 'required');
$this->form_validation->set_rules('search[url]', 'Search URL', 'required');
if ($this->form_validation->run())
{
$data = $this->input->post('search', TRUE);
$this->searches_model->update_search($search_id, $data);
redirect();
}
$data = array(
'subview' => 'search_edit_view',
'sites' => $this->sites,
'searches' => $this->searches,
'search' => $search
);
$data['main_content'] = $this->load->view('admin/scraper/search_edit_view', $data, TRUE);
$this->load->view('admin/index', $data);
}
/**
* Delete a search
*
* #param integer search id to delete
* #return none
*/
public function delete($search_id)
{
$search = $this->searches_model->get_search($search_id);
$search['id'] = $search_id;
if ($this->input->server('REQUEST_METHOD') == 'POST')
{
$data = array('id' => $search_id);
$this->searches_model->delete_search($data);
redirect();
}
$data = array(
'subview' => 'search_delete_view',
'sites' => $this->sites,
'searches' => $this->searches,
'search' => $search
);
$data['main_content'] = $this->load->view('admin/scraper/search_delete_view', $data, TRUE);
$this->load->view('admin/index', $data);
}
public function execute($search_id)
{
$search = $this->searches_model->get_search($search_id);
$this->form_validation->set_rules('search[url]', 'Search URL', 'required');
if ($this->form_validation->run())
{
$search['url'] = $this->input->post('search[url]', true);
$output = $this->scraper->scrape($search['url'], $search['driver']);
force_download($search['name'] . '.csv', $output);
}
$data = array(
'subview' => 'search_execute_view',
'sites' => $this->sites,
'searches' => $this->searches,
'search' => $search
);
$data['main_content'] = $this->load->view('admin/scraper/search_execute_view', $data, TRUE);
$this->load->view('admin/index', $data);
}
}
my routes.php is:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'home/index';
$route['404_override'] = 'home/error_404';
$route['translate_uri_dashes'] = FALSE;
$route['index'] = 'home/index';
$route['error-404'] = 'home/error_404';
$uri = $this->uri->segment(1);
if ( $uri == 'searches_view' || $uri == 'search_add_view' || $uri == 'search_execute_view' || $uri == 'search_delete_view' || $uri == 'search_edit_view') {
$route['search_add_view/(:any)'] = 'admin/profile/search_add_view/$1';
$route['searches_view'] = 'admin/profile/search_index';
$route['search_execute_view'] = 'admin/profile/search_execute_view';
$route['search_delete_view'] = 'admin/profile/search_delete_view';
$route['search_edit_view'] = 'admin/profile/search_edit_view';
}
search_view.php is:
<?php include'include/header.php';?>
<?php include'include/left_sideber.php';?>
<div style="height:100vh; margin-left: 230px;" class="content">
<?php echo $subview;?>
<h2>Saved Searches</h2>
<?php
if (count($searches) == 0)
{
echo '<p>There are no saved searches.</p>';
}
else
{
$this->table->set_heading('Search Name', 'Site', 'Search URL', 'Edit', 'Delete', 'Execute');
foreach ($searches as $search)
{
$this->table->add_row(
$search['name'], $search['site_name'], $search['url'],
anchor('searches/edit/' . $search['id'], 'Edit'),
anchor('searches/delete/' . $search['id'], 'Delete'),
anchor('searches/execute/' . $search['id'], 'Execute')
);
}
echo $this->table->generate();
}
?>
<?php echo $main_content;?>
<?php include'include/footer.php';?>
search_add_view.php:
<div class="content">
<form method="post">
<fieldset>
<legend>Add a New Search</legend>
<p class="space">
<label for="name">Search Name</label>
<input type="text" id="name" name="search[name]" value="<?php echo set_value('search[name]', ''); ?>" size="32" maxlength="32" autofocus>
<?php echo form_error('search[name]', ' <br>', '<br>' . PHP_EOL); ?>
</p>
<p>
<label for="sites">Site</label>
<select id="sites" name="search[site_id]">
<option value="" selected>Select one</option>
<?php foreach ($sites as $row):?>
<option value="<?=$row['id']?>" <?php echo set_select('search[site_id]', $row['id']); ?>><?=$row['name']?></option>
<?php endforeach;?>
</select>
<?php echo form_error('search[site_id]', ' <br>', '<br>' . PHP_EOL); ?>
</p>
<p>
<label for="url">Search URL</label>
<input type="text" id="url" name="search[url]" value="<?php echo set_value('search[url]', ''); ?>" size="105" maxlength="254">
<?php echo form_error('search[url]', ' <br>', '<br>' . PHP_EOL); ?>
</p>
<p><input type="submit" name="submit" value="Ok"></p>
</fieldset>
</form>
<p> </p>
</div>
search_model.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Searches_model extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function get_searches()
{
return $this->db->query(
'SELECT
searches.id AS id,
searches.name AS name,
searches.url AS url,
sites.id AS site_id,
sites.name AS site_name,
sites.driver AS driver
FROM searches
INNER JOIN sites ON
searches.site_id = sites.id
;'
)->result_array();
}
public function get_search($search_id)
{
$query = $this->db->query(
'SELECT
searches.name AS name,
searches.url AS url,
sites.id AS site_id,
sites.name AS site_name,
sites.driver AS driver
FROM searches
INNER JOIN sites ON
searches.site_id = sites.id
WHERE searches.id = ' . $search_id . '
;'
)->result_array();
return $query[0];
}
public function add_search($data)
{
$this->db->insert('searches', $data);
return $this->db->insert_id();
}
public function update_search($id, $data)
{
$this->db->update('searches', $data, array('id' => $id));
}
public function delete_search($data)
{
$this->db->delete('searches', $data);
}
public function get_sites()
{
return $this->db->query(
'SELECT
sites.id AS id,
sites.name AS name,
sites.driver AS driver
FROM sites
;'
)->result_array();
}
}
my file structure is:
I managed to solve this issue with two steps:
First, I confirmed the config.json contains the following property:
$config['csrf_protection'] = TRUE;
Second, I had to add the following code to the html after form declaration:
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">

Array to string conversion on CodeIgniter

my problem is a want to insert multiple check box on my web application.
error Message
Array to string conversion
please help....
Controller :
public function add_overview_product($type)
{
if ($this->input->post('submit')) {
$type = $type;
$id_overview = $this->input->post('id_overview');
$records = array();
for ($i=0; $i < count($id_overview) ; $i++) {
$records[] = array(
'type' => $type,
'id_overview' => $id_overview[$i]
);
}
$check_idoverview = $this->Biostar->check_idoverview($type,$id_overview);
if ($check_idoverview > 0) {
$message = 'Sorry, The Product can not input twice with the same "TYPE" ';
} else {
foreach ($records as $key => $value) {
$datafield = array(
'type' => $type,
'id_overview' => $id_overview
);
$this->Biostar->saveoverviewproduct($datafield);
}
$data['type'] = $type;
$data['content'] = 'biostar/add_specification';
$this->load->view('dashboard/index', $data);
}
}
$data['diy'] = $diy;
$data['content'] = 'biostar/add_overview_product';
$this->load->view('dashboard/index', $data);
}
My : Model
public function saveoverviewproduct($datafield){
$sql = $this->db->insert('overview_biostar',$datafield);
return $sql;
}
public function check_idoverview($type,$id_overview){
$this->db->select('type');
$this->db->where('type',$type);
$this->db->where('id_overview',$id_overview);
$query = $this->db->get('overview_biostar')->rows();
return $query;
}
View:
<form method="post"action="<?php echo base_url(); ?>biostar/add_overview_product/<?php echo $type; ?>" >
<div class="box-body">
<?php foreach ($audio as $row){ ?>
<div class="checkbox">
<label>
<input type="checkbox" name="id_overview[]" value="<?php echo $row['title']; ?>"><?php echo $row['title']; ?>
</label>
</div>
<?php } ?>
</div>
$check_idoverview = $this->Biostar->check_idoverview($type,$id_overview);
$id_overview is an array. It should be a string.

Codeigniter Form Not Updating And Not Redirecting

I am using codeigniter form validation lib and for some reason form is not updating this particular row. And there for not redirecting when form Submitted.
On my controller I use function like this
$this->load->model('admin/setting/model_setting');
$config_meta_title = $this->model_setting->edit_meta_title($this->input->post('config_meta_title'));
if (!empty($config_meta_title)) {
$data['config_meta_title'] = $this->input->post('config_meta_title');
} else {
$data['config_meta_title'] = $this->configs->get('config_meta_title');
}
But not updating database.
Model
<?php
class Model_setting extends CI_Model {
public function edit_meta_title() {
$data = array(
'group' => "config",
'key' => "config_meta_title",
'value' => $this->input->post('config_meta_title')
);
$this->db->where('setting_id', "2");
$this->db->update('setting', $data);
}
}
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Setting extends MY_Controller {
public function __construct() {
parent::__construct();
$this->lang->load('admin/setting/setting', 'english');
$this->lang->load('admin/english', 'english');
if ($this->session->userdata('user_id') == true) {
return true;
} else {
redirect('admin');
}
}
public function index() {
$this->load->library('form_validation');
$data['text_yes'] = $this->lang->line('text_yes');
$data['text_no'] = $this->lang->line('text_no');
$data['entry_meta_title'] = $this->lang->line('entry_meta_title');
$data['entry_maintenance'] = $this->lang->line('entry_maintenance');
$data['button_save'] = $this->lang->line('button_save');
$data['button_cancel'] = $this->lang->line('button_cancel');
$data['tab_store'] = $this->lang->line('tab_store');
$data['action'] = site_url('admin/setting');
$data['logout'] = site_url('admin/logout');
$data['cancel'] = site_url('admin/dashboard');
$this->load->model('admin/setting/model_setting');
$config_meta_title = $this->model_setting->edit_meta_title($this->input->post('config_meta_title'));
if (!empty($config_meta_title)) {
$data['config_meta_title'] = $this->input->post('config_meta_title');
} else {
$data['config_meta_title'] = $this->configs->get('config_meta_title');
}
if ($this->form_validation->run() == FALSE) {
return $this->load->view('setting/settings', $data);
} else {
redirect('admin/dashboard');
}
}
}
In your model, kindly try to pass it as a parameter:
public function edit_meta_title($config_meta_title) {
$data = array(
'group' => "config",
'key' => "config_meta_title",
'value' => $config_meta_title,
);
$this->db->where('setting_id', "2");
$this->db->update('setting', $data);
return $this->db->affected_rows();
}
I have fixed my issues now All working perfect
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Setting extends MY_Controller {
public function __construct() {
parent::__construct();
$this->lang->load('admin/setting/setting', 'english');
$this->lang->load('admin/english', 'english');
if ($this->session->userdata('user_id') == true) {
return true;
} else {
redirect('admin');
}
}
public function index() {
$data = array();
$data['text_yes'] = $this->lang->line('text_yes');
$data['text_no'] = $this->lang->line('text_no');
$data['entry_meta_title'] = $this->lang->line('entry_meta_title');
$data['entry_template'] = $this->lang->line('entry_template');
$data['entry_maintenance'] = $this->lang->line('entry_maintenance');
$data['button_save'] = $this->lang->line('button_save');
$data['button_cancel'] = $this->lang->line('button_cancel');
$data['tab_store'] = $this->lang->line('tab_store');
$data['action'] = site_url('admin/setting');
$data['logout'] = site_url('admin/logout');
$data['cancel'] = site_url('admin/dashboard');
$this->load->model('admin/setting/model_setting');
if (empty($config_meta_title)) {
$data['config_meta_title'] = $this->configs->get('config_meta_title');
}
if (empty($config_template)) {
$data['config_template'] = $this->configs->get('config_template');
}
$data['templates'] = array();
$directories = glob(APPPATH . 'modules/catalog/views/theme/*', GLOB_ONLYDIR);
foreach ($directories as $directory) {
$data['templates'][] = basename($directory);
}
if (empty($config_maintenance)) {
$data['config_maintenance'] = $this->configs->get('config_maintenance');
}
$this->load->library('form_validation');
$this->form_validation->set_rules('config_meta_title', 'Meta Title');
$this->form_validation->set_rules('config_template', 'Template');
$this->form_validation->set_rules('config_maintenance', 'Maintenance');
if ($this->form_validation->run() == FALSE) {
return $this->load->view('setting/settings', $data);
} else {
$config_meta_title = $this->model_setting->edit_meta_title($this->input->post('config_meta_title'));
$config_template = $this->model_setting->edit_template($this->input->post('config_template'));
$config_maintenance = $this->model_setting->edit_maintenance($this->input->post('config_maintenance'));
redirect('admin/dashboard');
}
}
}
Model
<?php
class Model_setting extends CI_Model {
public function edit_maintenance($config_maintenance) {
$data = array(
'group' => "config",
'key' => "config_maintenance",
'value' => $config_maintenance,
);
$this->db->where('setting_id', "1");
$this->db->update('setting', $data);
}
public function edit_meta_title($config_meta_title) {
$data = array(
'group' => "config",
'key' => "config_meta_title",
'value' => $config_meta_title,
);
$this->db->where('setting_id', "2");
$this->db->update('setting', $data);
}
public function edit_template($config_template) {
$data = array(
'group' => "config",
'key' => "config_template",
'value' => $config_template,
);
$this->db->where('setting_id', "3");
$this->db->update('setting', $data);
}
}

File Upload & data entry using CodeIgniter's File Uploading Class

I am developing a form that allows the user to upload images as well as data.
I have built forms before but am trying to integrate the file uploading class with my previous controller.
Below is the Controller, Model & Form from my View, can anyone help me tie up the missing link? I have searched all over & not found a solution
class Canvas extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index() {
$vars = array();
$this->load->library('FacebookConnect');
$facebook=$this->facebookconnect->connect();
$vars['facebook'] = $facebook;
$user = $vars['user'] = $facebook->getUser();
$this->load->model('Users_Model');
if($user != 0 && sizeof($_POST)>0) {
// user already set up. Show thank you page
$this->iterate_profile ($vars['user'],false,$_POST);
$this->load->view('upload_form',$vars);
} else {
// user not set, show welcome message
$this->load->view('canvas',$vars);
}
}
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())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
function thank_you() {
$this->load->view('thank_you',$vars);
}
function remove() {
$vars = array();
$this->load->library('FacebookConnect');
$facebook=$this->facebookconnect->connect();
$vars['facebook'] = $facebook;
$vars['user'] = $facebook->getUser();
$this->load->model('Users_Model');
if($vars['user'] == 0) {
// user not set, redirect
redirect('/', 'refresh');
} else {
// user already set up. Remove
$this->load->model('Users_Model');
$this->Users_Model->remove($vars['user']);
}
$this->load->view('removed',$vars);
}
protected function iterate_profile ($user,$breadcrumb,$item) {
foreach($item as $key => $value) {
if(is_array($value)) {
$this->iterate_profile($user,$key,$value);
}
else {
if($breadcrumb) {
//echo '[' . $breadcrumb . '_' . $key . ']= ' . $value . ' <br />';
$key = $breadcrumb . '_' . $key;
}
if( ! $this->Users_Model->exists($user,$key)) {
// does not exist in the database, insert it
$this->Users_Model->add($user,$key,$value);
} else {
$this->Users_Model->update($user,$key,$value);
}
}
}
}
Model:
class Users_Model extends CI_Model {
protected $_name = 'users';
function add($id,$key,$value) {
$data = array(
'id' => $id,
'name' => $key,
'value' => $value
);
return $this->db->insert($this->_name, $data);
}
function update($id,$key,$value) {
$data = array(
'value' => $value
);
$this->db->where(array(
'id' => $id,
'name' => $key
));
return $this->db->update($this->_name, $data);
}
function exists($id,$key=null) {
if($key == null) {
$this->db->where(array(
'id' => $id
));
} else {
$this->db->where(array(
'id' => $id,
'name' => $key
));
}
$query = $this->db->get($this->_name);
if($query->num_rows() > 0) {
return true;
}
return false;
}
function remove($id) {
$data = array(
'id' => $id,
);
return $this->db->delete($this->_name, $data);
}
function all() {
$query = $this->db->get($this->_name);
$results = array();
if($query->num_rows() > 0) {
foreach($query->result() as $row) {
$results[]=$row;
}
}
return $results;
}
}
Form in view:
<form action="./" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<table>
<tr><td><b class="ara">XXXXXXXX</b> <input id="name" type="text" name="uid" value="" size="30"></td></tr>
<tr><td><b class="ara">XXXXXXXX</b><input id="dob" type="text" name="date_of_birth" value="" size="30"></td></tr>
<tr><td><b class="ara" >XXXXXXXX</b><input id="email" type="text" name="email" value="" size=30></td></tr>
<tr><td><b class="ara">XXXXXXXX</b> <input id="tel" type="text" name="telephone" value="" size="30"></td></tr>
<tr><td><input type="radio" name="device" value="11" checked><b>XXXXXXXX</b></input>
<input type="radio" name="device" value="305"><b>XXXXXXXX</b></input>
<input type="radio" name="device" value="306"><b>XXXXXXXX</b></input></td></tr>
<tr><td><b>XXXXXXXX</b><select name="reason">
<option value="volvo">XXXXXXXX</option>
<option value="saab">XXXXXXXX</option>
<option value="mercedes">XXXXXXXX</option>
<option value="audi">XXXXXXXX</option></select></td></tr>
<tr><td><b>XXXXXXXX</b><select name="game">
<option value="chuzzle">XXXXXXXX</option>
<option value="tetris">XXXXXXXX</option>
<option value="speed">XXXXXXXX</option></select></td></tr>
<tr><td><input type="file" name="userfile" class="upload" size="30"/></td></tr>
<tr><td><span class="ara tandc">XXXXXXXX</span></td><td><input id="checkme" type="checkbox" class="check" name="Check me" value="Submit"/></td></tr>
<tr><td><b><input type="button" class="btn" id="send" name="Add me" value="Submit"/></td></tr>
</table>
</form>
What error are you actually getting? One problem I see at first glance is that the form submits to ./, not do_upload, so that code probably never runs?
From index(), run do_upload() and change it so it returns true/false depending on success. If there was an error, save it to a class variable like this:
class Canvas extends CI_Controller {
private $upload_error = '';
function __construct() {
...
Then in do_upload(), instead of doing:
$error = array('error' => $this->upload->display_errors());
You can do:
$this->upload_error = array('error' => $this->upload->display_errors());
And in index(), if the result of calling do_upload() is false, read the error from $this->upload_error
Overall, your do_upload() method would look like this:
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())
{
$this->upload_error = array('error' => $this->upload->display_errors());
return false;
}
else
{
//$data = array('upload_data' => $this->upload->data());
return true;
}
}

PHP Todo CheckList Checked Feature

I need help with a PHP ToDo List. I have most of it working except for the check box to move it from a completed list to a still need to do list. here is the code I have so far:
For the edit screen:
%% views/header.html %%
<h1>{{$title}}</h1>
<div class='inputs'>
<form action="##todo/update##" method="post">
<input type="hidden" id="id" name="id" value="{{$todo['id']}}" />
<label for="description">Description:</label>
<input type="text" id="description" name="description" value="{{$todo ['description']}}" />
<label for="done">Done?:</label>
<input type="checkbox" id="done" name="done" value="1" />
<input type="submit" value="Update" />
<form>
</div>
<p><< Back</p>
%% views/footer.html %%
For the todo.inc file:
<?php
include_once "include/util.inc";
include_once "models/todo.inc";
function safeParam($arr, $index, $default) {
if ($arr && isset($arr[$index])) {
return $arr[$index];
}
return $default;
}
function get_view($params) {
$id = safeParam($params, 0, false);
if ($id === false) {
die("No todo id specified");
}
$todo = findToDoById($id);
if (!$todo) {
die("No todo with id $id found.");
}
// #formatter:off
renderTemplate(
"views/todo_view.inc",
array(
'title' => 'Viewing To Do',
'todo' => $todo
)
);
// #formatter:on
}
function get_list($params) {
$todos = findAllCurrentToDos();
$dones = findAllDoneToDos();
// #formatter:off
renderTemplate(
"views/index.inc",
array(
'title' => 'To Do List',
'todos' => $todos,
'dones' => $dones
)
);
// #formatter:on
}
function get_edit($params) {
$id = safeParam($params, 0, false);
if (!$id) {
die("No todo specified");
}
$todo = findToDoById($id);
if (!$todo) {
die("No todo found.");
}
// #formatter:off
renderTemplate(
"views/todo_edit.inc",
array(
'title' => 'Editing To Do',
'todo' => $todo
)
);
// #formatter:on
}
function post_add($params) {
if (!isset($_POST['description'])) {
die("no description given");
}
$description = htmlentities($_POST['description']);
addToDo($description);
redirectRelative("index");
}
function validate_present($elements) {
$errors = '';
foreach ($elements as $element) {
if (!isset($_POST[$element])) {
$errors .= "Missing $element\n";
}
}
return $errors;
}
function post_update($params) {
$errors = validate_present(array('id', 'description', 'done'));
if ($errors) {
die($errors);
}
$id = $_POST['id'];
$description = $_POST['description'];
$done = $_POST['done'];
updateToDo($id, $description, $done);
redirectRelative("todo/view/$id");
}
function get_delete($params) {
$id = safeParam($params, 0, false);
if (!$id) {
die("No todo specified");
}
$todo = findToDoById($id);
if (!$todo) {
die("No todo found.");
}
deleteToDo($id);
redirectRelative("index");
}
?>
Your error is in these two functions.
function validate_present($elements) {
$errors = '';
foreach ($elements as $element) {
if (!isset($_POST[$element])) {
$errors .= "Missing $element\n";
}
}
return $errors;
}
function post_update($params) {
$errors = validate_present(array('id', 'description', 'done'));
if ($errors) {
die($errors);
}
$id = $_POST['id'];
$description = $_POST['description'];
$done = $_POST['done'];
updateToDo($id, $description, $done);
redirectRelative("todo/view/$id");
}
You are attempting to validate that done exists in validate_present when called by post_update. done obviously cannot exists since it is not sent to the server when the checkbox is not checked. The $_POST does not even contain that variable, so it returns that element is missing (since it technically is). I would leave validate_present alone and change post_update as follows:
function post_update($params) {
$errors = validate_present(array('id', 'description'));
if ($errors) {
die($errors);
}
$id = $_POST['id'];
$description = $_POST['description'];
$done = (isset($_POST['done'])? 1 : 0);
updateToDo($id, $description, $done);
redirectRelative("todo/view/$id");
}

Categories