dongliang1654 2015-06-04 04:43
浏览 17

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

I am new for CakePHP. Now I'm having a task which is self-learning where I want to display all the data from PhpMyAdmin between 2 selected dates when user click 'Show' and display at the same page which is "index.ctp".

However, I'm stuck where I do now know where should I put the codes that can display all the information. Below are the codes I had done till now:


Model (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(Database):

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(Database):

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

If the date selected are in the database, it will display the room_type_availability database.

Else if the date selected are not in database, it will display the room_type database.

Hope you guys can give some advice on it.

Thanks for helping and appreciate it.

:)

  • 写回答

1条回答 默认 最新

  • dtdr84101 2015-06-04 04:54
    关注
    //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);
        }  
    }
    

    in Room Model

    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.',
            ),
        ),
    );
    }
    

    in RoomType Model

    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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答