doujindou4356 2016-07-21 05:39
浏览 57
已采纳

如何使用php phalcon日历检索数据

I'm using phalcon-2.1.0. I just create a calendar for my blog archive. i want to retrieve data from database which is matched with the date. my code retrieve only one post and that is my last post in db. I'm not understanding how to query loop through to check the date related data. I need to retrieve that posts which is related with the date users clicked.

[controller]

public function indexAction()
{
    $bloger = Blogs::find();
    $postedDays = [];
    foreach ($bloger as $blog) {
       $date = date('Y-m-d', strtotime($blog->datetime));

       if (!in_array($date, $postedDays)) {
           $postedDays[] = $date;
       }
    }
    $this->view->setvar('dates', $postedDays);        
}

public function archivesAction($date)
{
    $archs = Blogs::find(["datetime LIKE :key:","bind"=>["key"=>'%'.$date.'%']]);        
    $this->view->setvar('dates', $archs);
    $this->view->pick('blog/archive');
}

[Calendar in blog page]

    <?php
date_default_timezone_set("Asia/Dhaka");
if(!isset($_REQUEST['month'])){$month = date("m");}else{$month = $_REQUEST['month'];}
if(!isset($_REQUEST['year'])){$year = date("Y");}else{$year = $_REQUEST['year'];}
if(!isset($_REQUEST['day'])){$day = date('d');}else{$day = $_REQUEST['day'];}   
$timestamp = mktime (0, 0, 0, $month, 1, $year);
$monthName = date("F", $timestamp);
$prev_year = $year;
$next_year = $year;
$prev_month = $month-1;
$next_month = $month+1;
if($prev_month == 0 ){$prev_month = 12;$prev_year = $year - 1;}
if($next_month == 13 ){$next_month = 1;$next_year = $year + 1;}
$prev_month = str_pad($prev_month, 2, '0', STR_PAD_LEFT);
$next_month = str_pad($next_month, 2, '0', STR_PAD_LEFT);
?>

<div class="table">
<div class="tr caption">
<div class="th L"><a href="blog?month=<?php echo($prev_month.'&amp;year='.$prev_year);?>">&lsaquo;</a></div>
<div class="th monyer"><?php echo($monthName.'-'.$year); ?></div>
<div class='th R'><a href="blog?month=<?php echo($next_month); ?>&amp;year=<?php echo($next_year);?>">&rsaquo;</a></div>
</div>

<div class='thead'>
<div class="td">S</div>
<div class="td">M</div>
<div class="td">T</div>
<div class="td">W</div>
<div class="td">T</div>
<div class="td">F</div>
<div class="td">S</div>
</div>

<?php
$monthstart = date("w", $timestamp);
$lastday = date("d", mktime (0, 0, 0, $month + 1, 0, $year));
$startdate = -$monthstart;
//Figure out how many rows we need.
$numrows = ceil (((date("t",mktime (0, 0, 0, $month + 1, 0, $year))
+ $monthstart) / 7));
//Let's make an appropriate number of rows...
for ($k = 1; $k <= $numrows; $k++){
?><div class="tr days"><?php
//Use 7 columns (for 7 days)...
for ($i = 0; $i < 7; $i++){
$startdate++;
$startdate = str_pad($startdate, 2, '0', STR_PAD_LEFT); //Make dates leading zero
if ($startdate <= 0){
//If we have a blank day in the calendar.
?>

<div class="td L"><?php echo("&nbsp;");?></div>
<?php }elseif($startdate > $lastday){echo('<div class="td R">&nbsp;</div>');}else {
if ($startdate == date("d") && $month == date("m") && $year == date("Y")){?>
<div class="td today"><?php echo($startdate); ?></div><?php
} else { ?>
<?php if(in_array($year.'-'.$month.'-'.$startdate, $dates)) { ?>
<div class="td days"><a href="blog/archives/<?php echo($year.'-'.$month.'-'.$startdate); ?>"><?php echo($startdate); ?></a></div>
<?php }else{ ?><div class="td days"><?php echo($startdate); ?></div><?php }?>   
<?php } } } ?></div><?php } ?>
</div>

[View - archive.volt]

{% for archs in dates %}
<a href="blog/showfull/<?php echo($archs->id); ?>">
    <dl class="archL">
        <dt><b>{{archs.btitle}}</b><br/><em>{{archs.datetime}}</em></dt>
        <dd>{{archs.bintro}}</dd>
    </dl>
</a>
{% endfor %}
  • 写回答

1条回答 默认 最新

  • dongshan2004 2016-07-21 09:02
    关注

    Your archivesAction takes a parameter $date which you aren't using.

    You are basically querying all your blogs and looping through them. While looping you assign values to $times, $dater and $timer. After your foreach ends, these values will have the date of your last blog record.

    $get = Blogs::find();
    
    foreach($get as $d)
    {
        $times = explode(' ', $d->datetime);
        $dater = $times[0];
        $timer = $times[1];
    }
    
    $archs = Blogs::find(["datetime LIKE :key:","bind"=>["key"=>'%'.$dater.'%']]);
    

    What you should do instead is take the date from the parameter of your action archivesAction($date). Replace the above lines of code with this single line instead:

    // changed variable $dater to $date
    $archs = Blogs::find(["datetime LIKE :key:", "bind" => ["key"=> '%' . $date . '%']]);
    

    UPDATE

    To have your months always output with 2 characters you can do this:

    <?php prev_month = str_pad($prev_month, 2, '0', STR_PAD_LEFT); ?>
    <a href="blog?month=<?php echo($prev_month.'&amp;year='.$prev_year);?>">&lsaquo;</a>
    

    Check str_pad documentation for more information on this.


    UPDATE 2

    To highlight only your calendar days which have posts in the database, you can do something like this:

    [controller]

    $blogs = Blogs::find();
    $uniqueDays = [];
    
    foreach ($blogs as $blog) {
       $date = date('Y-m-d', strtotime($blog->datetime));
    
       if (!in_array($date, $uniqueDays)) {
           $uniqueDays[] = $date;
       }
    }
    
    $this->view->setVar('databaseDates', $uniqueDays');
    

    [view]

    <?php
    if (in_array($year.'-'.$month.'-'.$startdate, $databaseDates) {
       // date is found in the database
       // highlight date
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ghpython这里总是报错而且rhino视图窗口内不显示怎么办
  • ¥15 布尔运算推导,请证明
  • ¥15 有没有人会打学生成绩管理系统呀
  • ¥15 在使用Fiddler和夜神模拟器抓包的时候一直出现443该怎么办啊QAQ搜了好几个笔记都没有解决
  • ¥15 3x7的二维数组A、B、C,A中的任意1个数组元素与B的任意1个数组元素、同时又与C的任意1个数组元素比较,把不同位置出现相同数的比较称为无意义,反之称为有意义,把有意义的比较打印输出。
  • ¥20 预测模型怎么处理原始数据(随机森林)
  • ¥20 请问discuz3.5如何实现插入ckplayer全能播放器功能呢?
  • ¥15 thingsboard代码编译出错误
  • ¥15 博途v18仿真报错怎么解决
  • ¥15 欧姆龙plc枕式包装机 ST编程