By nadongtae on
Can anyone show me an example how to use pager_query() which is more detail than the example in http://api.drupal.org/api/HEAD/function/pager_query.
Can anyone show me an example how to use pager_query() which is more detail than the example in http://api.drupal.org/api/HEAD/function/pager_query.
Comments
Here you go...
Useful code that can be stolen from the taxonomy module:
Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database
NancyDru
It's too complicated
The code is so complicated. Can you give me a simple one ? It will be better if you can show me a simple module . Thanks for your supporting
Try this one
Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database
NancyDru
Not working ...
Hello @NancyDru,
I am using "pager_query($select . $where . $order, $items_per_page, 0, $sql_count);" function but It is not woking and it show
Fatal error: Call to undefined function pager_query();
like : $result = pager_query($Sql, 10, 0, count($Sql));
pager_query() doens't exist
pager_query() doens't exist in Drupal 7 - so I suspect you are using Drupal 7. To have a pager in Drupal 7, read the following: https://www.drupal.org/node/508796
Contact me to contract me for D7 -> D10/11 migrations.
pager_query is not working for me
friend
here i have some problem with pager_query
$sqlquery="SELECT profile.nid,profile.field_name_value,profile.field_age_value,profile.field_sex_value,profile.field_height_value,t1.name AS caste,t2.name AS religion,t3.name AS location,files.filepath AS imgloc FROM {content_type_profile} AS profile INNER JOIN term_data AS t1 ON profile.field_caste_tid=t1.tid INNER JOIN term_data AS t2 ON profile.field_religion_tid=t2.tid INNER JOIN term_data AS t3 ON profile.field_location_tid=t3.tid LEFT JOIN files ON (files.nid=profile.nid-1 AND files.filename='".$Imagetpe."') WHERE profile.field_sex_value='".$sex."'";
$Countqry="SELECT COUNT(*) FROM {content_type_profile} AS profile INNER JOIN term_data AS t1 ON profile.field_caste_tid=t1.tid INNER JOIN term_data AS t2 ON profile.field_religion_tid=t2.tid INNER JOIN term_data AS t3 ON profile.field_location_tid=t3.tid LEFT JOIN files ON (files.nid=profile.nid-1 AND files.filename='".$Imagetpe."') WHERE profile.field_sex_value='".$sex."'";
$sqlquery=$sqlquery." AND profile.field_age_value BETWEEN'".$age_lower_limit."' AND '".$age_upper_limit."' ORDER BY profile.nid DESC";
$GL_count_query=$Countqry." AND profile.field_age_value BETWEEN'".$age_lower_limit."' AND '".$age_upper_limit."'";
$result=pager_query($sqlquery,10,0,$GL_count_query);
while($data= db_fetch_object($result))
{
//,...............some code here
}
$search_result = theme('table',$header,$newdata);
$srch_rslt .=$search_result;
$srch_rslt .=theme('pager');
it is showing ten records but there is no links other result page
Hmm...
If you only have ten records there won't be any pager links.
NancyDru (formerly Nancy W. until I got married to Drupal)
NancyDru
pager_query is not working for me
There are more than 10 records
it is showing only ten records
there is no link to other result page
Count SQL?
Have you confirmed that the count(*) sql you are using actually produces the right number of records? If your count query is incorrect, this could be the problem.
Dave
My site: http://www.unitorganizer.com/myblog
I'm Probably Missing Something Obvious
I've adapted examples from the Drupal site to come up with the below code. I get the pager display with the correct number of pages based on the number of records meeting the SELECT criteria, but instead of data returned from the database, I get:
"Resource id #74Resource id #74Resource id #74Resource id #74Resource id #74Resource id #74Resource id #74Resource id #74Resource id #74Resource id #74Resource id #74Resource id #74".
What do I need to do to get the actual data to display? Any help greatly appreciated.
function pager_test(){
drupal_set_title("Pager Test");
//This is numbers per page
$num_per_page = 12;
//actual query
$query = "SELECT STORE_ID AS ID, STORE_STATE AS STATE FROM {stores} WHERE STORE_STATE = 'SC'";
//the count query should be similar to the query above
$count_query = "SELECT COUNT(*) AS row_count FROM {stores} WHERE STORE_STATE = 'SC'";
//pager_query function
$rs = pager_query($query, $num_per_page, 0, $count_query);
while ($data = db_fetch_object($rs)){
$output .= $rs;
}
//dont forget
$output .= theme('pager');
return $output;
}
Try this
It's a simple query; you don't need a count query. But it might help to tell the pager theme how many per page.
BTW, you might also be interested in http://drupal.org/coding-standards
NancyDru
Thanks for Reply
Hey Nancy, thanks for a quick reply.
Using the example you posted, I get the same "Resource ID #74 displayed 12 times. However, the code is cleaner and more to the point, so I'll keep trying with it. I'll also take a lookie at the coding standards link.
Oops
Yes, I guess you would.
NancyDru
Doh!
It's the simplest things that usually trip me up.
Went back and looked at a project I had done in MySQL/PHP where I displayed paged results. Below is the code that now works. Thank you for the nudge in the right direction.
While I'm thinking about it, on the line "$nd .= ( - );", my intention was to put some space between the 2 columns. Is there a better way to do this? A reference to a good example of how to include header info in the query results would probably be spot on.
Here you go
This will give you a table that you can add attributes to if you want. It automatically allows you to colorize the odd and even rows. HowTo: Create Simple Tables
http://api.drupal.org/api/function/theme_table/6
NancyDru
Awesome!
That is so cool. Thank you very much for taking the time to help a "Drubie" (you probably figured it out, but it's Drupal Newbie).
We all were
We all were newbies at one time. When you find one who is trying, it is a pleasure to help.
NancyDru
Please help!
Hi Nancy,
I'm real new in Drupal. I've used your code to page my Drupal content, but it didn't show anything. Here is my code:
Please help me out as soon as possible. I want to display 20 records per page!
TNX in advance!
I see two potential
I see two potential problems:
1) You are maybe connecting to the wrong database. For that matter, you shouldn't even be connecting to a database - drupal already takes care of the connection for you.
2) Your query is probably not returning anything due to your adding the $dbk variable to pager_query().
Contact me to contract me for D7 -> D10/11 migrations.
My database was working until
My database was working until I replaced my code and i've removed $dbc from pager_query(), but it's still not showinganything. Do I have to enable or disable something?
Are you sure your query is
Are you sure your query is built right? Try running a regular query on it (not pager_query) and making sure that it returns something.
Contact me to contract me for D7 -> D10/11 migrations.
Hmm...
FROM m_entrepreneursshould beFROM {m_entrepreneurs}. Without the brackets you won't get the table prefixes, if there are any. Secondly, and I have no idea why, I have seen Drupal queries with a large amount of white space no work right. That's the easiest thing to fix.Try running the same query in PhpMyAdmin and see if it works at all. Typically the only way this type of code "Doesn't do anything" is if there are now rows selected. Almost anything else will generate an error message.
NancyDru
Query is workin' fine
query is working fine in phpmyadmin! Actually here is my previous code that works perfect and displays all the data with current date! I just wanted to edit this code to show pagers! So please help me with this one:
Where should I edit here?
Use the drupal functions
You really need to read this http://api.drupal.org/api/function/db_query/6.
There is no good reason to be using any function that starts with mysql_*.
Drupal handles all the database connection stuff for you.
Also you were using pager_query to generate objects, and i believe theme_table wants arrays.
Also you should check out http://api.drupal.org/api/function/theme_table/6.
am i wrong or this code
am i wrong or this code allows sql injection?
Hmm...
I'm not sure which code sample you're referring to. It is not uncommon to find less than secure code snippets in the forums, simply because they are often thrown together for demonstration. No, that doesn't make it right.
NancyDru
yep, i've found several
yep, i've found several pieces with the tipical mysql "... where uid=".$uid ...".
it should be good idea to correct or point these errors, it makes no sense telling everybody to follow the good coding practices and having all the forums and docs plenty of code not following these practices.
Good to know anyway, thank you NancyDru.
i was in the same boat as
i was in the same boat as you a couple of weeks ago..it worked for me
Hope this helps..
--------------------------------------------------
http://www.usingdrupal.com
Tutorial Screencasts
---
WebWash: Drupal Tutorials and Videos
Thanks for your supporting but
Because i'm so new to drupal, Can you show me how it works in a simple module ? I mean if you can show me a demo for that . Thanks you very much .
Going offline
emailing time...
Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database
NancyDru
Can you send a simple source code
This is my email address : kanguru1225@gmail.com. Can you send me a simple demo ?
Complicated but well commented demo
If you download the new project, og_management, and read the source code for this module, in the 'og_management_load_subscribers' function, I use pagination when I do a couple of the queries.
Dave
My site: http://www.unitorganizer.com/myblog
How to apply pagintation to a table (array)
Hi I'm new in drupal and I'm trying to do a pagination in a table which the results are coming from an array.
The array looks like this :
I have no idea how to do that, please help?
Thanx
-Raff
Pagination is for Database queries
Drupal Pagination is for database queries, if you are constructing your array in a different way, you'll have to custom create the pagination as far as I know.
However, presumably at some stage you are collecting information from the database, although it could be well before that array is constructed.
How do you know how many rows there will be in your table anyway?
Dave
My site: http://www.unitorganizer.com/myblog
Well...
How about "count(array_name)"?
Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database
NancyDru
I think he wants more than this
I think he wants to take his giant array and display parts of it depending on which link the user chooses. Ie, generate a huge array, and display parts of it. However since most building of arrays can be eventually traced back to some database query, I'm pretty sure he can find a way to use pager_query.
Dave
My site: http://www.unitorganizer.com/myblog
A similar kind of query based on the pagination
Hi,
I am new to Drupal. I tried using pager_query() for my data to be shown as a paginated result. I have a problem here. My table consists of some 30000 rows and i want to show only the 1st 50 records which are descending ordered. Looking at the code for the pager_query, it is constructed in a way that i could not limit the records retrieved as per my wish. Irrespective of whatever is the number of records i need to show in my site, drupal wants to retrieve all the records from the table. Hope my question is clear. To put in simple words, "How can i show only a limited number of rows that i desire instead of retrieving all the records from the table?"
Any help is greatly thanked.
Regards,
~ Presel Karthik
LIMIT
Have you tried using the LIMIT clause? SELECT * FROM ... ORDER BY ... LIMIT 50
Nancy Dru (formerly Nancy W. until I got married to Drupal)
NancyDru
Yes. I tried using the limit
Yes. I tried using the limit clause. But the results were not shown when i used the limit. I am looking more deep into it now.
Well as per my
Well as per my understanding, i have to pass 'the actual query' as well as 'the query to get the count of records' as parameters to the pager_query function right?
Based on this understanding i passed 'the actual query' and the 'same query with limits' - to get the count, but then i get the result as follows:
The first scenario is i need 50 records and those records will be displayed in pages of 10 records each. So total 5 pages. Now the page 1-4 shows 10 records each and the 'Showing 10 of 50' was same as i pasted here. But for the 5th page, the number of records shown is 10, which is as expected, but 'Showing 0 of 50' is displayed in the top left part of the pagination which NOT correct.
The second scenario is i gave 0,49 in the limit of the 'the query to get the count of records'. But the result is again total 5 pages.And the page 1-4 shows 10 records each and the 'Showing 10 of 50' was same as i pasted here. But for the 5th page, the number of records shown is 10, which is NOT as expected, but 'Showing 9 of 49' is displayed in the top left part of the pagination as expected.
It depends
The count query is only needed if the query is complex.
Nancy Dru (formerly Nancy W. until I got married to Drupal)
NancyDru
Also i tried giving limit in
Also i tried giving limit in the actual query also. This does not give me any result but blank
I think i found a patch for
I think i found a patch for this...
http://drupal.org/node/65169
Need to try implementing this
It does not solve my problem
It does not solve my problem of getting 'Showing 0 of 50' in the last page even though i get last 10 records out of the total 50 records my query retrieved.
Any help greatly appreciated.
Post an issue
You might need to post an issue at http://drupal.org/project/issues/drupal
Nancy Dru (formerly Nancy W. until I got married to Drupal)
NancyDru
I never knew this existed
I never knew this existed before today. Here is what I am doing:
The pagination outputs at the bottom fine. But when I hit a page number or Next, I just get a blank page. Anyone know why? I am passing a predefined $query into this function, it is coming from a form POST / real estate search form.
I feel like im very close.
==============
delaware web design
delaware website design
===========
read my thoughts
???
I don't see how this could work, but I am far from a MySql expert.
NancyDru
It works perfectly- I can
It works perfectly- I can post the code I came up with when I get back to work tomorrow.
==============
delaware web design
delaware website design
===========
read my thoughts
Here we are. This is what I
Here we are. This is what I came up with that works. This is for a page with a listing of homes for sale, similar to the way this page looks http://www.houseplans.com/asearch.asp:
Pretty straightforward. This hooks into a search form and is called from a special page, searchresults.php, which is include() with php on specific pages.
==============
delaware web design
delaware website design
===========
read my thoughts
Not very secure
It looks like you are setting variables directly equal to GET and POST variables though and then directly injecting them into an SQL query. There must be a way to avoid doing that. I'd take a look at the Forms API so you don't need to inspect GET and POST directly, which helps with 'Cross site request forgery'. I'd also use placeholders for your SQL query (%d, %s, etc...) so that you aren't using user input directly in your sql query.
All someone needs to do is enter ?homeType=sql_injection;SELECT password FROM node WHERE uid = 1 and they'll have your admin password (hashed) and then use a rainbow table to look up your clear-text password. Even worse they could do ?homeType=sql_injection;DELETE * FROM node_revisions WHERE nid > 0 and delete all of your site content.
Dave
My site: http://www.unitorganizer.com/myblog
What is Forms
What is Forms API?
I'm using Drupal 5.7.
==============
delaware web design
delaware website design
===========
read my thoughts
Forms Application Program Interface
http://api.drupal.org/api/file/developer/topics/forms_api.html/5
NancyDru
How do you use the
How do you use the placeholders inside a pager_query()?
EDIT: Nevermind - it's in the api. Same as good old db_query().
A comprehensive example
You can find a comprehensive example at:
http://molecularsciences.org/drupal/drupal_paging
I'd like to see
I'd like to see your example meet Drupal coding standards. Also, with the simple query you are using, a count query is unnecessary.
NancyDru
Quick question: Does anyone
Quick question: Does anyone know how I can number each results? For example, if the query yields 8000 results that are split into pages of 20, right now I would see this on each page:
Result 1
Result 2
Result 3
...
Result 20
and so on for all 400 page.
What I would like is for page one to display:
1. Result 1
2. Result 2
3. Result 3
....
20. Result 20
and then for page 2 to display:
21. Result 21
22. Result 22
22. Result 23
....
40. Result 40
and so on for all 400 pages.
If you're confused by what I mean, check www.reddit.com.
Any advice on how I can modify my SQL query would be greatly appreciated.
Well...
First you would have to render your list as an OL (which can be done with theme('item_list',...))The page function places a "page" query string in the URL, so it would be available in $_GET. You could then multiply that by the number of results per page and use that in the "start" attribute.
NancyDru (formerly Nancy W. until I got married to Drupal)
NancyDru
Is it possible to use this
Is it possible to use this with 2 different tables in a page? if so, can anyone pls tell me how? thx so much
Yes, It's Possible to use Two Tables (or more) per Page
Look at the comments for the pager.inc file (in /includes folder).
To have more than one pager query per page assign each an $element value.
Your coding syntax may vary, but the $element tells Drupal which pager_query is which.
You can use 0 as your first $element value for the first pager_query, 1 for the next, and so on.
thx so much... that helped a
thx so much... that helped a lot... which points me to another problem... is there a way to put anchors on the page numbers URL?
or I have to resort to make something with jQuery/javascript to make the page stay on the current scroll location. My tables could go way down the page in the future depending on content.
Not Sure About Anchors
Anchors are just html links to specific locations on a page, so you could insert them into your code. If you could explain why you want to do so, perhaps someone can give a nudge in the right direction.
Ok, my apologies for not
Ok, my apologies for not explaining it fully.
Let me start by:
- I have put my tables in a block w/c is placed on the 'below content'(added block region) part of the page. The reason for this is because the other user that can access this page is only concerned on adding text content on the page. And doesn't know much on code.
- So, once the other user adds content my tables will go way down the page and it wouldn't be good if he/she would have to scroll down again and again when they click the next page of either tables.
Before, I did my tables with just php, it worked fine but wanted to take advantage of the drupal API. I am also new to Drupal, thanks so much for the help. I very much appreciate it.
No Worries
I'm also from a primarily php/mysql background, and am still getting used to Drupal's API. While I can't answer your question without some research, I'm sure someone will be along presently with some help.
Maintain Scroll Position of EVERY Element on PostBacks
Hello,
After a few research, I have come across Mr. Mahdi Hasheminezhad's blog. He made a nice javascript plug-in, that solves my problem. Here is the URL to his blog : http://en.hasheminezhad.com/scrollsaver
Cheers!
I think this would help you
Click here pager_query()
Regards,
Veera Prasad Dagudu