tastro Posted October 24, 2010 Share Posted October 24, 2010 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 and i know that i will think how stupid i'm after i will see how it should be done. :S Quote Link to comment https://forums.phpfreaks.com/topic/216726-problem-with-advanced-pagination/ Share on other sites More sharing options...
Adam Posted October 24, 2010 Share Posted October 24, 2010 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.. Quote Link to comment https://forums.phpfreaks.com/topic/216726-problem-with-advanced-pagination/#findComment-1125944 Share on other sites More sharing options...
tastro Posted October 24, 2010 Author Share Posted October 24, 2010 files are named with numbers: 1.txt 2.txt 3.txt and in the file with the highest number are the last added names. data is stored in lines, also in every line is 1 name, example: john bruce jessica ... Quote Link to comment https://forums.phpfreaks.com/topic/216726-problem-with-advanced-pagination/#findComment-1125947 Share on other sites More sharing options...
mentalist Posted October 24, 2010 Share Posted October 24, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/216726-problem-with-advanced-pagination/#findComment-1125948 Share on other sites More sharing options...
tastro Posted October 24, 2010 Author Share Posted October 24, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/216726-problem-with-advanced-pagination/#findComment-1125954 Share on other sites More sharing options...
tastro Posted October 24, 2010 Author Share Posted October 24, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/216726-problem-with-advanced-pagination/#findComment-1125967 Share on other sites More sharing options...
tastro Posted October 25, 2010 Author Share Posted October 25, 2010 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. no idea how to say it in english. but i know what it is in my language. i'm doing process Quote Link to comment https://forums.phpfreaks.com/topic/216726-problem-with-advanced-pagination/#findComment-1126202 Share on other sites More sharing options...
mentalist Posted October 25, 2010 Share Posted October 25, 2010 it's the number which stays when you / something. no idea how to say it in english. Remainder Quote Link to comment https://forums.phpfreaks.com/topic/216726-problem-with-advanced-pagination/#findComment-1126226 Share on other sites More sharing options...
tastro Posted October 26, 2010 Author Share Posted October 26, 2010 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- // 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. Quote Link to comment https://forums.phpfreaks.com/topic/216726-problem-with-advanced-pagination/#findComment-1126570 Share on other sites More sharing options...
tastro Posted October 26, 2010 Author Share Posted October 26, 2010 i mean: // page 1 (4.txt 0-8 + 3.txt 0-7) // page 2 (3.txt 8-15 + 2.txt 0-7) // page 3 (2.txt 8-15 + 1.txt 0-7) Quote Link to comment https://forums.phpfreaks.com/topic/216726-problem-with-advanced-pagination/#findComment-1126573 Share on other sites More sharing options...
Adam Posted October 26, 2010 Share Posted October 26, 2010 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 />'; } } Quote Link to comment https://forums.phpfreaks.com/topic/216726-problem-with-advanced-pagination/#findComment-1126575 Share on other sites More sharing options...
tastro Posted October 26, 2010 Author Share Posted October 26, 2010 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 />'; } Quote Link to comment https://forums.phpfreaks.com/topic/216726-problem-with-advanced-pagination/#findComment-1126580 Share on other sites More sharing options...
mentalist Posted October 26, 2010 Share Posted October 26, 2010 FYI This type of file based database is known as a 'Flatfile Database' Quote Link to comment https://forums.phpfreaks.com/topic/216726-problem-with-advanced-pagination/#findComment-1126592 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.