2013-08-06 148 views
0

我有這個代碼塊在html中輸入搜索。

<?php echo form_open('bookstore/_searchbook'); ?> 
<table> 
    <tr> 
     <td> 
    <label for="searchid">Search:</label> 
     </td> 
     <td> 
    <input type="text" size="15" name="searchid" /> 
     </td> 
    </tr> 
    <tr> 
     <td> 
    <label for="searchtype">Type:</label> 
     </td> 
     <td> 
    <select> 
     <option value="book_id">Id</option> 
     <option value="book_author">Author</option> 
     <option value="book_name">Title</option> 
    </select> 
     </td> 
    </tr> 
    <tr> 
    <input type="submit" value="Search" /> 
    </tr> 
</table> 
<?php echo form_close(); ?> 

,我有這個我控制器上,

public function booksearch() 
{ 
    if($_POST['submit']) 
    { 
    $col= $this->input->post('searchtype', TRUE); 
    $val= $this->input->post('searchval', TRUE); 

    $data['book'] = $this->books_model->showbook($col,$val); 
    $this->load->view('showbooks',$data); 
    } 
} 

,這將是我的模型

public function showbook($col, $searchid) 
{ 
     $this->db->select()->from('books')->where(array($col=>$searchid)); 
     $query=$this->db->get(); 
     return $query->first_row('array'); 
} 

對我的看法進一步信息,

我有這樣的打印我的搜索結果。

<table cellpadding='5'> 
<th>Book ID</th> 
<th>Title</th> 
<th>Author</th> 
<th>Released Year</th> 
<th>ISBN</th> 

<?php 
if(isset($books)) : foreach($books as $book) : 
?>  
     <tr> 
     <td><?php echo $book['book_id'] ?></td> 
     <td><?php echo $book['book_name'] ?></td> 
     <td><?php echo $book['book_author'] ?></td> 
     <td><?php echo $book['book_year'] ?></td> 
     <td><?php echo $book['book_isbn'] ?></td> 
     </tr> 
<?php 
    endforeach; 
?> 

<?php 
else : 
?> 
<h5>No records</h5> 
<?php 
endif; 
?> 
</table> 

它沒有返回任何東西,所以我看到的是沒有記錄。有人指示我做錯了事。

+0

您打開'書店/ _searchbook'形式和你的函數名'booksearch' ...是故意的嗎? –

回答

0

因爲您的提交按鈕沒有任何name屬性。而不是if($_POST['submit'])$this->input->post(null)

+0

傻了,忘了名字屬性.. 小東西你不要指望問題:( –

+0

Happie幫助!接受答案,如果它幫助你。:D –

+0

我也忘了添加,$ this-> load - >查看('showbooks',$ data);在我的控制器上,所以在處理之後,它將我鏈接回我的視圖並打印出來.. hmm –

-1

嘗試添加全局$ _POST;到你的函數的頂部。在php中,函數中使用(聲明)的所有變量都是私有的,除非通過將其添加到函數中的全局語句中來進行說明。

public function booksearch() 
{ 
     global $_POST; 

if($_POST['submit']) 
    { 
    $col= $this->input->post('searchtype', TRUE); 
    $val= $this->input->post('searchval', TRUE); 

    return $data['book'] = $this->books_model->showbook($col,$val); 
    } 
} 
+0

我剛剛更新了上面的帖子,這個問題已經得到了解決,我只是缺乏名稱屬性,儘管這可能是另一種方式。 反正如果你不介意再看一遍。 –

1
  • 表單的動作(視圖)爲_searchbook,而你的控制器功能是booksearch
  • 你提交輸入需要name屬性
  • 控制器功能,您有$data['book'],而在你的示意圖(在foreach循環)你引用那個變量爲$books
  • 模型:你至少需要選擇一些東西;例如$this->db->select('*')->from('books')->where(array($col=>$searchid));代替$this->db->select()
  • 型號:我認爲你需要的return $query->result_array();代替$query->first_row('array')