Codeigniter单击单选按钮并打开修改模型,该模型显示数据库中的数据

时间:2017-06-08 18:36:10

标签: php codeigniter

在我的代码中有一个显示搜索结果的表。当用户单击单选按钮然后单击修改项目按钮时,它应该打开模式,其中包含有关根据项目ID从数据库获取的项目的信息。目前,模式会打开,其中包含结果页面上所有项目的信息,不仅仅是使用单选按钮选择的项目我不知道为什么会这样做&#39 ; s没有取出单选按钮的值并显示所有内容。

这是我的代码: 控制器:

class Search extends CI_Controller {
public function __construct(){
    parent::__construct();
    $this->load->helper('url');
    $this->load->helper('form');
    $this->load->model('searchModel');
    $this->load->model('itemModal');
}
public function index(){
    $this->load->view('base');
    $this->load->view('search');
}
public function displayItem(){
//modify item button is clicked
    if(isset($post['#modifyItem'])){
      //radio button is checked
        if(isset($post['id'])){
           //value from the radio button
            $id=$this->input->post("id");
            $data['results'] = $this->itemModal->get_item_by_id($id);
            //open modal with the results
            $this->load->view('searchResult/#modifyItem',$data);
        }
    }       
}}

型号:

<?php
class ItemModal extends CI_Model {
function __construct(){
    parent::__construct();
}
function get_item_by_id($id){
    $this->db->select('*');
    $this->db->where('inventoryID =',$id);

    // Execute the query.
    $query = $this->db->get('inventory');

    // Return the results.
    return $query->result_array();
}
}

查看:

 <body>
    <h1><center>Item List</center></h1>
    <hr>
     <div class="container">
         <form method="post" action="<?php echo site_url('itemView/viewItems'); ?>">
                <table>
                    <tr>
                        <th><center><input type="radio" name="id"></center></th>
                        <th>Inventory ID</th>
                        <th>Master Code</th>
                        <th><center>Item Name</center></th>
                        <th>Color Name</th>
                        <th><center>Location</center></th>
                        <th><center>Checkout Allowed</center></th>
                    </tr>
                    <?php foreach($results as $rows):?>
                    <tr>
                        <td><input type="radio" name="id" value="<?php echo $rows['inventoryID'] ?>" <?php echo set_radio('id', '$rows[inventoryID]'); ?>></td>
                        <td><a href="<?php echo site_url('itemView/viewItems/'.$rows['inventoryID']); ?>"><?php echo $rows['inventoryID'] ?></a></td>
                        <td><?php echo $rows['masterCode'] ?></td>
                        <td><?php echo $rows['itemName'] ?></td>
                        <td><?php echo $rows['colorName'] ?></td>
                        <td><?php echo $rows['location'] ?></td>
                        <td><input type="checkbox" <?php if($rows['checkoutAllowed'] == 'Yes') echo " checked='checked' "; ?>></td>
                    </tr>
                    <?php endforeach; ?>
                </table>
         </form><br><br>
         <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modifyItem" data-title="Modify an Item" onclick="<?php echo site_url("Search/displayItem"); ?>">Modify an Item</button>
         <!-- Modify an Item Modal -->
                <div id="modifyItem" class="modal fade">
                    <div class="modal-dialog">
                        <form action="<?php echo site_url("Search/updateItem"); ?>" method='POST'>
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                                <h4 class="modal-title">Modify an Item</h4>
                            </div>
                            <div class="modal-body">
                                <form role="form">
                                    <?php foreach($results as $rows):?>
                                    <table>
                                    <tr>
                                        <td><input type="text" name="rfid" value="<?php echo $rows['inventoryID']?>"/></td>
                                        <td><input type="text" name="itemCode" placeholder="Item Code"/></td>
                                        <td><input type="text" name="masterCode" value="<?php echo $rows['masterCode']?>"/></td>
                                    </tr>
                                    <tr>
                                        <td><input type="text" name="itemName" value="<?php echo $rows['itemName']?>" /></td>
                                        <td><input type="text" name="colorCode" placeholder="Color Code" /></td>
                                        <td><input type="text" name="colorName" placeholder="Color Name" /></td>
                                    </tr>
                                    <tr>
                                        <td><input type="text" name="location" placeholder="Location" /></td>
                                        <td><input type="text" name="makelocation" placeholder="Location Made"/></td>
                                        <td><input type="text" name="itemCategory" placeholder="Item Category" /></td>
                                    </tr>
                                    <tr>
                                        <td><input type="text" name="materialDescription" placeholder="Material Description" /></td>
                                        <td><input type="text" name="supplier" placeholder="Supplier/Vendor" /></td>
                                        <td><input type="text" name="checkoutAllowed" placeholder="Checkout Allowed" /></td>
                                    </tr>
                                </table>
                                    <div class="row personal-info">
                                        <div class="col-sm-4">
                                            <div class="form-group">
                                                <textarea name="itemDescription" placeholder="Insert information regarding the weather this item is suitable for and where it is used"></textarea>
                                                <textarea name="Comments" placeholder="Additional Coments on the Item"></textarea>
                                            </div>
                                        </div>
                                    </div>
                                    <?php endforeach; ?>
                                </form>
                            </div>
                            <div class="modal-footer" style="text-align:center;">
                                <input type="submit" class="btn btn-primary" name="modifyItem" value="Modify Item">
                            </div>
                        </div>
                        </form>
                    </div>
                </div>
                <!-- Modify an Item Modal -->
         </div></body>

