db paging in plugin

Creating and modifying plugins.
Post Reply
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

db paging in plugin

Post by Timbalu »

Hi Gravin

How can I use serendipities (entry)paging functions inside a new plugin?
Is there an example?
I do not mean the entry table date, its a new plugin table.
I tried to include a paginator class I always used, but this gets messed up with s9y classes.

Thanks in advance
Ian
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: db paging in plugin

Post by garvinhicking »

Sorry, I don't understand what you mean, please describe it exactly. :)

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Post by Timbalu »

well.... I´ll try ;)
I need a n Entries/next/previous paging function for some db data.
I tried to have a look at entrypaging plugin, bur I could not find the right
Idea how to use this sort of stuff for a new plugin I want to use.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Ah, okay.

Well, pagination is really simple:

Code: Select all

$min_page = 0;
$results_per_page = 15;
$current_page = $serendipity['GET']['page'];
if (empty($current_page)) {
  $current_page = $min_page;
}

$result = serendipity_db_query("SELECT * FROM yourstuff " . serendipity_db_limit_sql(serendipity_db_limit($current_page * $results_per_page, $results_per_page)));

$max_page = count($result) / $results_per_page;

echo "You are on Page " . ($current_page+1) . " of " . ($max_page+1) . " pages. Go to page:<br />";
for ($i = $min_page; $i <= $max_page; $i++) {
  echo '<a href="index.php?serendipity[subpage]=stuff&serendipity[page]=' . $i . '">' . $i . '</a>';
}
It's even so simple that creating functions for this is pretty much overhead and unneeded in most plugin cases that have to freeform their output nevertheless. :)

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Post by Timbalu »

Ahhhh, simple and effective!
Merci bien!
Ian
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Post by Timbalu »

garvinhicking wrote:

Code: Select all

$result = serendipity_db_query("SELECT * FROM yourstuff " . serendipity_db_limit_sql(serendipity_db_limit($current_page * $results_per_page, $results_per_page)));

$max_page = count($result) / $results_per_page;
In case someone wants to use this...
count($result) will not give you the full sum of items of your table, because the db_limit_sql function will not allow it.
You need a resultset without those limit functions.

Ian
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Ah, that's true, thanks for noticing. I adapted the code:

Code: Select all

$min_page = 0;
$results_per_page = 15;
$current_page = $serendipity['GET']['page'];
if (empty($current_page)) {
  $current_page = $min_page;
}

$fullresult = serendipity_db_query("SELECT count(*) AS counter FROM yourstuff", true);

$result = serendipity_db_query("SELECT * FROM yourstuff " . serendipity_db_limit_sql(serendipity_db_limit($current_page * $results_per_page, $results_per_page)));

$max_page = $fullresult['counter'] / $results_per_page;

echo "You are on Page " . ($current_page+1) . " of " . ($max_page+1) . " pages. Go to page:<br />";
for ($i = $min_page; $i <= $max_page; $i++) {
  echo '<a href="index.php?serendipity[subpage]=stuff&serendipity[page]=' . $i . '">' . $i . '</a>';
} 
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Synchro
Regular
Posts: 8
Joined: Thu Apr 28, 2005 12:03 pm
Location: London

There is a more efficient way...

Post by Synchro »

Check out the MySQL FOUND_ROWS() function:

http://dev.mysql.com/doc/mysql/en/infor ... tions.html

Just FYI, but I thought it was quite a cool feature.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: There is a more efficient way...

Post by garvinhicking »

Sadly this is MySQL only and would not work on our PostgreSQL or SQLite backends, thus I don't encourage it's usage.

Thanks for the pointer, though! :)

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
carl_galloway
Regular
Posts: 1331
Joined: Sun Dec 04, 2005 5:43 pm
Location: Andalucia, Spain
Contact:

Post by carl_galloway »

Hi all,

I'm trying to work out whether I can use smarty to add entry paging to a hard-coded location on the detail page of a theme. I don't want to use the entrypaging plugin because it outputs additional divs, I simply want to use smarty much like I do for the next prev links in overview mode.

So basically I want to;

if overview page, then next prev is next prev overview page
elseif detail page, then next prev is next detail page

if this is possible, then we wouldn't need the entry paging plugin at all, we would just need to call the appropriate variable either $footer_next_page, or $detailfooter_next_page. And the same for the previous pages.

Could this be done, and apologies for posting here, I just thought since this thread is already open I might add to it.

Carl
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi Carl!

I've patched up this: http://nopaste.php-q.net/189279

It inserts these smarty variables if you set the plugin config placement to smarty:

{$pagination_(next|prev|random)_(title|link)}

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
carl_galloway
Regular
Posts: 1331
Joined: Sun Dec 04, 2005 5:43 pm
Location: Andalucia, Spain
Contact:

Post by carl_galloway »

I'll go and try that and let you know how it works, thanks.

Carl
Post Reply