Results per page

Been doing some light(ha!) reading in the PHP and MySQL monster book
looking for a way to limit the number of results shown per page. I’ve
tried a few things but I keep breaking the [mar] app.

What I want to do is take all the entries on the admin page (follow
the link below) and break it down into blocks of 10 with the
corresponding “next” and “previous” navigation.

http://www.anoptic.com/MAR/client

Todd


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

All of the basic MAR “finders” support OFFSET and LIMIT arguments. If
you are returning your results in ID order, you can simply set up a
route like

http://example.com/widgets/next/123

where 123 is the ID of the last element in the current result set, and
then have your finder look like this:

$widgets = ActiveRecord::FindAll('widgets','id > ' . $id,'id ASC',10);
//loop through your results and make your list of links to each
//then find the last element in the results and use that to construct  
the
//next link, and vice-versa for the previous link
$last = array_pop($widgets);
$next_link = $last->link_for('next','name','full','next');
$first = array_shift($widgets);
$previous_link = $first->link_for('previous','name','full','previous');

If you’re returning results in a different order, then it becomes more
complex. You have to figure out how many “pages” of results you will
have, then use this sort of route to navigate:

http://example.com/widgets/page/4

This example shows you how to construct the links and how to use the
result.

$total = ActiveRecord::Count('widgets');
$pages = ceil($total / 10);
$pages_array = range(1,$pages);
foreach($pages as $k=>$page){
	$class = ($page == $id) ? ' class="here"' : '';
	$pages_array[$k] = '<a href="widgets/page/' . $page . '"' . $class .  
'>' . $page . '</a>';
}
$page_links = implode(' | ',$pages_array);
$offset = $id * 10;
$widgets = ActiveRecord::FindAll('widgets',null,'name ASC',10,$offset);

Walter

On Jul 17, 2009, at 11:19 AM, Todd wrote:

Been doing some light(ha!) reading in the PHP and MySQL monster book
looking for a way to limit the number of results shown per page.
I’ve tried a few things but I keep breaking the [mar] app.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

It’s throwing a couple SQL errors. The case ‘index’: <http://pastie.org/549966

Todd

Notice: Undefined variable: id in /Applications/MAMP/htdocs/MAR/admin/
controllers/mar_controller.php on line 19

Warning: MyActiveRecord::Query() - query failed: SELECT * FROM mar
WHERE id > ORDER BY id ASC LIMIT 0, 10 with error: You have an error
in your SQL syntax; check the manual that corresponds to your MySQL
server version for the right syntax to use near ‘ORDER BY id ASC LIMIT
0, 10’ at line 1 in /Applications/MAMP/htdocs/MAR/admin/lib/
MyActiveRecord.0.5.php on line 200

Fatal error: MyActiveRecord::FindBySql() - SQL Query Failed: SELECT *
FROM mar WHERE id > ORDER BY id ASC LIMIT 0, 10 in/Applications/MAMP/
htdocs/MAR/admin/lib/MyActiveRecord.0.5.php on line 621


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

From what I can see the id in the MySQL is empty… WHERE id > ORDER
should look something like WHERE id > 24 ORDER

HTH

On Jul 17, 2009, at 11:51 PM, Todd wrote:

It’s throwing a couple SQL errors. The case ‘index’: <http://pastie.org/549966

Todd

Notice: Undefined variable: id in /Applications/MAMP/htdocs/MAR/
admin/controllers/mar_controller.php on line 19

Warning: MyActiveRecord::Query() - query failed: SELECT * FROM mar
WHERE id > ORDER BY id ASC LIMIT 0, 10 with error: You have an error
in your SQL syntax; check the manual that corresponds to your MySQL
server version for the right syntax to use near ‘ORDER BY id ASC
LIMIT 0, 10’ at line 1 in /Applications/MAMP/htdocs/MAR/admin/lib/
MyActiveRecord.0.5.php on line 200

Fatal error: MyActiveRecord::FindBySql() - SQL Query Failed: SELECT

  • FROM mar WHERE id > ORDER BY id ASC LIMIT 0, 10 in/Applications/
    MAMP/htdocs/MAR/admin/lib/MyActiveRecord.0.5.php on line 621

dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Yeah, I thought (and tried) that also but it just throws a different
set of errors.

Thanks,

Todd

On Jul 17, 2009, at 4:58 PM, Mike B wrote:

From what I can see the id in the MySQL is empty… WHERE id > ORDER
should look something like WHERE id > 24 ORDER


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

It would be best then to see what those errors would be because this
one ‘as you say’ is probably going to throw an error different to the
one that would be thrown when it has a value… and that would
probably be the one to look at. :slight_smile:

On Jul 18, 2009, at 12:11 AM, Todd wrote:

Yeah, I thought (and tried) that also but it just throws a different
set of errors.

Thanks,

Todd

On Jul 17, 2009, at 4:58 PM, Mike B wrote:

