2014-05-01 68 views
0

我使用Codeigniter開發了自己的基於Web的圖書館系統。我對活躍的記錄感到困惑。爲了明確的目的,請參閱下面我的代碼:我的數據庫列互相交換

//Add Publisher Form 
<form action="<?php echo base_url(); ?>admin/addPublisher" method="post" class="form-horizontal" role="form"> 
    <div class="col-lg-12"> 
     <div class="row"> 
      <div class="form-group"> 
       <label for="penerbit" class="col-lg-2 control-label">Publisher</label> 
       <div class="col-lg-9"> 
        <input type="text" name="publisher" class="form-control"> 
       </div> 
      </div> 
     </div> 
     <div class="row"> 
      <div class="form-group"> 
       <label for="website" class="col-lg-2 control-label">Website</label> 
       <div class="col-lg-9"> 
        <input type="text" name="website" class="form-control"> 
       </div> 
      </div> 
     </div> 
     <div class="row"> 
      <div class="form-group"> 
       <label for="email" class="col-lg-2 control-label">Contact</label> 
       <div class="col-lg-9"> 
        <input type="text" name="phone" class="form-control"> 
       </div> 
      </div> 
     </div> 
     <div class="row"> 
      <div class="form-group"> 
       <label for="telepon" class="col-lg-2 control-label">E-mail</label> 
       <div class="col-lg-9"> 
        <input type="text" name="email" class="form-control"> 
       </div> 
      </div> 
     </div> 
     <div class="row"> 
      <div class="form-group"> 
       <label for="alamat" class="col-lg-2 control-label">Address</label> 
       <div class="col-lg-9"> 
        <textarea name="address" class="form-control" rows="5"></textarea> 
       </div> 
      </div> 
     </div> 
     <div class="row"> 
      <div class="form-group"> 
       <label for="" class="col-lg-2"></label> 
       <div class="col-lg-9"> 
        <button type="submit" class="btn btn-success">Add Publisher</button> 
       </div> 
      </div> 
     </div> 
    </div> 
</form> 


// Add Publisher @ Admin Controller 
public function addPublisher() 
    { 
     if($this->session->userdata('logged_in')) 
     {   
      $publisher = $this->input->post('publisher'); 
      $web = $this->input->post('website'); 
      $email = $this->input->post('email'); 
      $phone = $this->input->post('phone'); 
      $addr = $this->input->post('address'); 


      if(!empty($publisher) && !empty($web) && !empty($email) && !empty($phone) && !empty($addr)){ 
       $this->adminModel->insertPublisher($publisher, $web, $email, $phone, $addr); 
       redirect ('admin/publisher'); 
      } else { 
       redirect ('admin/publisher'); 
      } 


      $session_data = $this->session->userdata('logged_in'); 
      $data['user'] = $session_data['user']; 
      $data['pubs'] = $this->adminModel->getAllPublisher(); 
      $data['tittle'] = "Add Publisher Page"; 

      $this->load->view('admin/static/header', $data); 
      $this->load->view('admin/static/header-nav'); 
      $this->load->view('admin/static/left-nav'); 
      $this->load->view('admin/publisher/publisher', $data); 
     } else { 
      $this->index(); 
     } 
    } 


// Here my adminModel for inserting records 
public function insertPublisher($publisher, $web, $phone, $email, $address) 
    { 
     $query = $this->db->query("INSERT INTO publisher (`id_publisher`, `publiser`, `website`, `phone`, `email`, `address`, `created_at`) VALUES ('', '$publisher', '$web', '$phone', '$email', '$address', NOW())"); 
     return $query; 
    } 


// The output in print_r() 
stdClass Object 
(
    [id_publisher] => 1 
    [publisher] => Example Publishing 
    [website] => example.com 
    [phone] => [email protected] <--- this is my point 
    [email] => 1234567    <--- and this is 
    [address] => asdfgj 
    [create_at] => 2014-04-30 10:48:08 
) 

我的問題是:

  1. 是什麼原因導致「手機」和「電子郵件」列之間的交流?
  2. 如何按字母順序排序發佈者的視圖?

非常感謝您的建議。

回答

1

檢查你的代碼 改變這種

$this->adminModel->insertPublisher($publisher, $web, $email, $phone, $addr); 
                 ^ ^
       redirect ('admin/publisher'); 

這個

$this->adminModel->insertPublisher($publisher, $web, $phone,$email, $addr); 
        redirect ('admin/publisher'); 

,並檢查你的模型

public function insertPublisher($publisher, $web, $phone, $email, $address) 
    { 
     $query = $this->db->query("INSERT INTO publisher (`id_publisher`, `publiser`, `website`, `phone`, `email`, `address`, `created_at`) VALUES ('', '$publisher', '$web', '$phone', '$email', '$address', NOW())"); 
     return $query; 
    } 

你的第二個問題,當你檢索從數據你的數據使用此查詢

"select * from publisher ORDER BY publiser ASC" 

你可以按字母順序發行商名稱升序

+0

我的天啊。感謝您的建議@Mohit Jain。你真棒 – life4learn

0

你需要更具體的瞭解,你在「的print_r()」打印變量,你插入到數據庫後打印它?

正如我理解你的問題,原因是數據庫中的字段的順序。

+0

Mohit Jain的建議確實解決了我的問題。我檢查了兩天,問題出在我的控制器中。如果允許,還有一個問題,如何按字母順序排列所有發佈者的視圖?我在迭代中使用「排序」功能,並得到錯誤 – life4learn

+0

這個答案應該在評論中 –