如何使用 php phalcon 日历检索数据
How to retrieve data with php phalcon calendar
我正在使用 phalcon-2.1.0。我只是为我的博客存档创建一个日历。我想从数据库中检索与日期匹配的数据。我的代码只检索一个 post,那是我在数据库中的最后一个 post。我不明白如何通过查询循环来检查与日期相关的数据。我需要检索与用户点击日期相关的 posts。
[控制器]
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');
}
[博客页面中的日历]
<?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.'&year='.$prev_year);?>">‹</a></div>
<div class="th monyer"><?php echo($monthName.'-'.$year); ?></div>
<div class='th R'><a href="blog?month=<?php echo($next_month); ?>&year=<?php echo($next_year);?>">›</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(" ");?></div>
<?php }elseif($startdate > $lastday){echo('<div class="td R"> </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>
[查看 - 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 %}
您的 archivesAction
接受了一个您没有使用的参数 $date
。
您基本上是在查询所有博客并循环浏览它们。在循环时,您将值分配给 $times
、$dater
和 $timer
。在您的 foreach
结束后,这些值将包含您上次博客记录的日期。
$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.'%']]);
您应该做的是从您的操作参数中获取日期 archivesAction($date)
。用这一行替换上面的代码行:
// changed variable $dater to $date
$archs = Blogs::find(["datetime LIKE :key:", "bind" => ["key"=> '%' . $date . '%']]);
更新
要让您的月份始终以 2 个字符输出,您可以这样做:
<?php prev_month = str_pad($prev_month, 2, '0', STR_PAD_LEFT); ?>
<a href="blog?month=<?php echo($prev_month.'&year='.$prev_year);?>">‹</a>
查看 str_pad documentation 了解更多信息。
更新 2
要仅突出显示数据库中有帖子的日历日,您可以这样做:
[控制器]
$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');
[查看]
<?php
if (in_array($year.'-'.$month.'-'.$startdate, $databaseDates) {
// date is found in the database
// highlight date
}
我正在使用 phalcon-2.1.0。我只是为我的博客存档创建一个日历。我想从数据库中检索与日期匹配的数据。我的代码只检索一个 post,那是我在数据库中的最后一个 post。我不明白如何通过查询循环来检查与日期相关的数据。我需要检索与用户点击日期相关的 posts。
[控制器]
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');
}
[博客页面中的日历]
<?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.'&year='.$prev_year);?>">‹</a></div>
<div class="th monyer"><?php echo($monthName.'-'.$year); ?></div>
<div class='th R'><a href="blog?month=<?php echo($next_month); ?>&year=<?php echo($next_year);?>">›</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(" ");?></div>
<?php }elseif($startdate > $lastday){echo('<div class="td R"> </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>
[查看 - 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 %}
您的 archivesAction
接受了一个您没有使用的参数 $date
。
您基本上是在查询所有博客并循环浏览它们。在循环时,您将值分配给 $times
、$dater
和 $timer
。在您的 foreach
结束后,这些值将包含您上次博客记录的日期。
$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.'%']]);
您应该做的是从您的操作参数中获取日期 archivesAction($date)
。用这一行替换上面的代码行:
// changed variable $dater to $date
$archs = Blogs::find(["datetime LIKE :key:", "bind" => ["key"=> '%' . $date . '%']]);
更新
要让您的月份始终以 2 个字符输出,您可以这样做:
<?php prev_month = str_pad($prev_month, 2, '0', STR_PAD_LEFT); ?>
<a href="blog?month=<?php echo($prev_month.'&year='.$prev_year);?>">‹</a>
查看 str_pad documentation 了解更多信息。
更新 2
要仅突出显示数据库中有帖子的日历日,您可以这样做:
[控制器]
$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');
[查看]
<?php
if (in_array($year.'-'.$month.'-'.$startdate, $databaseDates) {
// date is found in the database
// highlight date
}