1 个答案:

答案 0 :(得分:1)

为什么你加载两次foreach s not good way when multiple record on database for load all data it s使用更多时间使用codei-gniter的显示记录分页库并使用ajax编辑动态模型代码 你在控制器中的功能&display; displayItem&#39;编写修改项目的代码并编辑表格html

      <table>
                <tr>
                    <th><center><input type="radio" name="id"></center></th>
                    <th>Inventory ID</th>
                    <th>Master Code</th>
                    <th><center>Item Name</center></th>
                    <th>Color Name</th>
                    <th><center>Location</center></th>
                    <th><center>Checkout Allowed</center></th>
                </tr>
                <?php foreach($results as $rows):?>
                <tr>
                    <td><input type="radio" name="id" value="<?php echo $rows['inventoryID'] ?>" <?php echo set_radio('id', '$rows[inventoryID]'); ?>></td>
                    <td><a href="<?php echo site_url('itemView/viewItems/'.$rows['inventoryID']); ?>"><?php echo $rows['inventoryID'] ?></a></td>
                    <td><?php echo $rows['masterCode'] ?></td>
                    <td><?php echo $rows['itemName'] ?></td>
                    <td><?php echo $rows['colorName'] ?></td>
                    <td><?php echo $rows['location'] ?></td>
                    <td><input type="checkbox" <?php if($rows['checkoutAllowed'] == 'Yes') echo " checked='checked' "; ?>></td>
                </tr>
                <?php endforeach; ?>
            </table>


     <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modifyItem" data-title="Modify an Item" onclick="updateItem();">Modify an Item</button>
     <!--Bootstrap model for edit start-->
    <div id="myModal" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content" id="model_data">
                  //append form data here
            </div>
        </div>
    </div>
 <!--Bootstrap model for edit end-->
 <script>
 function updateItem()
{

    var CHECKBOXIDS = PASS_CHECKBOX_CHECKEDVALUE;
    $('#model_data').html('');
    $.ajax({
        url: "<?php echo site_url('Search/displayItem');?>",
        type: "POST",
        dataType: "html",
        data: {'<?php echo $this->security->get_csrf_token_name(); ?>': '<?php echo $this->security->get_csrf_hash(); ?>', 'checkids': CHECKBOXIDS, },
        catch : false,
        success: function (data) {
            $('#model_data').append(data);

        }
    });
}