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 MDK–ARM里一直找不到调试器
  • ¥15 oracle中sql查询问题
  • ¥15 vue使用gojs3.0版本,在nodeDataArray中的iconSrc使用gif本地路径,展示出来后动画是静态的,不是动态的
  • ¥100 代写个MATLAB代码,有偿
  • ¥15 ansys electronics 2021 R1安装报错,错误代码2,如图
  • ¥15 Dev-c++打字不出现中文,但出现日文
  • ¥30 搭建面包板由NE555N和SN74LS90N组成的计时电路时出了问题
  • ¥15 无源定位系统的时差估计误差标准差
  • ¥15 请问这个代码哪里有问题啊
  • ¥20 python--version在命令端输入结果Python is not defined怎么办?还有pip不是exe格式是不是没安装成功?