【发布时间】:2018-03-15 16:45:41
【问题描述】:
我正在尝试了解 php 的工作原理,但我在理解语法和数组/对象方面遇到了问题。
- 我知道代码点火器只使用 $_POST 并将表单数据保存在一个数组中,但是如何在视图中访问它?
我在下面代码中的 cmets 中还有一些问题:
我了解 MVC 的概念,但下面的代码是一体的。
<?php
//database - formvalidation
//controller - form
//model - form
//model methods - add
//view form/add.php
class Form extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model( 'Form_model' );
$this->load->library( array( 'form_validation' ) );
}
//controller > add
public function add() {
if ( $this->form_validation->run() == FALSE ) { //displaying form if validation doesn't run
$this->load->view( 'form/add' );
}
else { //inserting data from the form into the database
//I dont understand what happens here and how to access keys in the view :/
$data = array(
'username' => $this->input->post( 'username' ),
);
$this->Form_model->add( $data ); // passing data array to the view
$this->load->view( 'form/success' ); //loading success page
}
}
//Form model > add
var $table = 'formvalidation';
public function add( $data ) {
$this->db->insert( $this->table, $data );
return $this->db->insert_id();
}
//Form model > get all items
//how can I use this function to display data in the view?
public function get_all() {
return $this->db->get( $this->table )->result_array();
}
}
?>
<? //view ?>
<?php echo form_open('form/add',array('class'=>'pure-form', 'style'=>'width:50%')); ?>
<?php echo form_error('username'); ?>
<?php foreach($asd as $a){
//What if I don't want to use foreach loop?
//How can i display field values in the view?
?>
<input type="text" name="username" value="<?php echo $asd ?>" class="w3-input" />
<?php } ?>
<div><input type="submit" value="Submit" /></div>
</form>
<? //Can you show me simple example of using objects and arrays to display data in the view? ?>
【问题讨论】:
标签: database forms codeigniter view