PDA

View Full Version : Paging - How?



franzisk
11 May 2007, 4:12 AM
I still didnt figure out how this pagging stuf works.
Could anybody give me (in a few words) a tip how to proceed to do pagging?

Example:
I have a table (in MySQL accessed with PHP) with 70.000 lines (records), I want to show pages in a grid with 25 lines per page.

What I have until now:
acion.php

$start = $_POST['start'];
$limit = $_POST['limit'];
$searchId = $_POST['searchId'];

$sql = 'SELECT
accountID id,
screenName username,
headline title,
profile_Writeup body,
dLastLogon login,
topMember topmember
FROM
`MemberProfile`
LIMIT '.$start.', '.$limit.';';
...
$json = new Services_JSON();
$output = $json->encode($search);
print("{totalCount:\"".count($search)."\", data:".$output."}");


Part of my Grid code:

var GridUI = function() {

var ds; //hold our data
var grid; //component
var columnModel; // definition of the columns
var dialog;

function setupDataSource() {
ds = new Ext.data.Store({
proxy: new Ext.data.HttpProxy ({url:'action.php'}),
reader: new Ext.data.JsonReader(
{root:'data', id: 'id', totalProperty: 'totalCount'},
[
{name: 'id', type: 'int'},
{name: 'username'},
{name: 'title', align: 'center'},
{name: 'photo', align: 'center'},
{name: 'login', type: 'date', dateFormat: 'Y-m-d H:i:s'},
{name: 'topmember', type: 'boolean'}
]
)
});
ds.load({params:{start:0, limit:25, searchId:1}});
}
...

// add a paging toolbar to the grid's footer
var paging = new Ext.PagingToolbar(gridFoot, ds, {
pageSize: 25,
displayInfo: true,
displayMsg: 'Showing profile {0} - {1} of {2}',
emptyMsg: "No profiles to show"
});


}
...

What can I do to have the correct total of records and what can I do to go to the next set of 25 records?

Still (for who uses php), is there any way to get ISO-8859-1 characters correctly with json->encode ?

Thanks in advance

BernardChhun
11 May 2007, 4:35 AM
reading this tutorial should help you figure out most of it : http://extjs.com/tutorial/basics-paging-grid-component

if you use firebug, you can also check what are the post values that are sended to your php page.

jsakalos
11 May 2007, 5:27 AM
For total count you have to:



select count(*) from table


Once paging toolbar gets proper total count it handles paging nicely itself. You don't need to do anything special. As I can see your server handles posted start and limit so everything should by ok.

About ISO-8859-1: Consider switching to utf8. While this may be quite a task to convert all databases on the other hand it will pay back in the long run. I have my own experiences with switching from ISO-8859-2 to utf8.

Meanwhile you can use PHP's iconv function to convert back and forth

franzisk
11 May 2007, 5:39 AM
Thanks a lot (again) for your reply.

I am already with the pagging working like a charm.

Here is what is happening about the charset:

[FONT="Courier New"]I have in the database this word "condi

jsakalos
11 May 2007, 5:42 AM
I guess there is no need to convert if you supply charset utf8 in page's content type. Browser will then know how to display.

Check with firebug if you get something like this as headers:



Content-Type application/json; charset=utf-8

franzisk
11 May 2007, 6:03 AM
Nope, Firefox asks where I want to open that file.

I saw somewhere something doing that in JavaScript (converting it back), I am not able to remember where and how.

jsakalos
11 May 2007, 6:06 AM
:-/ No idea what file you mean....

Do you use firebug?

See attachment.

franzisk
11 May 2007, 6:14 AM
I mean the PHP script, the page where I am getting the json values.

I dont see anything in Firebug console, once the page is not loaded at all.

It seems for me that Firefox doesnt know what to do with that header.

jsakalos
11 May 2007, 6:32 AM
Can you post your headers from firebug's console? Same as I did...

franzisk
11 May 2007, 6:42 AM
Yes, I can, there it is.

jsakalos
11 May 2007, 6:52 AM
That's it! Your browser is informed that charset is ISO-8859-1 while it is utf-8 in fact. Try to experiment with this.

I live in Slovakia and I'd been struggling with ISO/Windows/Linux/utf8 encodings for a while;)

Then I've decided to change everything around me what is under my control to utf-8 and the encoding problem is over. By everything I mean e.g. filesystems encodings of servers (I use Linux servers exclusively), database servers (MySQL), data in database servers, web pages, mailing, just everything.

Once all this was setup properly, I haven't had any encoding issue.

franzisk
11 May 2007, 7:51 AM
It's impossible for me to do that now.

So I decided to create my own json PHP function, now it's working (I will get it better as soon as possible , and necessary).


function getJSON($array){
$total_records = count($array);
$json_object = '[';
if($array){
$j = 0;
foreach($array as $key => $value){
$j++;
$record = $value;
$total_columns = count($record);
$json_object .= '{';
$i=0;
foreach($record as $k => $v){
$i++;
$json_object .= '"'.$k.'":"'.addslashes($v).'"'.($i<$total_columns?',':'');
}
$json_object .= '}'.($j<$total_records?',':'');
}
}
$json_object .= "]";
return $json_object;
}


Thanks a lot for your help.

Can you point where I can find material about having kind of filter in my grid?

jsakalos
11 May 2007, 8:11 AM
I have posted something in some threads plus search for "protein+database". I just wouldn bother to implement that quick local search as it is in the latter. Should you really run in troubles post a question. I've done it so I know how.

tryanDLS
11 May 2007, 8:12 AM
Use the Search function - there's a thread in Examples.

wlambardi
3 Sep 2007, 9:57 AM
The paging with grid tutorial uses $_GET to pass the start and limit variables, but to me it only worked with $_POST.


$qry = "SELECT * FROM TITUBA LIMIT ".$_POST['limit']." OFFSET ".$_POST['start'];

I