baby machinery baby machinery - 1 year ago 128
PHP Question

How to retrieve data with php phalcon calendar

I'm using phalcon-2.0.1. 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.


public function archivesAction($date)
$description = "blog-Archive-List";
$this->view->setVar("dynadesc", $description);
$keywords = "blog-key";
$this->view->setVar("dynakey", $keywords);
$uid = $this->session->get('id');
$name = $this->session->get('uname');
$active = $this->session->get('active');
$level = $this->session->get('level');
$this->view->setVar('uid', $uid);
$this->view->setVar("uname", $name);
$this->view->setVar("active", $active);
$this->view->setVar("level", $level);
$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.'%']]);
$this->view->setvar('dates', $archs);

[Calendar in blog page]

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

$monthstart = date("w", $timestamp);
$lastday = date("d", mktime (0, 0, 0, $month + 1, 0, $year));
$startdate = -$monthstart;
$numrows = ceil (((date("t",mktime (0, 0, 0, $month + 1, 0, $year))
+ $monthstart) / 7));
for ($k = 1; $k <= $numrows; $k++){
?><tr class="days"><?php
for ($i = 0; $i < 7; $i++){
if (($startdate <= 0) || ($startdate > $lastday)){
//If we have a blank day in the calendar.
?><td class="L"><?php echo('&nbsp;');?></td><?php
} else {
if ($startdate == date("j") && $month == date("n") && $year == date("Y")){?>
<td class="today"><a href="blog/archives?date=<?php echo($year.'-'.$month.'-'.$startdate); ?>"><?php echo($startdate); ?></a></td><?php
} else {
?><td><a href="blog/archives/<?php echo($year.'-'.$month.'-'.$startdate); ?>"><?php echo($startdate); ?></a></td><?php } } } ?>
</tr><?php } ?>

[View - archive.volt]

{% for archs in dates %}
<a href="blog/showfull/<?php echo($archs->id); ?>">
<dl class="archL">
{% endfor %}

Answer Source

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 . '%']]);


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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download