views:

333

answers:

3

I am having a serious problem converting my 'select' statement into something that will work with the zend paginator... could someone have a crack at it, as I am having no luck...

Here is my query:

$query = "SELECT
         user_id, name, gender, city, province, country, image_id, one_liner, self_description, reputation
          FROM
         users
          WHERE
          (
         (69.1 * (latitude - " . $user->latitude . ")) * 
         (69.1 * (latitude - " . $user->latitude . "))
          ) + ( 
         (69.1 * (longitude - " . $user->longitude . ") * COS(" . $user->latitude . " / 57.3)) * 
         (69.1 * (longitude - " . $user->longitude . ") * COS(" . $user->latitude . " / 57.3))
          ) < " . pow($radius, 2) . " 
          ORDER BY 
          (
                (69.1 * (latitude - " . $user->latitude . ")) * 
         (69.1 * (latitude - " . $user->latitude . "))
          ) + ( 
         (69.1 * (longitude - " . $user->longitude . ") * COS(" . $user->latitude . " / 57.3)) * 
         (69.1 * (longitude - " . $user->longitude . ") * COS(" . $user->latitude . " / 57.3))

Here is what I have so far:

  $select = $db->select();
  $select->from(
   array('users'),
    array(
      'user_id', 
      'name', 
      'gender', 
      'city', 
      'province', 
      'country', 
      'image_id', 
      'one_liner', 
      'self_description', 
      'reputation'
     )
  );
  $select->where("(69.1 * (latitude - " . $user->latitude . ")) * (69.1 * (latitude - " . $user->latitude . "))) + ((69.1 * (longitude - " . $user->longitude . ") * COS(" . $user->latitude . " / 57.3)) * (69.1 * (longitude - " . $user->longitude . ") * COS(" . $user->latitude . " / 57.3))) < " . pow($radius, 2));
  $select->order("(69.1 * (latitude - " . $user->latitude . ")) * (69.1 * (latitude - " . $user->latitude . "))) + ((69.1 * (longitude - " . $user->longitude . ") * COS(" . $user->latitude . " / 57.3)) * (69.1 * (longitude - " . $user->longitude . ") * COS(" . $user->latitude . " / 57.3))) ASC");
+1  A: 

Why do you have "<" in your order by clause?

erenon
removed it, still having probs though
Mike Curry
this Answer should have been a comment... ?
anonymous coward
+1  A: 

What does this have to do with Zend_Paginator? Ah, do you have the query and you don't know how to make a paginator with it, or is the paginator not working with this query?

The only thing I can see is you're missing an opening parenthesis in both the where() and order() clause:

$select->where("((69.1 * [...] ");
$select->order("((69.1 * [...] ");
                 ^

So maybe Zend_Paginator isn't working because the SQL query has errors?

And of course I have to ask: are those variables you're interpolating safe, or should you really be using $db->quote($user->latitude, Zend_Db::FLOAT_TYPE)?

mercator
A: 

Assuming you are using MVC-pattern, won't this work?

in your bootstrap:

Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');

in your controller:

$page = Zend_Paginator::factory($select);
$page->setCurrentPageNumber($this->_getParam('page', 1));
$page->setItemCountPerPage($this->_getParam('par', 20));
$this->view->results= $page;

in your view:

<?php foreach($this->results as $result) : ?>
    <!-- print some $result stuff here -->
<?php endforeach;?>
<?= $this->results ?>

then place a pagination.phtml example that you can find on zend manual -Lo

Lau