douzhuo2722 2017-06-08 13:01
浏览 29
已采纳

显示有关项目php codeigniter的更多信息

I have a database table in mysql that has 11 entities. When the user first searches for an item, my code displays 5 entities and then there is an link for the user to click to view more information on the item. When the user click the link it should lead them to a new page with information about the item that is clicked based on itemID. I don't know how to pass the itemID of the entity to the controller. Please help! Here's my code: Contoller:

 <?php defined('BASEPATH') OR exit('No direct script access allowed');
class ItemView extends CI_Controller {
public function __construct(){
    parent::__construct();
    $this->load->helper('url');
    $this->load->helper('form');
    $this->load->model('itemModal');
}
public function index(){
    $this->load->view('base');
    $this->load->view('searchResult');
}
public function viewItems(){
    //trying to get the id of the clicked item
    $id = $this->input->post($rows['inventoryID']);
    $data['results'] = $this->itemModal->get_items($id);
    $this->load->view('base.php',$data);
    $this->load->view('itemview.php',$data);
}
}
?>

Model:

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

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

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

}}?>

View:

<body>
    <h1><center>Item List</center></h1>
    <hr>
     <div class="container">
         <form method="post" action="<?php echo site_url('itemView/index'); ?>">
                <table>
                    <tr>
                        <th><input type="radio" name="chk"></th>
                        <th>Inventory ID</th>
                        <th>Master Code</th>
                        <th>Item Name</th>
                        <th>Color Name</th>
                        <th>Location</th>
                        <th>Link to more information</th>
                    </tr>
                    <?php foreach($results as $rows):?>
                    <tr>
                        <td><input type="radio" name="chk"></td>
                        <td><?php echo $rows['inventoryID'] ?></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><a href="<?php echo site_url('itemView/viewItems'); ?>">click to view more information</a></td>
                    </tr>
                    <?php endforeach; ?>
                </table>
         </form>
 </div></body>

View for itemView

<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>css/search.css">
    <title>Item Information Page</title>
</head>
<body>
    <h1><center>Item Information</center></h1>
    <hr>
    <div class="container">

    </div>
    <!-- End of Container -->
</body><br><br>
</html>
  • 写回答

1条回答 默认 最新

  • dongpan1308 2017-06-08 13:44
    关注

    yo need to change like this controller

    <?php defined('BASEPATH') OR exit('No direct script access allowed');
        class ItemView extends CI_Controller {
            public function __construct(){
                parent::__construct();
                $this->load->helper(array('url','form'));
                $this->load->model('itemModal', 'model_items');
            }
    
            public function index(){
                //get all items from db to display basic info in form
                $data['items']=$this->model_items->get_all();
                $this->load->view('base');
                $this->load->view('searchResult', $data);
            }
    
            //get the specific item to be updated
            public function updateItem( $id ){
                $data['results'] = $this->model_items->get_item_by_id($id);
                $this->load->view('base',$data);
                //viw with inpus form to change the info and send to controller to be updated
                $this->load->view('itemview',$data);
            }
    
            //get all items to be updated
            public function updateItems(){
                foreach($this->input->post() as $item => $value){
                    ${$item} = $value;
                }
    
                //here you need to add logic to make wherever you want with all items selected in form
            }
        }
    

    model itemModal

    <?php 
        class ItemModal extends CI_Model {
            function get_all(){
                return $this->db->get('inventory')->result();
            }
    
            function get_item_by_id($id){
                return $this->db->get_where('inventory', array('id' => $id))->row();
            }
        }
    

    view searchResult

    <form method="post" action="<?= site_url('itemView/updateItems'); ?>">
                <table>
                    <tr>
                        <!-- select one or more items to be updated bach -->
                        <th><input type="checkbox" name="chk" value="<?= $rows->inventoryID ?>"></th>
                        <th>Inventory ID</th>
                        <th>Master Code</th>
                        <th>Item Name</th>
                        <th>Color Name</th>
                        <th>Location</th>
                        <th>Link to more information</th>
                    </tr>
                    <?php foreach($itemsas $rows):?>
                    <tr>
                        <td><input type="radio" name="chk"></td>
                        <td><?= $rows->inventoryID ?></td>
                        <td><?= $rows['masterCode'] ?></td>
                        <td><?= $rows['itemName'] ?></td>
                        <td><?= $rows['colorName'] ?></td>
                        <td><?= $rows['location'] ?></td>
                        <!-- update specific item -->
                        <td><a href="<?= site_url('itemView/updateItem/'.$rows->inventoryID ); ?>">click to view more information</a></td>
                    </tr>
                    <?php endforeach; ?>
                    <tr>
                        <td>Update all items selected</td>
                        <td><input type="submit" value="Update"></td>
                    </tr>
                </table>
         </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥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 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?