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 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