views:

25

answers:

2

I'm using some Zend libraries outside of the Zend Framework in a small project. I'm using Zend_Db and Zend_Paginator but when I'm trying to set up the pagination using Zend_Paginator_Adapter_DbTableSelect I get an error that it can't find the class. Fatal error: Class 'Zend_Paginator_Adapter_DbTableSelect' not found in C:\xampp\htdocs\php_testing\zend\zend_db\index1.php on line 65 Here is my code:

<?
require_once 'Zend/Db.php';
$config=array(
    'adapter' => 'PDO_MYSQL',
    'hostname' => 'localhost',
    'dbname' => 'dm_xxxx',
    'username' => 'un',
    'password' => 'pw'

);
$db=Zend_Db::factory($config['adapter'], $config);
$select=$db->select()
        ->from('photocontest__photos', array('*'))
        ->order('created')
        ;    
require_once 'Zend/Paginator.php';
$currentPageNumber = ($_GET['page'] > 0)?$_GET['page']:'1';
$totalNumberOfItems = 11;
$itemsPerPage = 5;
$pageRange = 10;

$adapter = new Zend_Paginator_Adapter_DbTableSelect($select);
$paginator = new Zend_Paginator($adapter);
$paginator->setCurrentPageNumber($currentPageNumber);
$paginator->setItemCountPerPage($itemsPerPage);
$scrollType = 'Sliding';
$paginator = get_object_vars($paginator->getPages($scrollType));

foreach($paginator AS $item){
    echo $item->title.'<br>';
}
?>

thanks

######### UPDATE EDIT ##########

<?
$config=array(
    'adapter' => 'PDO_MYSQL',
    'hostname' => 'localhost',
    'dbname' => 'dm_xxxx',
    'username' => 'un',
    'password' => 'pw'

);
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();
$db=Zend_Db::factory($config['adapter'], $config);
$table = new Photos(array('db' => $db));
$select = $table->select();
$select->setIntegrityCheck(false)
        ->from('photocontest__photos', array('*'))
        ->order('created')
        ;       

$currentPageNumber = ($_GET['page'] > 0)?$_GET['page']:'1';
$itemsPerPage = 6;
$adapter = new Zend_Paginator_Adapter_DbTableSelect($select); //Setup the adapter object
$paginator = new Zend_Paginator($adapter); //Setup the actual paginator object

$paginator->setCurrentPageNumber($currentPageNumber);
$paginator->setItemCountPerPage($itemsPerPage); // items pre page

$scrollType = 'Sliding'; //change this to 'All', 'Elastic', 'Sliding' or 'Jumping' to test all scrolling types

Zend_Paginator::setDefaultScrollingStyle('Sliding'); // Sliding  or  Elastic
//Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');

$paginatorControl = get_object_vars($paginator->getPages($scrollType)); //and that's it! we can now use the $paginator variable as we want
?>
<h1>Zend_Paginator Demo</h1>
<ul id="items">
    <?php foreach($paginator AS $item): ?>
        <li><?php echo $item->title; ?></li>
    <?php endforeach ?>
</ul>

<div id="paginator">
    <p>Showing <strong><?php echo $paginatorControl['firstItemNumber'] ?> to <?php echo $paginatorControl['lastItemNumber'] ?></strong> out of <strong><?php echo $paginatorControl['totalItemCount'] ?></strong> items</p>

    <?php if($paginatorControl['previous']): ?>
        <a href="?page=<?php echo $paginatorControl['previous'] ?>">&laquo; Prev</a>
    <?php else: ?>
        <span>&laquo; Prev</span>
    <?php endif ?>

    <?php if($paginatorControl['firstPageInRange'] > $paginatorControl['first']): ?>
        <a href="?page=<?php echo $paginatorControl['first'] ?>"><?php echo $paginatorControl['first'] ?></a>
        <span>...</span>
    <?php endif ?>

    <?php foreach($paginatorControl['pagesInRange'] as $page): ?>
        <?php if($page == $paginatorControl['current']): ?>
            <span><?php echo $page ?></span>
        <?php else: ?>
            <a href="?page=<?php echo $page ?>"><?php echo $page ?></a>
        <?php endif ?>
    <?php endforeach ?>

    <?php if($paginatorControl['lastPageInRange'] < $paginatorControl['last']): ?>
        <span>...</span>
        <a href="?page=<?php echo $paginatorControl['last'] ?>"><?php echo $paginatorControl['last'] ?></a>
    <?php endif ?>

    <?php if($paginatorControl['next']): ?>
        <a href="?page=<?php echo $paginatorControl['next'] ?>">Next &raquo;</a>
    <?php else: ?>
        <span>Next &raquo;</span>
    <?php endif ?>
</div>
A: 

Edited: I dont think my frirst solution will help you unless you can live without Zend_Paginator_Adapter_DbTableSelect. But looking at your code have you tried including directely DbTableSelect.php?

Try using ::factory.

$paginator = Zend_Paginator::factory($adapter);


