【问题标题】:Code igniter form - passing post data and populating fields from databaseCodeigniter 表单 - 传递帖子数据并从数据库填充字段
【发布时间】:2018-03-15 16:45:41
【问题描述】:

我正在尝试了解 php 的工作原理,但我在理解语法和数组/对象方面遇到了问题。

  1. 我知道代码点火器只使用 $_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


【解决方案1】:

如果验证失败,您希望使用发布数据重新填充字段,以便用户不必为未失败的输入重新输入数据。

您可以通过在表单视图中使用set_value($fieldname) 方法来做到这一点。

<?php echo form_open('form/add',array('class'=>'pure-form', 'style'=>'width:50%')); ?>
<?php echo form_error('username'); ?>
<input type="text" name="username" value="<?php echo set_value('username'); ?>" class="w3-input" />
<input type="submit" value="Submit" />

现在,如果发生验证错误,用户发布的任何内容都将显示在 username 字段中。


在这个函数中:

public function get_all() {
    return $this->db->get( $this->table )->result_array();
}   

您正在使用result_array()result_array()row_array() 的不同之处在于表中的所有用户都将作为数组返回(只要您没有唯一的 where 条件),例如array( 0 =&gt; array('username'=&gt;'bob'), 1 =&gt; array('username'=&gt;'jeff'));。因此,您可以生成一个表或所有用户的任何内容:

在控制器中:$this-&gt;load-&gt;view('form/success', array('users' =&gt; $this-&gt;form_model-&gt;get_all());

在视图中:

foreach ($users as $user) {
    echo $user['username'];
}

回声:鲍勃·杰夫

但我认为您只想获取刚刚添加的用户...在这种情况下,像这样的模型函数会很好地工作:

public function get_one($id) {
    $this->db->where('id', $id);
    return $this->db->get( $this->table )->row_array();
}   

然后在控制器中:

if ($this->form_validation->run() == FALSE) {
        $this->load->view('form/add');
    } else {
        $data = array(
            'username' => $this->input->post('username'),
        );
        $id = $this->Form_model->add($data);
        $user = $this->Form_model->get_one($id);
        $this->load->view('form/success', array('user' => $user));
    }

在视野中:

echo $user['username'];

result()row() 生成的对象分别与它们的对应对象result_array()row_array() 相同,但对象是通过-&gt; 而不是[$somekey] 访问的。在上一个示例中,如果您在 get_one() 中使用 row() 而不是 row_array() 传递了一个对象,您将在如下视图中访问它:$user-&gt;username;。将数据分配给视图通常在上述方法中处理,但更多信息您可以view the docs.

【讨论】:

  • 非常感谢您的回答。它推动了我不起眼的代码点燃灰质,并激励我编写一个简单的示例并帮助他人。但请期待更多问题 =)
【解决方案2】:

好的。我将帖子值与数据库函数值混淆了。现在这个主题对我来说有点光明了。往下看:

我不知道在哪里可以找到用户指南中的信息:/

<?php
defined( 'BASEPATH' )OR exit( 'No direct script access allowed' );

class Lol extends CI_Controller {

function __Construct() {
    parent::__Construct();
    //loading helpers, models, libraries
    $this->load->database();
    $this->load->helper( array( 'form' ) );
    $this->load->library( array( 'form_validation' ) );
}
//form remembers the username field value
public
function index() {
    echo form_open( 'lol' );
    $val = set_value('username');
    echo form_input( 'username', $val );
    echo form_submit( 'submit', 'Submit' );
    echo form_close();
}

public
function validation() {
    //setting up the form validation
    $this->form_validation->set_rules( 'username', 'Username', 'required|min_length[3]' );

    //displaying form if validation doesn't run
    if ( $this->form_validation->run() == FALSE ) {
        echo form_error( 'username' );
        echo form_open( 'lol/validation' );
        $val = set_value('username');
        echo form_input( 'username', $val );
        echo form_submit( 'submit', 'Submit' );
        echo form_close();
    }
    //inserting data from the form into the database
    else {
        echo "username is ok";
    }
}
//getting field value from database in two ways: array and object. Your pick.
public
function database() {
    echo form_open( 'lol' );
    $val = $this->get_all();
    //echo form_input( 'username', $val->username ); // object
    echo form_input( 'username', $val['username'] ); //array
    echo form_submit( 'submit', 'Submit' );
    echo form_close();
}
//getting database data
public function get_all() {
    //return $this->db->get('formvalidation')->row(); //object
    return $this->db->get('formvalidation')->row_array(); //array
} 

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多