PHPEye开源社区 » Zend Framework 使用讨论 » 我想用zf做个分类程序,有没有好的方法啊?
《Programming PHP》第二版上市
2007-8-6 01:15 wohugb
我想用zf做个分类程序,有没有好的方法啊?

我的zf知识有限,谁有没有好的方法,来做一个分类程序,我看利用已有的zf程序无法实现,如果谢插件plugin,谁有没有做过啊?我不大会做这个啊!

2007-8-6 08:37 Haohappy
分类网站? 没什么特别的啊。不需要靠写什么插件来实现,从头开发吧。

2007-8-7 21:40 wohugb
恩,还有个问题 在視圖中調用一個自定義函數應該怎麽做?

2007-8-10 23:17 伶俜
i do this, but i don't know if any way better?????

[B]une idée.[/B]

1. add[QUOTE]$sql .= " SQL_CALC_FOUND_ROWS ";[/QUOTE] in line 133 zend/db/select.php
or rewrite the method

[B]2. Model[/B]
[php]
class Album extends Zend_Db_Table
{
        protected $_name = 'album';
       
        //get le total nombre de résultat
        public function getNombre()
        {
                $res = $this->_db->fetchRow("SELECT FOUND_ROWS() as total_nombre;");
                return $res["total_nombre"];
        }
}[/php]

[B]3. Controler[/B]
[php]
class IndexController extends Zend_Controller_Action
{
        public function indexAction()
        {
                $Album = new Album();
                $page =  (int)$this->_request->getParam('page', 0);
                $where = array();//à ajouter
                $sortby = 'title';//à modifier

                $this->view->albums = $Album->fetchAll($where,$sortby,NUM_PAR_PAGE,$page*NUM_PAR_PAGE)->toArray();

                $pagination = new Custom_Pagination($Album->getCountQuery(),$this->_request);

                $this->view->pagination = $pagination->render();
        }
}
[/php]
NUM_PAR_PAGE is a const

[B]4.View (smarty)[/B]
[php]
<table>
        {section name=album loop=$albums}
        <tr bgcolor="#FFFFFF">
                <td align="center">{$albums[album].title}</td>
                <td align="center">{$albums[album].artist}</td>
        </tr>
        {/section}
</table>
{$pagination}
[/php]

[B]5. class Custom_Pagination[/B]
[php]
class Custom_Pagination
{
    private $total_page;
    private $total_nombre;
    private $page_courante;
    private $_request;

        function __construct($total_nombre,$request)
    {
            $this->_request = clone $request;
            $this->page_courante = $this->_request->getParam("page");
                $this->total_page = ceil($total_nombre/NUM_PAR_PAGE);
                $this->total_nombre = $total_nombre;
    }
   
    function render()
    {
                //à ajouter les pages précédentes, suivantes .......
                $html = '';
                if($this->total_page > 1)
                {
                        for($i=0;$i<$this->total_page;$i++)
                        {
                                if($i>0)$html.= "/";
                                $this->_request->setParam("page",$i);
                                $html.= '<a href="'.$this->rebuildUrl().'">'.($i+1).'</a>';
                        }
                }
                return $html;
    }
   
    // reconstruire l'URL /album/index/index/page/1,/album/index/index/page/2,...
    //à améliorer. j'ai essayé $this->_request->setParam("page", $num_page); mais $this->_request->getRequestUri ne chage pas, si autre méthod?
    function rebuildUrl()
    {
            $url = $this->_request->getBaseUrl();
            $params = $this->_request->getParams();
            $url .="/".$params["module"];
            $url .="/".$params["controller"];
            $url .="/".$params["action"];
            foreach ( $params as $key=>$value)
            {
                    if(!in_array($key,array("module","controller","action")))
                    $url.="/".$key."/".$value;
            }
            return $url;
    }
}
[/php]

2007-8-18 01:28 wohugb
谢谢,收下!

页: [1]


Powered by Discuz! Archiver 5.5.0  © 2001-2006 Comsenz Inc.