$paginator->setCurrentPageNumber($currentPageNumber);
$paginator->setItemCountPerPage($itemsPerPage);
$scrollType = 'Sliding';
$paginator = get_object_vars($paginator->getPages($scrollType));
Iznogood
I included it and now I get a different error:Fatal error: Uncaught exception 'Zend_Db_Select_Exception' with message 'Unrecognized method 'getTable()'' in C:\xampp\php\dev\include\ZendFramework-1.9.6\library\Zend\Db\Select.php:1320 Stack trace: #0 [internal function]: Zend_Db_Select->__call('getTable', Array) #1 C:\xampp\php\dev\include\ZendFramework-1.9.6\library\Zend\Paginator\Adapter\DbTableSelect.php(46): Zend_Db_Select->getTable() #2 C:\xampp\php\dev\include\ZendFramework-1.9.6\library\Zend\Paginator.php(750): Zend_Paginator_Adapter_DbTableSelect- ....etc...
EricP
Zend uses the autoloader to load all its classes and it seems you are not setup to use it. So I guess you have to include every file by hand. You could post a new question with this new information and get more precise help
Iznogood
I think I have that part working now by adding: require_once('Zend/Loader.php');Zend_Loader::registerAutoload();I'm still getting the error: Fatal error: Uncaught exception 'Zend_Db_Select_Exception' with message 'Unrecognized method 'getTable()'' I think it has to do with not calling my table name like I do when I'm in the ZF like this:$photoTable = new Photo_Model_Photos();$select = $photoTable->select();
EricP
I got it working. See my updates in my original post.I had to create a class for the db table name called "Photos.php" in the same directory with this contents: <?phpclass Photos extends Zend_Db_Table_Abstract{ protected $_name = 'photocontest__photos';} ?>The only thing I can't figure out is using the template file "pagination.phtml". It was looking for it in views/scripts. So I placed it there, but it had problems with it.
EricP
In my edited updated code, is there a way to tell set the table name without having to use: $table = new Photos(array('db' => $db)); and use a class file Photos.php ? I don't want to create seperate files just for setting a table name. I know it's great when using the entire framework, but in this instance I'm just using a few components. thanks
EricP
A: 
<?
$config=array(
    'adapter' => 'PDO_MYSQL',
    'hostname' => 'localhost',
    'dbname' => 'dm_xxxx',
    'username' => 'un',
    'password' => 'pw'

);
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();
$db=Zend_Db::factory($config['adapter'], $config);
$table = new Photos(array('db' => $db));
$select = $table->select();
$select->setIntegrityCheck(false)
        ->from('photocontest__photos', array('*'))
        ->order('created')
        ;       

$currentPageNumber = ($_GET['page'] > 0)?$_GET['page']:'1';
$itemsPerPage = 6;
$adapter = new Zend_Paginator_Adapter_DbTableSelect($select); //Setup the adapter object
$paginator = new Zend_Paginator($adapter); //Setup the actual paginator object

$paginator->setCurrentPageNumber($currentPageNumber);
$paginator->setItemCountPerPage($itemsPerPage); // items pre page

$scrollType = 'Sliding'; //change this to 'All', 'Elastic', 'Sliding' or 'Jumping' to test all scrolling types

Zend_Paginator::setDefaultScrollingStyle('Sliding'); // Sliding  or  Elastic
//Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');

$paginatorControl = get_object_vars($paginator->getPages($scrollType)); //and that's it! we can now use the $paginator variable as we want
?>
<h1>Zend_Paginator Demo</h1>
<ul id="items">
    <?php foreach($paginator AS $item): ?>
        <li><?php echo $item->title; ?></li>
    <?php endforeach ?>
</ul>

<div id="paginator">
    <p>Showing <strong><?php echo $paginatorControl['firstItemNumber'] ?> to <?php echo $paginatorControl['lastItemNumber'] ?></strong> out of <strong><?php echo $paginatorControl['totalItemCount'] ?></strong> items</p>

    <?php if($paginatorControl['previous']): ?>
        <a href="?page=<?php echo $paginatorControl['previous'] ?>">&laquo; Prev</a>
    <?php else: ?>
        <span>&laquo; Prev</span>
    <?php endif ?>

    <?php if($paginatorControl['firstPageInRange'] > $paginatorControl['first']): ?>
        <a href="?page=<?php echo $paginatorControl['first'] ?>"><?php echo $paginatorControl['first'] ?></a>
        <span>...</span>
    <?php endif ?>

    <?php foreach($paginatorControl['pagesInRange'] as $page): ?>
        <?php if($page == $paginatorControl['current']): ?>
            <span><?php echo $page ?></span>
        <?php else: ?>
            <a href="?page=<?php echo $page ?>"><?php echo $page ?></a>
        <?php endif ?>
    <?php endforeach ?>

    <?php if($paginatorControl['lastPageInRange'] < $paginatorControl['last']): ?>
        <span>...</span>
        <a href="?page=<?php echo $paginatorControl['last'] ?>"><?php echo $paginatorControl['last'] ?></a>
    <?php endif ?>

    <?php if($paginatorControl['next']): ?>
        <a href="?page=<?php echo $paginatorControl['next'] ?>">Next &raquo;</a>
    <?php else: ?>
        <span>Next &raquo;</span>
    <?php endif ?>
</div>
EricP