Php 使用codeigniter显示数据库中的数据
嗨,我是codeigniter的新手,我一直在显示数据库中的数据。我试图找到解决办法,但还是无法正确理解。有人能帮我吗?真的需要你的专家建议谢谢 查看文件Userinsert_View.phpPhp 使用codeigniter显示数据库中的数据,php,codeigniter,Php,Codeigniter,嗨,我是codeigniter的新手,我一直在显示数据库中的数据。我试图找到解决办法,但还是无法正确理解。有人能帮我吗?真的需要你的专家建议谢谢 查看文件Userinsert_View.php <html> <head> <title>Insert Data Into Database Using CodeIgniter Form</title> </head> <body> <div id="container"&
<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>
使用CodeIgniter表单将数据插入数据库
使用CodeIgniter将数据插入数据库
数据插入成功
控制器文件
Userinsert\u 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);
}
}
控制器功能中的
公共函数GetAll(){
$data['all_data']=$this->Userinsert_model->selectAllData();
$this->load->view('view\u page',$data);
}
模型中
公共函数selectAllData(){
$query=$this->db->get('students');
返回$query->result();
}
鉴于
我已经阅读了您的问题,您没有编写获取数据的代码。您只在数据库中提交了它
下面是一个简单的示例,您可以轻松找到解决方案
在控制器文件中:
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 } }
?>
希望它能帮助您什么不起作用?为什么您在视图中成功地编写了插入的数据
<代码>echo$消息代码>更好在哪里显示数据?我可以看到,您只为在数据库中插入数据编写了代码,但您没有编写一行代码来显示数据。我们如何知道您需要在何处显示数据?请解释@AbhishekSingh我删除了代码,只是上传了正常工作的代码!!在任何可以使用veiw.php文件显示数据的地方,我都可以向您展示我的代码,如果您这么说的话,我想做什么@旁白,当你做这个“学生名字”=>this->input->post('dname'),就像这样做“学生名字”=>html\u escape($this->input->post('dname'))。请退出您的输入。这只是一个好习惯,即使表单已经退出谢谢,先生非常感谢!!!我正在做类似的事情,但我查看文件时无法正确编写代码!!!非常感谢@pervaz先生!!
//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 } }
?>