dongxiangchan0743 2013-07-25 07:32
浏览 27

从joomla下拉开始的几个月的数据

i have this code on joomla view php: (this code generate the months dropdown)

    <form action="<?php echo JRoute::_('index.php?option=com_mycom'); ?>" method="post" name="adminForm" id="adminForm" class="modelList">        
    <select name="filter.search" class="inputbox" onchange="this.form.submit()">
                        <?php
          for ($i = 0; $i <= 12; ++$i) {
            $time = strtotime(sprintf('-%d months', $i));
            $value = date('Y-m', $time);
            $label = date('F Y', $time);
            printf('<option value="%s">%s</option>', $value, $label);
          }
          ?>
          </select>
</form>

and i have a mysql table with row of "date" call "date" so what that i am looking for is - when i am select the month all the data from this month will load on this page

so how can i get it from the sql query? i try this code on model but that give me an error:

    $search = $this->getState('filter.search');

    if(!empty($search))
    {
        if(stripos($search, 'id:') === 0)
        {
            $query->where('id = '.(int) substr($search, 3));
        }
        else
        {
            $search = $db->Quote('%'.$db->escape($search, true).'%');
            $query->where('(date LIKE '.$search.')');
        }
    }

i try this code:

if(!empty($search))
    {
        if(stripos($search, 'id:') === 0)
        {
            $query->where('a.id = '.(int) substr($search, 3));
        }
        else
        {

            $searchParts = explode('-', $search);
            // day 1 of selected month
            $searchStartDate = date('Y-m-d', mktime(0, 0, 0, $searchParts[1], 1, $searchParts[0]));
            // day 0 of next month is last day of selected month
            $searchEndDate = date('Y-m-d', mktime(0, 0, 0, $searchParts[1] + 1, 0, $searchParts[0]));

            $query->where("(a.date BETWEEN '$searchStartDate' AND '$searchEndDate' LIKE '.$search.')");
        }
    }

but it return an error:

Fatal error: Call to a member function where() on a non-object in 

on this line - $query->where("(a.date BETWEEN '$searchStartDate' AND '$searchEndDate' LIKE '.$search.')");

  • 写回答

1条回答 默认 最新

  • duanjia7912 2013-07-25 09:02
    关注

    When working with DATE fields you should do just that. You are trying to work with it as a string, which is possible but very inefficient.

    Instead of asking the database to return all records WHERE date LIKE '%2013-07%' you should be asking for all records between the beginning and end of the selected month.

    SELECT *
    FROM tbl
    WHERE `date` BETWEEN '2013-07-01' AND '2013-07-31'
    

    You can change your PHP code to something like this -

    else
    {
        $searchParts = explode('-', $search);
        // day 1 of selected month
        $searchStartDate = date('Y-m-d', mktime(0, 0, 0, $searchParts[1], 1, $searchParts[0]));
        // day 0 of next month is last day of selected month
        $searchEndDate = date('Y-m-d', mktime(0, 0, 0, $searchParts[1] + 1, 0, $searchParts[0]));
    
        $query->where("(date BETWEEN '$searchStartDate' AND '$searchEndDate')");
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 被蓝屏搞吐了,有偿求帮解答,Ai回复直接拉黑
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并