2013-05-21 143 views
0

我是cakephp的新手。 如何在cakephp中按日期搜索,無範圍。 使用日期選擇器和搜索選擇一個日期。 在我的索引部分創建的記錄在那裏。我有一個日期選擇器和搜索按鈕是在index.ctp的頂部。 在搜索我應該寫什麼Cakephp:按日期搜索,無範圍

創建部分完成。在那個創建中,如果我選擇日期和視圖意味着特定日期的細節應該顯示。

這是我使用這樣的表單助手井將創建一個使用POST方法的表單的編碼

function index() 
{ 

$user = $this->Create->find('all'); 
$this->set('create', $user); 

    } 
     function Search() 

     { 

     ??? 


    $user = $this->Create->find('all',array('conditions'=>array('journeydate'=>'journeydate'))); 
    $this->set('create', $user); 

}

index.ctp 


    <!-- link to add new users page --> 

    <script> 
     $(function() { 
    $(".datepicker").datepicker({ dateFormat: "yy-mm-dd" }); 
    }); 
</script> 
    <?php 
    foreach($create as $user){} 
    ?> 
    <?php echo $this->form->create("Create",array('action' => 'search',',$user['Create']['id'])); 
    echo $this->form->input('date', 
    array(
     'class'=>'datepicker', 
     'type'=>'text' 
    )); 
    echo $this->form->end("Search"); 
?> 

<table style='padding:5px;'> 
<!-- table heading --> 
<tr style='background-color:#fff;'> 
    <th>Id</th> 
    <th>Name</th> 
    <th>Age</th> 
    <th>Gender</th> 
    <th>Phone no</th> 
    <th>Bus Name</th> 
    <th>Ticket no</th> 
    <th>Seat No</th> 
    <th>From</th> 
    <th>To</th> 
    <th>Journey Date</th> 
    <th>Booking Date</th> 
    <th>Amount</th> 

    </tr> 

<?php 
    foreach($create as $user){ 

    echo "<tr>"; 
     echo "<td>{$user['Create']['id']}</td>"; 
     echo "<td>{$user['Create']['name']}</td>"; 
     echo "<td>{$user['Create']['age']}</td>"; 
     echo "<td>{$user['Create']['gender']}</td>"; 
     echo "<td>{$user['Create']['phoneno']}</td>"; 
     echo "<td>{$user['Create']['busname']}</td>"; 
     echo "<td>{$user['Create']['ticketno']}</td>"; 
     echo "<td>{$user['Create']['seatno']}</td>"; 
     echo "<td>{$user['Create']['from']}</td>"; 
     echo "<td>{$user['Create']['to']}</td>"; 
     echo "<td>{$user['Create']['journeydate']}</td>"; 
     echo "<td>{$user['Create']['bookingdate']}</td>"; 
     echo "<td>{$user['Create']['amount']}</td>"; 

    echo "</tr>"; 
    } 
    ?> 

    <tr><td> 
    <INPUT TYPE="BUTTON" VALUE="Back" ONCLICK="window.location.href='/newcake/creates/create'"></td></tr> 
    </table> 
+0

請發佈您試過的代碼,我們可以幫助您改進或修復它。即使你編寫僞代碼是一個開始,但目前,你的問題基本上聽起來像「請給我寫我的控制器的動作」 – Dave

回答

1

。 我不知道你爲什麼想要用id來支持它。此外,它似乎像你期望更多的一個用戶返回,應該開始他們的旅程在選定的日期...在這種情況下$ user ['Create'] ['id']沒有任何意義。

它改成這樣:

<?php echo $this->form->create("Create",array('action' => 'search')); ?> 

在你的控制器,你需要找到所有用戶開始他們在選定日期的旅程: 由於默認情況下,表單助手使用POST作爲提交方法,你可以找到表單數據: $ this-> request-> data ['MyModel'] ['title'];

如果你不知道,你可以隨時輸出,

pr($this->request->data); 

你的搜索功能提交的數據應該是這樣的:

public function Search(){ 

    $date = $this->request->data['Create']['date']; 
    $user = $this->Create->find('all', array(
     'conditions' => array(
      'journeydate'=> $date, 
     ), 
    )); 
    $this->set('create', $user); 
} 

你也應該與命名您的模型更加小心,因爲在這種情況下,「創建」似乎不是很適合。 爲了充分利用CakePHP,您應該遵循約定。 http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html

+0

Thankz Jeroen爲你的幫助:)它工作很好.. – william

+0

@Jereon:它是工作正常,顯示日期後,在同一頁面我重新刷新顯示錯誤,未定義索引:創建。 – william

+0

沒有代碼就說有什麼問題有點難。請參閱http://book.cakephp.org/2.0/en/development/configuration.html#core-configuration,瞭解如何獲取更多調試信息(堆棧跟蹤,行號等)。請注意,您將始終使用.get php通知警告來嘗試訪問數組中的未設置元素等等。更新您的原始問題或使用額外信息打開一個新問題 – Jeroen