我遇到的问题是当我去http://domain/admin/editpage/"id"
时,我找不到404,我无法理解为什么id没有被传递到表单中:
控制器
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 = array(
'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);
}
}
}
模型
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);
}
}
查看
<?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
答案 0 :(得分:1)
由于您使用的是默认的index
方法,因此您需要将其包含在您的uri中:
http://domain/admin/editsale/index/$id
或设定路线:
$route['admin/editsale/(:num)'] = 'admin/editsale/index/$1';
或使用其他方法:
http://domain/admin/editsale/edit/$id
如果你没有通过uri传递任何参数,index
段只是默认是可选的。
http://domain/admin/editsale
祝你好运。
答案 1 :(得分:0)
您需要添加
$data['id']=$id;
在你的控制器中让它在视图中工作。