Tạo view codeigniter 3

Lượt xem: 2333

Tạo view codeigniter 3

Đây là bước cuối cùng của serial  tạo ứng dụng đơn giản CRUD Codeigniter 3 cùng dandev. Trong bước này chúng ta sẽ tạo các file php cho phần view. Ngoài việc tạo view cho item chúng ta cần tạo thêm các file bố cục của web. Chúng gồm các file như sau:

1.header.php

2.footer.php

3.list.php

4.create.php

5.edit.php

6.show.php

 

Vì vậy chúng ta cần tạo các tệp với mã như sau:

application/views/theme/header.php

<!DOCTYPE html>
<html>
<head>
   <title>Tạo ứng dụng CRUD đơn giản với codeigniter 3 sử dụng bootstrap và mysql</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
</head>
<body>
<div class="container">

application/views/theme/footer.php

</div>
</body>
</html>

application/views/items/list.php

<div class="row">  
  <div class="col-lg-12 margin-tb">  
       <div class="pull-left">  
           <h2>Codeigniter 3 CRUD with DANDEV</h2>  
      </div>  
        <div class="pull-right">
            <a class="btn btn-success" href="<?php echo base_url('item/create') ?>"> Tạo mới Item</a>
        </div>
   </div>
</div>
<table class="table table-bordered">
 <thead>
      <tr>
          <th>Tiêu đề</th>
          <th>Mô tả</th>
          <th width="220px">Action</th>
      </tr>
 </thead>
  <tbody>
   <?php foreach ($data as $item) { ?>     
      <tr>
          <td><?php echo $item->title; ?></td>
         <td><?php echo $item->description; ?></td>          
     <td>
        <form method="DELETE" action="<?php echo base_url('item/delete/'.$item->id);?>">
         <a class="btn btn-info" href="<?php echo base_url('item/'.$item->id) ?>">Xem</a>
         <a class="btn btn-primary" href="<?php echo base_url('item/edit/'.$item->id) ?>">Sửa</a>
          <button type="submit" class="btn btn-danger">Xóa</button>
       </form>
      </td>    
      </tr>
      <?php } ?>
  </tbody>
</table>

application/views/items/create.php


<div class="row">
    <div class="col-lg-12 margin-tb">
       <div class="pull-left">
           <h2>Thêm mới Item</h2>
        </div>
        <div class="pull-right">
            <a class="btn btn-primary" href="<?php echo base_url('item');?>"> Quay lại</a>
        </div>
   </div>
</div>
<form method="post" action="<?php echo base_url('itemCreate');?>">
   <?php


    if ($this->session->flashdata('errors')){
        echo '<div class="alert alert-danger">';
        echo $this->session->flashdata('errors');
        echo "</div>";
    }


    ?>

   <div class="row">
       <div class="col-xs-12 col-sm-12 col-md-12">
          <div class="form-group">
              <strong>Tiêu đề:</strong>
                <input type="text" name="title" class="form-control">
            </div>
		</div>
        <div class="col-xs-12 col-sm-12 col-md-12">
          <div class="form-group">
                <strong>Mô tả:</strong>
               <textarea name="description" class="form-control"></textarea>
           </div>
       </div>
        <div class="col-xs-12 col-sm-12 col-md-12 text-center">
                <button type="submit" class="btn btn-primary">Lưu</button>
        </div>
    </div>
</form>

application/views/items/edit.php


<div class="row">
    <div class="col-lg-12 margin-tb">
       <div class="pull-left">
           <h2>Sửa Item</h2>
        </div>
        <div class="pull-right">
            <a class="btn btn-primary" href="<?php echo base_url('item');?>"> Quay lại</a>
        </div>
   </div>
</div>
<form method="post" action="<?php echo base_url('item/update/'.$item->id);?>">
   <?php


    if ($this->session->flashdata('errors')){
        echo '<div class="alert alert-danger">';
        echo $this->session->flashdata('errors');
        echo "</div>";
    }


    ?>

   <div class="row">
       <div class="col-xs-12 col-sm-12 col-md-12">
          <div class="form-group">
              <strong>Tiêu đề:</strong>
                <input type="text" name="title" class="form-control" value="<?php echo $item->title; ?>" >
            </div>
		</div>
        <div class="col-xs-12 col-sm-12 col-md-12">
          <div class="form-group">
                <strong>Mô tả:</strong>
               <textarea name="description" class="form-control"><?php echo $item->description; ?></textarea>
           </div>
       </div>
        <div class="col-xs-12 col-sm-12 col-md-12 text-center">
                <button type="submit" class="btn btn-primary">Lưu</button>
        </div>
    </div>
</form>

application/views/items/show.php


<div class="row">
    <div class="col-lg-12 margin-tb">
       <div class="pull-left">
           <h2>Chi tiết Item</h2>
        </div>
        <div class="pull-right">
            <a class="btn btn-primary" href="<?php echo base_url('item');?>"> Quay lại</a>
        </div>
   </div>
</div>
   <div class="row">
       <div class="col-xs-12 col-sm-12 col-md-12">
          <div class="form-group">
              <strong>Tiêu đề:</strong>
                <?php echo $item->title; ?>
            </div>
	</div>
        <div class="col-xs-12 col-sm-12 col-md-12">
          <div class="form-group">
                <strong>Mô tả:</strong>
               <?php echo $item->description; ?>
           </div>
       </div>        
    </div>


Vậy là chúng ta đã hoàn thành tạo ứng dụng CRUD đơn giản với Codeigniter 3 sử dụng bootstrap và mysql. Giờ chúng ta chạy ứng dụng thôi.
Lưu ý thêm cho các bạn là chúng ta cần vào file config.php để cấu hình lại phần base_url như sau:


$config['base_url'] = 'http://localhost/codeigniter3demo';

Trong quá trình thêm code theo như hướng dẫn ở trên blog các bạn sẽ gặp phải 1 vài vấn đề mà code không chạy mình có giải thích nguyên nhân và cách khắc phục trong video nhé