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 mysql , 用自己创建的本地主机和用户名 登录不上
  • ¥15 关于#web项目#的问题,请各位专家解答!
  • ¥15 vmtools环境不正常
  • ¥15 请问如何在Ubuntu系统中安装使用microsoft R open?
  • ¥15 jupyter notebook
  • ¥30 informer时间序列预测
  • ¥20 SSR引物多态性分析
  • ¥15 大漠插件在Win11易语言注册调用和免注册灵异事件,VS上注册调用完全没问题
  • ¥15 Addressable缓存机制做热更新的问题
  • ¥15 微信开发者工具vant组件