我想要點評(Kohana 3.2)

[英]I want count comment (Kohana 3.2)


I'm development mini blog by use Kohana 3.2 and Bootstrap 2.3.0, This it table of database structure.

我是使用Kohana 3.2和Bootstrap 2.3.0開發迷你博客,這是數據庫結構表。

CREATE TABLE IF NOT EXISTS `entries` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `entry_title` varchar(255) NOT NULL,
  `entry_content` text NOT NULL,
  `entry_created_date` datetime NOT NULL,
  `entry_edited_date` datetime NOT NULL,
  `entry_created_ip` varchar(40) NOT NULL,
  `entry_edited_ip` varchar(40) NOT NULL,
  `entry_published` tinyint(1) NOT NULL,
  `entry_published_comment` tinyint(1) NOT NULL,
  `entry_hits` int(11) NOT NULL,
  `catentry_id` tinyint(2) NOT NULL,
  `user_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `comments` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `comment_message` text NOT NULL,
  `comment_date` datetime NOT NULL,
  `comment_ip` varchar(40) NOT NULL,
  `entry_id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8; 

This my project location. C:\wamp\www\iblog

這是我的項目位置。 C:\ WAMP \ WWW \ iblog

Create Model Entry. C:\wamp\www\iblog\application\classes\model\entry.php

創建模型條目。 C:\ WAMP \ WWW \ iblog \程序\類\型號\ entry.php

<?php

defined('SYSPATH') or die('No direct script access.');

class Model_Entry extends ORM {
    protected $_belongs_to = array(
        'catentry' => array(),
        'user' => array()
    );

    protected $_has_many = array(
        'comments' => array()
    );
}

Create Model Comment. C:\wamp\www\iblog\application\classes\model\comment.php

創建模型評論。 C:\ WAMP \ WWW \ iblog \程序\類\型號\ comment.php

<?php

defined('SYSPATH') or die('No direct script access.');

class Model_Comment extends ORM {
    protected $_belongs_to = array(
        'entry' => array(),
        'user' => array()
    );
} 

Create function entry in Controller manage. C:\wamp\www\iblog\application\classes\controller\manage.php

在Controller manage中創建功能條目。 C:\瓦帕\ WWW \ iblog \應用\類\控制器\ manage.php

function action_entry() {
        if (Session::instance()->get('user') == null && Session::instance()->get('user')->usertype_id != 1) {
            $this->template->content = View::factory('user/login');
        } else {

            // Get the total count of records in the enry table
            $count = ORM::factory('entry')->find_all()->count();

            // Create an instance of Pagination class and set values
            $pagination = Pagination::factory(array(
                        'total_items' => $count,
                        'items_per_page' => 10,
                    ));
            $pagination->route_params(array('controller' => $this->request->controller(), 'action' => $this->request->action()));

            // Load specific results for current page
            $entries = ORM::factory('entry')
                    ->order_by('id')
                    ->limit($pagination->items_per_page)
                    ->offset($pagination->offset)
                    ->find_all();

            // Render the pagination links
            $page_links = $pagination->render();

            // Define our template view and bind to variables
            $this->template->content = View::factory('manage/entry')
                    ->bind('count', $count)
                    ->bind('entries', $entries)
                    ->bind('page_links', $page_links);
        }
    }

Create View Entry. C:\wamp\www\iblog\application\views\manage\entry.php

創建視圖條目。 C:\ WAMP \ WWW \ iblog \程序\意見\管理\ entry.php

<ul class="breadcrumb">
    <li>
        <?php echo HTML::anchor('', '<i class="icon-home icon-white"></i>&nbspHome'); ?>
        <span class="divider">/</span>
    </li>
    <li>
        <?php echo HTML::anchor('manage/index', 'manage'); ?>
        <span class="divider">/</span>
    </li>
    <li class="active">entry</li>
</ul>

