Jump to content

problem with advanced pagination


tastro

Recommended Posts

ok here is my problem... for example: i have 1000 files with 15 entrys each. but in the last one there is only 8 entrys. (i want to make a pagination)

 

i want to display 15 entrys on each page.

 

also how to calculate from which entry to which should it read. (i have another text file in which is the number of total entrys in all files together)

 

for the first page it should read the first 8 and from thefirst file and the first 7 from the second file to get 15 displayed on the page.

 

i was tryin now for some hours, but i just can't figure it out. :S :D and i know that i will think how stupid i'm after i will see how it should be done. :S

Link to comment
Share on other sites

i have 1000 files with 15 entrys each. but in the last one there is only 8 entrys.

 

Can you explain how you have the data stored a little clearer? 'Files' and 'entries' is a little vague to give you any kind of code solution..

Link to comment
Share on other sites

Generally you'd be using a db, and it would easily be able to count the number of entries there are (or just as many as are required). When using files like this, you'd need to count all the number of entries in each lower file (i'll say, store those in an array). Then knowing what page your on, say entries 45 to 60, you'd have to iterate through the entry counts array and add up as you go, checking each time to see if your in range, if so, you got your your main file to extract from. Next you'd need to calculate if you'll need any entries from the previous or latter files...

 

 

Tip if you don't already know, modulo (or %) comes in handy at times here...

Link to comment
Share on other sites

for now i have this (made myself):

 

function ea($f,$n){
echo 'Reading '.$n.' entrys from file: '.$f.'<br /><br />';
$a=file(''.$f.'.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
for($i=0;$i<$n;$i++){echo $a[$i].'<br />';}
}

//total
$t=53;

//limit
$l=15;

//page
$p=0;

//all pages/files
$ap=ceil($t/$l);

//from which file to start and ends
$e=$ap-$p;
$s=$e-1;

//number of entrys in the last file
$c1=count(file(''.$ap.'.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES));
$c2=$l-$c1;

if($s>0){ea($s,$c1);}
if($s>1){ea($e,$c2);}

 

but something doesn't work right.

 

i change the $p variable manualy now just for testing.

Link to comment
Share on other sites

i have modified the code a couple of times, but i just don't get the right results out of it. :S and i'm going insane again. :S so if someone could help me out a bit i would apprechiate it. thank you. ;)

 

+ also i can't put all the entrys into one array because then it would be too big server load. :S

Link to comment
Share on other sites

mentalist tnx for the suggestion to use modulo (or %) ;) that helped... i didn't knew for what exactly % was even used, but now i know. :)

 

it's the number which stays when you / something. :D no idea how to say it in english. but i know what it is in my language. :P

 

i'm doing process ;)

Link to comment
Share on other sites

ok i think that i have explained my problem bad before, so here it is what i'm tryin to do:

 

that it would read like this:

 

// page 1 (4.txt 0-8 + 3.txt 0-7)

// page 2 (3.txt 7-15 + 2.txt 0-8)

// page 3 (2.txt 8-15 + 1.txt 0-7)

 

example: on page 1 it should read from 4.txt and start with line 0 to line 8.

Link to comment
Share on other sites

Page 1 should read from 4.txt? That makes no sense. Not to mention over complicating this by reading eight 'entries' from one file, and seven from the other. Why not just use a database for this? You'll save yourself a lot of headaches.

 

Here's an example of how you could set this up:

 

Table structure:

 

id | name

----------

1  | john

2  | bruce

3  | etc

 

Your descrpition of the 'entries' is a little vague so that's best I can suggest there.

 

The queries you'd need to select them:

 

-- page 1
select name from table_name limit 0, 15;

-- page 2
select name from table_name limit 15, 15;

-- etc.

 

And roughly the PHP you'd need to generate those queries and output the entries:

 

// check if the page number was passed
$page = !empty($_GET['page']) ? intval($_GET['page']) : 1;

// calculate the limit values
$length = 15;
$start = ($page - 1) * $length;

// build the sql
$sql = "select name from table_name limit $length, $start";

// query the database
$query = mysql_query($sql) or trigger_error('MySQL error: ' . mysql_error());

// make sure rows were returned
if (mysql_num_rows($query) > 0)
{
    // loop through each row
    while ($row = mysql_fetch_assoc($query))
    {
        echo $row['name'] . '<br />';
    }
}

 

 

 

 

Link to comment
Share on other sites

adam it's because of the way my script stores the entrys.

 

also... the first posts that are made get added into 0.txt and the next ones gets added into 1.txt and so on... and then i have to read the last ones from the last .txt ;)

 

but anyways! i managed to do it! it's done! thread closed! tnx for help! that's what i made now and it's working perfect. ;) below is the code, if someone else will need it maybe ;)

 

//total

$t=53;

 

//limit

$l=15;

 

//page

$p=1;

 

//max pages/files

$ap=ceil($t/$l);

 

//from which file to start and ends

$e=$ap-$p;

$s=$e-1;

 

$c1=count(file(''.$ap.'.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES));

$c2=$l-$c1;

 

echo 'Page: '.$p.'<br /><br />';

 

if($p==1){

echo ''.$e.'.txt (0 - '.$c1.')<br />'.$s.'.txt (0 - '.$c2.')<br />';

}else{

echo ''.$e.'.txt ('.$c2.' - '.$l.')<br />'.$s.'.txt (0 - '.$c2.')<br />';

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.