无法从Codeigniter的角度看待价值

时间:2018-11-08 01:50:16

标签: php codeigniter

我一直在为我的班级开发此codeigniter php应用程序,并且有一个视图,您可以在其中选择一定数量的硬币,并且所选择的数量返回为空。

我的控制器:

public function add(){

    $this->form_validation->set_rules('quantity', 'Quantity', 'required');

    if(!$this->session->userdata('logged_in')){
        redirect('users/login');
    }

    $inventorycoin = $this->uri->segment('3');
    $inventoryquantity = $this->input->post('quantity');

        $this->coin_model->add_coin($inventorycoin, $inventoryquantity);
        //$this->session->set_flashdata('post_deleted', 'Post has been deleted');
        redirect('coins');
    }

我的模特:

public function add_coin($inventorycoin, $inventoryquantity){

        $data = array (
            'inventory_user' =>  $this->session->userdata('user_id'),
            'inventory_quantity' => $inventoryquantity,
            'inventory_coin' => $inventorycoin
        );

        return $this->db->insert('inventory', $data);
    }

我的观点:

<?php echo form_open_multipart('coins/add', 'id="coins"'); ?>
<table width="100%">

<?php foreach($coins as $coin) : ?>
            <tr>
            <td><img  class="post-thumbnail" style="width: 200px; height: 200px" src="<?php echo site_url(); ?>assets/images/coins/<?php echo $coin['coin_image']; ?>"></td>
            <td><?php echo $coin['coin_name']; ?></td>
            <td><?php echo $coin['coin_country']; ?></td>
            <td><?php echo $coin['coin_weight']; ?></td>
            <td><input type="number" min="0" name="quantity" placeholder="Qt" style="width: 40px"></td>
            <td><p><a class="btn btn-primary" href="<?php echo base_url(); ?>coins/add/<?php echo $coin['coin_id']; ?>">Add</a></p></td>
            </tr>   
<?php endforeach; ?>

        </table>
        </form>

可能是什么问题?

2 个答案:

答案 0 :(得分:0)

您不使用提交按钮。因此,这部分将无法正常工作。

 $inventoryquantity = $this->input->post('quantity');

您正在循环input,因此请尝试将表单更改为此。

        <?php echo form_open_multipart('coins/add/', 'id="coins"'); ?>
        <table width="100%">

        <?php foreach($coins as $coin) : ?>
                    <tr>
    <input name="inventorycoin[]" type="hidden" value = "<?=$coin['coin_id'];?>" >
                    <td><img  class="post-thumbnail" style="width: 200px; height: 200px" src="<?php echo site_url(); ?>assets/images/coins/<?php echo $coin['coin_image']; ?>"></td>
                    <td><?php echo $coin['coin_name']; ?></td>
                    <td><?php echo $coin['coin_country']; ?></td>
                    <td><?php echo $coin['coin_weight']; ?></td>
                    <td><input type="number" min="0" name="quantity[]" placeholder="Qt" style="width: 40px"></td>
                    <td>--</td>
                    </tr>   
        <?php endforeach; ?>

                </table>
<button type="submit"> Add </button>
                </form>

和您对此的功能

public function add(){

    $this->form_validation->set_rules('quantity', 'Quantity', 'required');

    if(!$this->session->userdata('logged_in')){
        redirect('users/login');
    }
    $inventorycoin = $this->input->post('inventorycoin');

    $inventoryquantity = $this->input->post('quantity');

    echo "<pre>";print_r($inventorycoin);
    echo "<pre>";print_r($inventoryquantity); // you need to loop this because it is an array

        redirect('coins');
    }

答案 1 :(得分:0)

您可以在视图中更改以下行:

<td><p><a class="btn btn-primary" href="<?php echo base_url(); ?>coins/add/<?php echo $coin['coin_id']; ?>">Add</a></p></td>

尝试一下。

<td><p><a type="submit" class="btn btn-primary" href="<?php echo base_url('coins/add/'.$coin['coin_id']); ?>">Add</a></p></td>