<div class="page-header">
    <span class="pull-right">
        <?php echo HTML::anchor('manage/index', '<strong>กลับไปหน้าตั้งค่า</strong> <i class="icon-signout"></i>', array('class' => 'btn btn-danger')); ?>
    </span>
    <h2><i class="icon-tasks"></i> จัดการบล็อก</h2>
</div>

<table class="table table-striped table-condensed table-hover">
    <thead>
        <tr>
            <th>#</th>
            <th>Name</th>
            <th>Category</th>
            <th>Comment Count</th>
            <th>publish?</th>
            <th>comment?</th>
            <th>Hits</th>
            <th>Owner</th>
            <th>Created</th>
            <th>Modified</th>
            <th style="width: 36px;"></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($entries as $entry): ?>
            <tr class="tooltip-demo">
                <td><?php echo $entry->id; ?></td>
                <td>
                    <?php echo HTML::anchor('', '<i class="icon-eye-open"></i>', array('rel' => 'tooltip', 'title' => $entry->entry_title)); ?>
                </td>
                <td><?php echo $entry->catentry->cat_entry_name; ?></td>
                <td><?php echo 'no'; ?></td>
                <td>
                    <?php 
                        switch($entry->entry_published) {
                            case 0: echo '<span class="label label-warning">publish</span>';break; 
                            case 1: echo '<span class="label label-success">waited</span>';break;
                        }
                    ?>
                </td>
                <td>
                    <?php 
                        switch($entry->entry_published_comment) {
                            case 0: echo '<span class="label label-important">On</span>';break; 
                            case 1: echo '<span class="label label-success">Off</span>';break;
                        }
                    ?>
                </td>
                <td><?php echo HTML::anchor('entry/viewEntry/'. $entry->id, @$entry->entry_hits); ?></td>
                <td><?php echo $entry->user->username; ?></td>
                <td><?php echo $entry->entry_created_date; ?></td>
                <td>
                    <?php 
                        if ($entry->entry_edited_date == '0000-00-00 00:00:00') {
                            echo 'Not Modified.'; 
                        } else {
                            echo $entry->entry_edited_date;
                        }
                    ?>
                </td>
                <td>
                    <?php echo HTML::anchor('manage/editEntry/' . $entry->id, '<i class="icon-edit"></i>', array('rel' => 'tooltip', 'title' => 'แก้ไข')); ?>
                    <?php echo HTML::anchor('manage/deleteEntry/' . $entry->id, '<i class="icon-minus-sign"></i>', array('rel' => 'tooltip', 'data-placement' => 'right', 'title' => 'ลบบล็อกนี้', 'onclick' => 'return confirm(\'ต้องการลบบล็อกนี้ใช่หรือไม่?\')')); ?>

    <!--<a href="<?php //echo url::base();  ?>admin/delete/<?php //echo $catentry->id;  ?>" onclick="return confirm('ต้องการลบหมวดนี้ใช่หรือไม่?')" role="button" data-toggle="modal"><i class="icon-minus-sign"></i></a>-->
                </td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<hr>

<div class="pagination">
    <?php echo @$page_links; ?>
    <span class="pull-right"><?php echo Form::label('', '<h4><i class="icon-book"></i> ' . $count . ' Entries</h4>'); ?></span>
</div>

Ploblem. How I count comment on Comment count value?

Ploblem。我如何計算對評論計數值的評論?

View sample entries

查看示例條目

2 个解决方案

#1


1  

In view on "foreach" body you can write this:

鑒於“foreach”身體你可以這樣寫:

<?php echo $entry->comments->count_all(); ?>

#2


0  

In your view, inside the for loop, try inserting this:

在您的視圖中,在for循環中,嘗試插入:

<?php echo $entry->comments->find_all()->count(); ?>

That should work since you marked entry to has_many comments.

這應該有效,因為您標記了has_many注釋的條目。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/02/14/720ed5157a80f60be261044773b86aee.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com