$id not being collected from the uri segment
The issue that I am having is when I go http://domain/admin/editpage/"id"
I get 404 not found and I cannot understand why the id is not being passed into the form:
Controller
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Editsale extends CI_Controller {
function __construct() {
parent::__construct();
}
function index() {
if(!$this->session->userdata('logged_in')) {
redirect('admin/home');
}
$data['title'] = "Edit Sale Name";
$data['sales_pages'] = $this->sales_model->getSalesPages();
$data['cms_pages'] = $this->navigation_model->getCMSPages();
$id = $this->uri->segment(3);
$data['id'] = $id;
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('location', 'Location', 'trim|required');
$this->form_validation->set_rules('bedrooms', 'Bedrooms', 'trim|is_natural');
$this->form_validation->set_rules('bathrooms', 'Bathrooms', 'trim');
$this->form_validation->set_rules('condition', 'Condition', 'trim');
$this->form_validation->set_rules('description', 'Description', 'trim');
$this->form_validation->set_rules('price', 'Price', 'trim');
if($this->form_validation->run() === TRUE) {
$data = ar开发者_Python百科ray(
'name' => $this->input->post('name', TRUE),
'location' => $this->input->post('location', TRUE),
'bedrooms' => $this->input->post('bedrooms', TRUE),
'bathrooms' => $this->input->post('bathrooms', TRUE),
'condition' => $this->input->post('condition', TRUE),
'description' => $this->input->post('description', TRUE),
'price' => $this->input->post('price', TRUE)
);
$this->sales_model->updateSale($data);
$data['sales'] = $this->sales_model->getSalesPages();
redirect('admin/addsale' , $data);
$this->session->set_flashdata('success', 'Page Saved');
}else{
$data['content'] = $this->load->view('admin/editsale', NULL, TRUE);
$this->load->view('template', $data);
}
}
}
Model
class Sales_model extends CI_Model
{
function __construct() {
parent::__construct();
}
function getSalesPages() {
$query = $this->db->get('sales');
if($query->num_rows() > 0) return $query->result();
}
function addSale($data) {
$this->db->insert('sales', $data);
return;
}
function updateSale($id, $data) {
$this ->db->where('id', $id);
$this->db->update('sales', $data);
}
}
View
<?php
//Setting form attributes
$formEditSale = array('id' => 'editSale', 'name' => 'editSale');
$formName = array('id' => 'name', 'name' => 'name');
$formLocation = array('id' => 'location', 'name' => 'location');
$formBedrooms = array('id' => 'bedrooms','name' => 'bedrooms');
$formBathrooms = array('id' => 'bathrooms','name' => 'bathrooms');
$formCondition = array('id' => 'condition','name' => 'condition');
$formDescription = array('id' => 'description','name' => 'description');
$formPrice = array('id' => 'price','name' => 'price');
?>
<section id = "validation"><?php echo validation_errors();?></section>
<?php
echo form_open('admin/editsale/'.$id, $formEditsale);
echo form_fieldset();
echo form_label('Name:', 'name');
echo form_input($formName);
echo form_label ('Location', 'location');
echo form_input($formLocation);
echo form_label ('Bedrooms', 'bedrooms');
echo form_input($formBedrooms);
echo form_label ('Bathrooms', 'bathrooms');
echo form_input($formBathrooms);
echo form_label ('Condition', 'condition');
echo form_input($formCondition);
echo form_label ('Price', 'price');
echo form_input($formPrice);
echo form_label ('Description', 'description');
echo form_textarea($formDescription);
echo form_submit('submit','Submit');
echo form_fieldset_close();
echo form_close();
.htaccess
# Customized error messages.
ErrorDocument 404 /index.php
# Set the default handler.
DirectoryIndex index.php
# Various rewrite rules.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|css|js|images|files|scripts|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
# Set timezone
SetEnv TZ Pacific/Auckland
Since you are using the default index
method, you need to either include it in your uri:
http://domain/admin/editsale/index/$id
Or set a route:
$route['admin/editsale/(:num)'] = 'admin/editsale/index/$1';
Or use another method:
http://domain/admin/editsale/edit/$id
The index
segment is only optional by default if you are not passing any parameters through the uri.
http://domain/admin/editsale
Best of luck.
You need to add
$data['id']=$id;
in your controller to get it worked in the view.
精彩评论