CakePHP显示2个日期之间的所有数据

时间:2015-06-04 04:43:04

标签: php mysql cakephp

我是CakePHP的新手。 现在我正在执行自学的任务,当用户点击“显示”时,我想在 2个选定日期之间显示来自PhpMyAdmin的所有数据/ strong>并显示在同一页,即“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.',
            ),
        ),
    );
}

Controller(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数据库

希望你们能就此提出一些建议。

感谢您的帮助和欣赏。

:)

1 个答案:

答案 0 :(得分:0)

//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.',
        ),
    ),
);
}
模型中的

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>