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

标签 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() {

        }

    }
?>
<小时/>

索引.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>
<小时/>

房间类型(数据库):

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数据库 p>

希望大家给点意见。

感谢您的帮助并表示感谢。

:)

最佳答案

//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>

关于php - CakePHP显示2个日期之间的所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30635089/

相关文章:

php - 如果选中复选框则显示 div 否则显示不同的 div

php - javascript 向 php 发送单向消息

php - 无法安装 PHPExcel

php - 从另一个查询的结果中减去 1 个查询的结果

javascript - 选择经度和纬度最接近的记录

java - 我可以在 JDBC 连接器中使用数据库 IP 地址而不是主机名吗?

php - 我如何自动更新连接表并从连接表访问数据?

cakephp - 选择cakephp 3查询中除一个字段外的所有字段

php - CakePHP 无法连接到 Postgres。但是 Pgadmin4 可以

php - CakePHP 3.0 中的分页