From what I can see the id in the MySQL is empty… WHERE id >
ORDER should look something like WHERE id > 24 ORDER


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Yes, that would make sense, wouldn’t it? I’ll post it tomorrow, I’ve
had my fill of looking at code today. My blood-shot eyes have had
enough.

Thanks,

T.

On Jul 17, 2009, at 5:16 PM, Mike B wrote:

It would be best then to see what those errors would be because this
one ‘as you say’ is probably going to throw an error different to
the one that would be thrown when it has a value… and that would
probably be the one to look at. :slight_smile:


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Do you have a normal routing.php in your stack? Does it have a section
devoted to finding the ID from the URI (always the third segment) and
setting $id to that?

Walter

On Jul 17, 2009, at 5:51 PM, Todd wrote:

It’s throwing a couple SQL errors. The case ‘index’: <http://pastie.org/549966

Todd

Notice: Undefined variable: id in /Applications/MAMP/htdocs/MAR/
admin/controllers/mar_controller.php on line 19

Warning: MyActiveRecord::Query() - query failed: SELECT * FROM mar
WHERE id > ORDER BY id ASC LIMIT 0, 10 with error: You have an error
in your SQL syntax; check the manual that corresponds to your MySQL
server version for the right syntax to use near ‘ORDER BY id ASC
LIMIT 0, 10’ at line 1 in /Applications/MAMP/htdocs/MAR/admin/lib/
MyActiveRecord.0.5.php on line 200

Fatal error: MyActiveRecord::FindBySql() - SQL Query Failed: SELECT

  • FROM mar WHERE id > ORDER BY id ASC LIMIT 0, 10 in/Applications/
    MAMP/htdocs/MAR/admin/lib/MyActiveRecord.0.5.php on line 621

dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Try adding this as a test (not a substitute for fixing whatever is
wrong in routing.php)

if(!isset($id)) $id = 0;

Walter

On Jul 17, 2009, at 5:51 PM, Todd wrote:

It’s throwing a couple SQL errors. The case ‘index’: <http://pastie.org/549966

Todd

Notice: Undefined variable: id in /Applications/MAMP/htdocs/MAR/
admin/controllers/mar_controller.php on line 19

Warning: MyActiveRecord::Query() - query failed: SELECT * FROM mar
WHERE id > ORDER BY id ASC LIMIT 0, 10 with error: You have an error
in your SQL syntax; check the manual that corresponds to your MySQL
server version for the right syntax to use near ‘ORDER BY id ASC
LIMIT 0, 10’ at line 1 in /Applications/MAMP/htdocs/MAR/admin/lib/
MyActiveRecord.0.5.php on line 200

Fatal error: MyActiveRecord::FindBySql() - SQL Query Failed: SELECT

  • FROM mar WHERE id > ORDER BY id ASC LIMIT 0, 10 in/Applications/
    MAMP/htdocs/MAR/admin/lib/MyActiveRecord.0.5.php on line 621

dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Yes, I have a routing file but I don’t see anything specific to
finding the ID from the URI.

routing.php http://pastie.org/550022

T.

On Jul 17, 2009, at 6:08 PM, Walter Lee Davis wrote:

Do you have a normal routing.php in your stack? Does it have a
section devoted to finding the ID from the URI (always the third
segment) and setting $id to that?


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

I’ve commented the relevant lines, and added a refinement at line 16
which should catch your error in all cases.

Walter

On Jul 17, 2009, at 7:15 PM, Todd wrote:

Yes, I have a routing file but I don’t see anything specific to
finding the ID from the URI.

routing.php http://pastie.org/550022

T.

On Jul 17, 2009, at 6:08 PM, Walter Lee Davis wrote:

Do you have a normal routing.php in your stack? Does it have a
section devoted to finding the ID from the URI (always the third
segment) and setting $id to that?


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Duh!

http://pastie.org/550087

forgot the actual link…

Walter

On Jul 17, 2009, at 8:35 PM, Walter Lee Davis wrote:

I’ve commented the relevant lines,


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

I’m feeling like the village idiot at the moment (where did I put my
dunce hat anyway?). I updated routing.php but still get errors. I
haven’t setup the actual “next” and “previous” links yet so I’m
guessing that the 3rd error below is referring to that but I’m not
certain. I think I’ll put it all back the way it was and step away. My
head is obviously not where it needs to be to do this and I’m sure it
shouldn’t be this difficult.

Thank you for your help.

Todd

Notice: Undefined variable: id in /Applications/MAMP/htdocs/MAR/admin/
routing.php on line 17

Warning: array_pop() [function.array-pop]: The argument should be an
array in /Applications/MAMP/htdocs/MAR/admin/controllers/
mar_controller.phpon line 23

Fatal error: Call to a member function link_for() on a non-object in /
Applications/MAMP/htdocs/MAR/admin/controllers/mar_controller.php on
line 24

On Jul 17, 2009, at 7:35 PM, Walter Lee Davis wrote:

I’ve commented the relevant lines, and added a refinement at line 16
which should catch your error in all cases.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options