doubleJ Posted February 14, 2011 Share Posted February 14, 2011 Hello... I have a .txt database with ~100 records. I only want to show the 15 records that are at the top (referring to reading order of the file). Here is my php code to display the records. http://www.flcbranson.org/mobile.php?content=mobile-freedownloads.php if you want to see the results of the code, below. <?php $index_file = 'services/series/Index-Date.txt'; $fd = fopen($index_file, 'r'); if ($fd) { while (!feof ($fd)) { $seriestitle = trim(fgets($fd, 1024)); // Series Title if(feof ($fd)) break; $seriessubtitle = trim(fgets($fd, 1024)); // Series subtitle $seriesinfo = trim(fgets($fd, 1024)); // Series Info (Church name) $serieslocation = trim(fgets($fd, 1024)); // City, State $seriesindex = trim(fgets($fd, 1024)); // Series index file $seriesstatus = trim(fgets($fd, 1024)); //Online Only? $divider = trim(fgets($fd, 1024)); // Divider $seriestitle2 = $seriestitle; $seriestitle3 = str_replace("'", "%27", $seriestitle); // Kind of makes the alt= below work "God's Will" ends up being "God%27s Will" but without is "God" if(strstr($seriestitle,"<")) { $seriestitle2 = strip_tags($seriestitle); } $seriestitle2 = urlencode($seriestitle2); if(file_exists("images/ProductCovers/".substr($seriesindex,0,-4).".jpg")) { echo "<img src='images/ProductCovers/".substr($seriesindex,0,-4).".jpg' width='115' height='150' border='0' alt='".substr($seriestitle3,7)."'><br />"; } } fclose ($fd); } ?> I'm assuming that "for" would do the trick, but when I tried I got 10 copies of every record. Hehehe... I'm guessing that it's a quick fix. Thanks... JJ Quote Link to comment https://forums.phpfreaks.com/topic/227679-show-top-15-database-entries/ Share on other sites More sharing options...
Psycho Posted February 15, 2011 Share Posted February 15, 2011 Change this if ($fd) { while (!feof ($fd)) { $seriestitle = trim(fgets($fd, 1024)); // Series Title To this if ($fd) { $maxCount = 10; //Set to number of records to display $count = 0; while (!feof ($fd) && $count<$maxCount) { $count++; $seriestitle = trim(fgets($fd, 1024)); // Series Title Quote Link to comment https://forums.phpfreaks.com/topic/227679-show-top-15-database-entries/#findComment-1174298 Share on other sites More sharing options...
doubleJ Posted February 15, 2011 Author Share Posted February 15, 2011 Ah... I'll test that out, tomorrow. Thanks a lot for the help. JJ Quote Link to comment https://forums.phpfreaks.com/topic/227679-show-top-15-database-entries/#findComment-1174317 Share on other sites More sharing options...
doubleJ Posted February 15, 2011 Author Share Posted February 15, 2011 That worked, perfectly. Thanks... JJ Quote Link to comment https://forums.phpfreaks.com/topic/227679-show-top-15-database-entries/#findComment-1174544 Share on other sites More sharing options...
doubleJ Posted March 24, 2011 Author Share Posted March 24, 2011 Sorry for resurrecting, but it seemed pertinent... How would I show the next 15 records on a second page, third, etc? Thanks... JJ Quote Link to comment https://forums.phpfreaks.com/topic/227679-show-top-15-database-entries/#findComment-1191840 Share on other sites More sharing options...
matthew9090 Posted March 24, 2011 Share Posted March 24, 2011 you need to use pagination http://www.phpfreaks.com/tutorial/basic-pagination it's better to use a database for it. Quote Link to comment https://forums.phpfreaks.com/topic/227679-show-top-15-database-entries/#findComment-1191843 Share on other sites More sharing options...
doubleJ Posted March 24, 2011 Author Share Posted March 24, 2011 Bah... You with your fancy databases. I'll have to look into that. Unfortunately, it will probably mean that I will have to do all the maintenance, whereas multiple people maintain their own .txt files. I was hoping that it would be something simple like... if (%i >15 && >30) echo ""; elseif (%i >30 && <45) echo ""; etc... JJ Quote Link to comment https://forums.phpfreaks.com/topic/227679-show-top-15-database-entries/#findComment-1191892 Share on other sites More sharing options...
Psycho Posted March 24, 2011 Share Posted March 24, 2011 If you want to use txt files you could very easily run into performance issue. On every page load you would have to read the entire txt file to be able to determine the number of pages and to get the content for the current page. And, you could use a database and build an interface for users to modify their own data. If I get some time this afternoon, I'll throw some code together for you to get started with what you have. Quote Link to comment https://forums.phpfreaks.com/topic/227679-show-top-15-database-entries/#findComment-1191913 Share on other sites More sharing options...
Zane Posted March 24, 2011 Share Posted March 24, 2011 On every page load you would have to read the entire txt file to be able to determine the number of pages Unless of course you stored all that data in a SESSION, which probably isn't the wisest choice, but it would do the trick. That's what SMF (the forum software used here) does... although they have databases... they store the most relevant information in a SESSION variable. Quote Link to comment https://forums.phpfreaks.com/topic/227679-show-top-15-database-entries/#findComment-1191926 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.