我是CakePHP的新手。
现在我有一个自学任务,我想在这两个日期之间显示所有来自PhpMyAdmin的数据,当用户单击“显示”并在同一页面上显示“ index.ctp”时。

但是,我陷入了困境,现在我知道应该将可以显示所有信息的代码放在哪里。
以下是我到目前为止所做的代码:



模型(room.php):

<?php
class Room extends AppModel{
    public  $validate = array(
        'sdate' => array(
            'date' => array(
                //Add 'ymd' to the rule.
                'rule' => array('date', 'ymd'),
                'message' => 'Please select a valid start date.',
            ),
        ),
    );
        'edate' => array(
            'date' => array(
                //Add 'ymd' to the rule.
                'rule' => array('date', 'ymd'),
                'message' => 'Please select a valid end date.',
            ),
        ),
    );
}




控制器(RoomController.php):

    <?php
    class RoomsController extends AppController{
        public $helpers = array('Html', 'Form');
        public $components = array('Session');

        public function index() {

        }

    }
?>




index.ctp

    <h1>Room Reservation<h1>

<table>
    <?php
        echo $this->Form->create('rooms',
            array(
                'type' => 'get'
            )
        );
        echo $this->Form->input('Start Date:',
            array(
                'label' => 'Start Date',
                'id' => 'sdate'
            )
        );
        echo $this->Form->input('End Date:',
            array(
                'label' => 'End Date',
                'id' => 'edate'
            )
        );
        echo $this->Form->end('Show');
    ?>
</table>

<script type="text/javascript">
    $(document).ready(function() {
        $('#sdate').Zebra_DatePicker();
        $('#edate').Zebra_DatePicker();
    });
</script>




room_type(数据库):

CREATE TABLE `room_type` (
`room_id` int(11) NOT NULL AUTO_INCREMENT,
`room_type` varchar(10) NOT NULL,
`no_of_room` int(10) NOT NULL,
PRIMARY KEY (`room_id`)
);

room_id | room_type | no_of_room
    1   |     A     |    10
    2   |     B     |    10
    3   |     C     |    10
    4   |     D     |    10




room_type_availability(数据库):

CREATE TABLE `room_type_availability` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`room_id` int(11) NOT NULL,
`trx_date` date NOT NULL,
`no_of_room` int(2) NOT NULL,
PRIMARY KEY (`id`), ADD KEY `room_id` (`room_id`),
CONSTRAINT `room_type_availability_ibfk_1` FOREIGN KEY (`room_id`) REFERENCES `room_type` (`room_id`) ON DELETE CASCADE ON UPDATE CASCADE
);

    id | room_id | trx_date  | no_of_room
    1  |     1   |2015-05-05 |    10
    2  |     1   |2015-05-06 |    10
    3  |     1   |2015-05-07 |    9
    4  |     1   |2015-05-08 |    7
    5  |     1   |2015-05-09 |    6
    6  |     2   |2015-05-05 |    8
    7  |     2   |2015-05-06 |    3
    8  |     2   |2015-05-07 |    6
    9  |     2   |2015-05-08 |    4
   10  |     2   |2015-05-09 |    5




如果选择的日期在数据库中,它将显示room_type_availability数据库。

否则,如果所选日期不在数据库中,它将显示room_type数据库。

希望你们能对此提供一些建议。

感谢您的帮助和赞赏。

:)

最佳答案

//in controller

public function index() {

    if($this->request->isPost()) {
        $obj = $this->loadModel('RoomTypeAvailability');
        $start_date = $this->request->data['sdate'];
        $end_date  = $this->request->data['edate'];
        // use this "between" range
        $conditions = array('RoomTypeAvailability.trx_date BETWEEN ? and ?' => array($start_date, $end_date));
        $data = $this->RoomTypeAvailability->find('all',array('conditions'=>$conditions));
        $this->set('data', $data);
    }
}


室内模型

class Room extends AppModel{

public $useTable = false;

public  $validate = array(
    'sdate' => array(
        'date' => array(
            //Add 'ymd' to the rule.
            'rule' => array('date', 'ymd'),
            'message' => 'Please select a valid start date.',
        ),
    ),
);
    'edate' => array(
        'date' => array(
            //Add 'ymd' to the rule.
            'rule' => array('date', 'ymd'),
            'message' => 'Please select a valid end date.',
        ),
    ),
);
}


在RoomType模型中

class RoomTypeAvailability extends AppModel {

    public $useTable = 'room_type_availability';

    public $validate = array( );
}


// index.ctp

<h1>Room Reservation<h1>

<table>
    <?php
        echo $this->Form->create('rooms',
            array(
                'type' => 'post'
            )
        );
        echo $this->Form->input('Start Date:',
            array(
                'label' => 'Start Date',
                'id' => 'sdate',
                'name' => 'sdate'
            )
        );
        echo $this->Form->input('End Date:',
            array(
                'label' => 'End Date',
                'id' => 'edate',
                'name' => 'edate'
            )
        );
        echo $this->Form->end('Show');
    ?>
</table>
<?php if(isset($data) && count($data)>0) { ?>
<table>
    <tr>
        <th>id</th>
        <th>room_type</th>
        <th>trx_date</th>
        <th>no_of_date</th>
    </tr>

    <?php foreach($data as $row) { ?>
        <tr>
            <td><?php echo $row['RoomTypeAvailability']['id']?></td>
            <td><?php echo $row['RoomTypeAvailability']['room_type']?></td>
            <td><?php echo $row['RoomTypeAvailability']['trx_date']?></td>
            <td><?php echo $row['RoomTypeAvailability']['no_of_date']?></td>
        </tr>
    <?php } ?>

</table>
<?php } ?>
<script type="text/javascript">
    $(document).ready(function() {
        $('#sdate').Zebra_DatePicker();
        $('#edate').Zebra_DatePicker();
    });
</script>

08-18 03:53
查看更多