dongya0914 2015-02-26 10:15
浏览 148
已采纳

在PHP CodeIgniter foreach循环中从数据库中提取和更新数据

I have a view which contains the data which is displayed from the database by a foreach loop and i want is to get the data which is edited from the view and update it in the database one by one but the problem is that database will be updated if there is a where clause but in my case i don't know how to get the key(tid) from view to controller here is the controller function which displays the data

public function bulk()
{

    $pid = '';
    if($this->input->post('pid'))
        $pid = $this->input->post('pid');


    $template['menu'] = $this->shared_model->get_flightmenus();

    if($pid == '')
    {
        if($this->uri->segment(3))
            $pid = $this->uri->segment(3);
        else
            $pid = $template['menu'][0]['tid'];
    }

    $template['pid'] = $pid;

    // echo $pid;
    // exit();

    $template['airline'] = $this->shared_model->get_records('eat_airline');
    $template['terminal'] = $this->shared_model->get_records('eat_terminal');

    $template['data'] = $this->shared_model->get_recordbyvalue('eat_flight','menu_tid = '.$pid);

    $template['main_content'] = $this->load->view('flight/flight_bulk_view', $template, true);
    $this->load->view('includes/template', $template);



}

here is the view

    <h4><?php echo anchor('login/logout', 'Logout'); ?></h4>
<?php echo anchor('/home', 'Home'); ?>/Flights
<h2>Flights</h2>
    <div>
    <?php echo form_open('/flight/') ?>
    <?php foreach($menu as $row){ $options[$row['tid']] = $row['m_name']; } ?>
    <?php $js = 'name="pid" id="pid" onChange="this.form.submit();"'; ?>
    <?php echo form_dropdown('pid', $options, $pid, $js); ?>
    <?php echo form_close(); ?>
    </div>
<?php echo anchor('/flight/display/1/0/'.$pid, 'Create'); ?>
<?php echo anchor('flight/bulk', 'Bulk Update'); ?>
<table>
    <tr>
        <td>
            Name
        </td>
        <td>
            Airline
        </td>
        <td>
            From
        </td>
        <td>
            Stopover
        </td>
        <td>
            Destination
        </td>
        <td>
            Price
        </td>
        <td>
            Unit
        </td>
        <td>
            Tax
        </td>
        <td>
            Protection
        </td>
        <td>
            Fare
        </td>
        <td>
            Week Offer
        </td>
        <td>
            Menu
        </td>
        <td>
            Sort
        </td>
        <td>
            Edit
        </td>
        <td>
            Delete
        </td>
    </tr>
    <?php foreach($data as $row){ ?>
    <tr>
        <td>
            <?php echo $row['f_name'] ?>
        </td>
        <td>
            <?php $options1[0] = ''; foreach($airline as $row1){ $options[$row1['tid']] = $row1['a_name']; } ?>
            <?php echo $options[$row['airline_tid']]; ?>
        </td>
        <td>
            <?php $options1[0] = ''; foreach($terminal as $row1){ $options[$row1['tid']] = $row1['t_name']; } ?>
            <?php echo $options[$row['terminal_from_tid']]; ?>
        </td>
        <td>
            <?php echo $row['t_stopover'] ?>
        </td>
        <td>
            <?php $options1[0] = ''; foreach($terminal as $row1){ $options[$row1['tid']] = $row1['t_name']; } ?>
            <?php echo $options[$row['terminal_destination_tid']]; ?>
        </td>

        <td>
            <?php echo $row['f_price'] ?>
        </td>
        <td>
            <?php echo $row['f_unit'] ?>
        </td>
        <td>
            <?php if($row['f_tax']>0) echo  'Yes'; else echo 'No'; ?>
        </td>
        <td>
            <?php echo $row['f_protection'] ?>
        </td>
        <td>
            <?php echo $row['f_fare'] ?>
        </td>
        <td>
            <?php if($row['f_weekoffer']>0) echo  'Yes'; else echo 'No'; ?>
        </td>
        <!--
        <td>
            <?php $options[0] = ''; foreach($menu as $row1){ $options[$row1['tid']] = $row1['m_name']; } ?>
            <?php echo $options[$row['menu_tid']]; ?>
        </td>
        -->
        <td>
            <?php echo $row['sortindex'] ?>
        </td>
        <td>
            <?php echo anchor('flight/display/2/'.$row['tid'].'/'.$pid, 'Edit'); ?>
        </td>
        <td>
            <?php echo anchor('flight/display/3/'.$row['tid'].'/'.$pid, 'Delete'); ?>
        </td>
    </tr>
    <?php } ?>
</table>

i am unable to come up with the logic of how it will be done

any help will be appreciated

  • 写回答

2条回答 默认 最新

  • duancong2160 2015-02-26 13:38
    关注

    all i can see is that you want to pass the data from your view to controller you can do that by passing your data to url and then use it from the controller for future use.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看