你好我是codeigniter的新手,我坚持从数据库显示数据。我试图找到解决方案,但却无法正确理解它们。所以有人可以帮我这个吗?真的需要你的专家建议,谢谢你!
查看文件Userinsert_view.php
<html>
<head>
<title>Insert Data Into Database Using CodeIgniter Form</title>
</head>
<body>
<div id="container">
<?php echo form_open('Userinsert_controller'); ?>
<h1>Insert Data Into Database Using CodeIgniter</h1><hr/>
<?php if (isset($message)) { ?>
<CENTER><h3 style="color:green;">Data inserted successfully</h3></CENTER><br>
<?php } ?>
<?php echo form_label('Student Name :'); ?> <?php echo form_error('dname'); ?><br />
<?php echo form_input(array('id' => 'dname', 'name' => 'dname')); ?><br />
<?php echo form_label('Student Email :'); ?> <?php echo form_error('demail'); ?><br />
<?php echo form_input(array('id' => 'demail', 'name' => 'demail')); ?><br />
<?php echo form_label('Student Mobile No. :'); ?> <?php echo form_error('dmobile'); ?><br />
<?php echo form_input(array('id' => 'dmobile', 'name' => 'dmobile', 'placeholder' => '10 Digit Mobile No.')); ?><br />
<?php echo form_label('Student Address :'); ?> <?php echo form_error('daddress'); ?><br />
<?php echo form_input(array('id' => 'daddress', 'name' => 'daddress')); ?><br />
<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
<?php echo form_close(); ?><br/>
<div id="fugo">
</div>
</div>
</body>
</html>
控制器文件
Userinsert_controller.php
<?php
class Userinsert_controller extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('Userinsert_model');
}
function index() {
//Including validation library
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
//Validating Name Field
$this->form_validation->set_rules('dname', 'Username', 'required|min_length[5]|max_length[15]');
//Validating Email Field
$this->form_validation->set_rules('demail', 'Email', 'required|valid_email');
//Validating Mobile no. Field
$this->form_validation->set_rules('dmobile', 'Mobile No.', 'required|regex_match[/^[0-9]{10}$/]');
//Validating Address Field
$this->form_validation->set_rules('daddress', 'Address', 'required|min_length[10]|max_length[50]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('Userinsert_view');
} else {
//Setting values for tabel columns
$data = array(
'Student_Name' => $this->input->post('dname'),
'Student_Email' => $this->input->post('demail'),
'Student_Mobile' => $this->input->post('dmobile'),
'Student_Address' => $this->input->post('daddress')
);
//Transfering data to Model
$this->Userinsert_model->form_insert($data);
$data['message'] = 'Data Inserted Successfully';
//Loading View
$this->load->view('Userinsert_view', $data);
}
}
模型文件
Userinsert_model.php
<?php
class Userinsert_model extends CI_Model{
function __construct() {
parent::__construct();
}
function form_insert($data){
// Inserting in Table(students) of Database(college)
$this->db->insert('students', $data);
}
}
?>
答案 0 :(得分:0)
我已经读过您的问题,您没有编写用于获取数据的代码。您只在数据库中提交了它。
这是一个简单的示例,因此您可以轻松找到解决方案
在您的控制器文件中:
public function view()
{
$this->load->model('uModel'); //edit it with you model name
$data['users']= $this->uModel->All();
$this->load->view('list' , $data);
}
在模型文件中:
//Func for getting all data of a table in a 'users' variable by using get method
public function All()
{
return $users = $this->db->get('users')->result_array();
}
并在视图文件夹中创建一个表视图文件,您可以在其中获取数据
<tbody>
<?php if (!empty($users)) { foreach ($users as $user) { ?>
<tr>
<td> <?php echo $user['userid'] ?></td>
<td> <?php echo $user['name'] ?></td>
</tr>
<?php } }
?>
希望它会为您提供帮助