doujindou4356 2016-07-20 21: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 01: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 雄安新区高光谱数据集的下载网址打不开
  • ¥66 android运行时native和graphics内存详细信息获取
  • ¥100 求一个c#通过CH341读取数据的Demo,能够读取指定地址值的功能
  • ¥15 rk3566 Android11 USB摄像头 微信
  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
  • ¥20 在搭建fabric网络过程中遇到“无法使用新的生命周期”的报错
  • ¥15 Python中关于代码运行报错的问题
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部