使用codeigniter将文本字段与行中的主键相关联

时间:2016-05-30 23:55:15

标签: php mysql codeigniter

我正在尝试构建出价申请表。我希望关联一个textfield与输出行的主键。例如,textfield是bid_amount

在输入出价金额时,我希望输入truckerid,出价(luggage_id)和产品出价(<h2 class="page-header"> <i class="fa fa-legal"></i> &nbsp <font color="orange"> Loads To Bid For </font> </h2> </div><!-- /.col --> </div> <?php foreach ($h->result() as $row) {} ?> <div class="row invoice-info"> <div class="col-sm-1 invoice-col"> </div> <div class="col-sm-3 invoice-col"> <img src="<?php echo base_url()?>/res/images/goods/1.png"> </div> <div class="col-sm-4 invoice-col"> <address> Description: <?php echo $row->description;?><br> Location Address: <?php echo $row->l_area;?><br> Destination Address: <?php echo $row->d_area;?><br> Date: <?php echo $row->dom;?><br> Time: <?php echo $row->tom;?> </address> </div> <div class="col-sm-2 invoice-col"> <address> </address> </div><!-- /.col --> <div class="col-sm-2 invoice-col"> <?php echo form_open('truckeraccount_ctrl/bid'); ?> <input type="hidden" class="form-control" name="truckerid" value="<?php $truckerid = $this->session->userdata('truckerid'); echo $truckerid; ?>" required> <input type="text" class="form-control" name="bid_amount[$row->luggage_id]" placeholder="Bid"> <button type="submit" class="btn bg-orange btn-flat margin">Place Bid</button> </div> </div> )的人存储在出价表中。< / p>

要输出的输出产品存储在另一个表(寄售表)下面是我的模型,视图和控制器。

查看

public function bid() {
    $this->load->model('Truckeraccount_model');
    //  $luggage_id = $this->uri->segment(3);
    $this->Truckeraccount_model->bid();
    redirect('truckeraccount_ctrl');                    
}
    

控制器

function bid() {  
    $data = array(
        'luggage_id' => $this->input->post('luggage_id'),
        'bid_amount' => $this->input->post('bid_amount'),
        'truckerid' => $this->input->post('truckerid')
    );
    //$this->db->where('luggage_id', $luggage_id);
    $query=$this->db->update('bids', $data);

    return $query;
  }

模型

{{1}}

2 个答案:

答案 0 :(得分:1)

您可以通过更改名称将输入用作关联数组。这将使数组中的键成为产品ID

尝试

<input type="text" name="bidAmount[$row->productId]" />

然后在你的PHP中你可以遍历值

foreach ($_POST['bidAmount'] as $productId => $bidAmount) {
    // do your logic here
}

答案 1 :(得分:0)

您可以使用隐藏文字框来获取出价。

<?php
foreach ($products->result() as $row)  
{ ?>  
<?php echo $row->price;?>       
<input type="hidden" name="bid[]" value="<?php echo $row->bid;?>">
<input type="text" name="bidAmount[]" placeholder="place bid amount">
<?php }  ?>

现在bid和bidAmount都具有相同的数组计数。因此,使用for循环来获取出价值和bidAmount值。

for ($i=0; $i < count($_POST['bid']); $i++) { 
    $bid = $_POST['bid'][$i]; \\ You can get bid value here
    $bidAmount = $_POST['bidAmount'][$i]; \\ You can get bid amount value here
}

希望这个答案会有所帮助。如果它是正确的,那么不要忘记标记为正确的答案。欢呼声。