如何获取当前ID以更新特定ID

时间:2018-12-07 08:23:47

标签: php codeigniter

我正在使用first,next,prev,last links移至受关注的条目。.现在我的问题是,如果我想将值更新为当前ID,我想通过该ID来更新控制器功能。当前的ID? 这就是我将值从视图页面传递到控制器的方式。

更新视图:

 <a href='<?php 'BookKeeping/update/.$id[recno];'?>' class='button'><i class="icon-loop3 position-left">Update</button></i></a>

用于更新的控制器:

    public function update($id="")
{
$dc=$this->input->post('dc');
            if($dc=='c'){
            $amount=$this->input->post('credit1');
                        } 
            else if ($dc=='d') {
            $amount=$this->input->post('debit');
                                }

$data1=array(
      'date' =>$this->input->post('TDate'),
      'code' =>$this->input->post('TName'),
      'project' =>$this->input->post('TName1'),
      'part' =>$this->input->post('part1'),
      'part1' =>$this->input->post('part2'),
      'dc'=>$this->input->post('dc'),
      'amount'=>$amount,
   );
$this->db->where('recno', $id);
$this->db->update('daybook', $data1); 
redirect('BookKeeping/daybook','refresh');
}
    }

查看要移动的页面 first,next,last,prev

 <a href='<?php echo base_url().'BookKeeping/daybook/'.$firstID['first']; ?>' class='button'><i class="icon-first">First</i></a><br><br>
                                    <?php
                                        if($currentID != $lastID['last']){
                                            foreach ($allData as $next_key => $next_value) {
                                                //echo $next_key+1;
                                                //echo '<pre>';print_r($next_value);exit();
                                                if($currentID == $next_value['recno']){
                                                   $nextID = $allData[$next_key+1];

                                                }
                                            }
                                            $anchorTagNext = base_url().'BookKeeping/daybook/'.$nextID['recno'];
                                        }else {
                                            $anchorTagNext = '#';
                                        }
                                    ?>
                                        <a href='<?php echo $anchorTagNext; ?>' class='button'><i class="icon-next">Next</i></a><br><br>
                                    <?php
                                        if($currentID != $firstID['first']){
                                            foreach ($allData as $prev_key => $prev_value) {
                                                //echo '<pre>';print_r($prev_value);exit();

                                                if($currentID == $prev_value['recno']){
                                                    //echo $allData[$prev_key-1]['recno'];exit();
                                                   $prevId = $allData[$prev_key-1];
                                                }
                                            }
                                            $anchorTagPrevious = base_url().'BookKeeping/daybook/'.$prevId['recno'];
                                        } else {
                                            $anchorTagPrevious = '#';
                                        }
                                    ?>
                                    <a href='<?php echo $anchorTagPrevious; ?>' class='button'><i class="icon-previous">Previous</i></a><br><br>
                                    <a href='<?php echo base_url().'BookKeeping/daybook/'.$lastID['last']; ?>' class='button'><i class="icon-last">Last</i></a>

    </ul>

要移动的控制器 first,next,prev,last

$first = $this->db->query('SELECT MIN(recno) AS `first` FROM `daybook` ORDER BY recno DESC LIMIT 1')->row_array();

    $firstID = $this->db->query("SELECT * FROM `daybook`  where recno='$first[first]' ORDER BY recno ASC")->result_array();
    //$firstID = $firstIDQuery->result_array();
    $last = $this->db->query('SELECT MAX(recno) AS `last` FROM `daybook` ORDER BY recno DESC LIMIT 1')->row_array();
    $lastID = $this->db->query("SELECT * FROM `daybook` where recno='$last[last]'  ORDER BY recno")->result_array();
    //$lastID = $lastIDQuery->result_array();

    //$id = 1;
    if(!empty($id)){
        $result = $this->db->query("SELECT * FROM daybook WHERE recno = $id")->row_array();
    }else{
        $result = "";
    }

    $allData = $this->db->query("SELECT * FROM daybook ORDER BY recno ASC")->result_array();

        //$result = $resultQuery->result_array();

    if(!empty($id)){
        $data['currentID'] = $id;
    }else {
        $data['currentID'] = $first['first'];
    }
    $data['firstID'] = $first;
    $data['lastID'] = $last;
    $data['result'] = $result;
    $data['allData'] = $allData;

帮助我将 currrent id 传递给 controller

2 个答案:

答案 0 :(得分:0)

可以通过将currentID值传递给控制器​​来完成 更新调用必须在表单标签中定义

<form  class="form-horizontal" method="POST" action="<?=site_url('BookKeeping/update/'.$currentID)?>">
                            <input type="hidden" name="id" id="id"  value="<?php echo $currentID ?>">
                    <button type="submit" class="btn btn-info btn-xs"><i class="glyphicon glyphicon-ok"></i>update</button>

控制器代码:

 public function update($id='')
{

      $session_data = $this->session->userdata('logged_in');
        $data['username'] = $session_data['username'];

        $id=$this->input->post('id');
        echo "<pre>";var_dump($id);
            $dc=$this->input->post('dc');
            if($dc=='c'){
            $amount=$this->input->post('credit1');
                        } 
            else if ($dc=='d') {
            $amount=$this->input->post('debit');
                                }

$data=array(
      'date' =>$this->input->post('TDate'),
      'code' =>$this->input->post('TName'),
      'project' =>$this->input->post('TName1'),
      'part' =>$this->input->post('part1'),
      'part1' =>$this->input->post('part2'),
      'dc'=>$this->input->post('dc'),
      'amount'=>$amount,
   );
$this->db->where('recno', $_POST['id']);
$this->db->update('daybook', $data);

答案 1 :(得分:-1)

如果您在使用省略自动生成的主键进行INSERT之后尝试检索自动生成的ID ,则可以使用为此提供的简单帮助程序

$last_autogenerated_id = $this->db->affected_rows()

在执行插入查询之后。

您可以深入研究文档中有关Query Helper Method的内容