Forum Moderators: coopster

Message Too Old, No Replies

possibility to dynamically load specified amount of files?

         

jrs77

1:09 am on Apr 25, 2010 (gmt 0)

10+ Year Member



Hello all,

I'm making a small news-site at the moment and i don't really want to install a sql-database for it.

What I'm trying to do is to have a php-function reading an directory with txt.files and have a specified amount of them given out as text.
So I can just write a new textfile for every news-item and get 10 news shown on a page and then the next 10 news on the next page etc.

Is this even possible, or do I have to write for every 10 entries a new php-page with the textfiles in hardcoded into an array?

Tommybs

8:47 am on Apr 26, 2010 (gmt 0)

10+ Year Member



It should be possible, do you have a standard naming convention for the files in question?

jrs77

10:54 am on Apr 26, 2010 (gmt 0)

10+ Year Member



Yeah sure, I'd go with 00001.php - 99999.php as a naming convention.

To include all files, there's this:

<?php
foreach (glob("dir/*.php") as $filename)
{
include $filename;
}
?>


That's not very hard to do actually, but how do I limit it to 10 files and then have a dynamic menu that allows me to show the next 10 etc..

Tommybs

7:49 pm on Apr 27, 2010 (gmt 0)

10+ Year Member



Your best bet is to use a querystring that has a page number or an article start number. Then search for that file and increment it by 1 each time to get back the ten files you want.

$p = (isset($_GET['p']) && ctype_digit($_GET['p'])? $_GET['p']: 0;
$i = ($p *10) + 1;
$i = count($files - 1);
for($j = 0; $j<10; j++){
if(file_exists("path/to/files/".$i.php)){
include("path/to/files/".$i.php);
$i++;
}
}

Now the above code will start at 1.php If you want to display the latest first you will need to reverse the logic. However I suggest you look into the scandir function. This will return you an array of all the files in the specified directory. (Make sure you specify the directory for security reasons!). You can then get the size of the array and loop backwards from a specified position.

jrs77

6:04 pm on Apr 29, 2010 (gmt 0)

10+ Year Member



OK, I gave in... I looked around to learn about mysql-databeses and wrote a little mysql-database now for the news-section.

So now my question is, how do I limit the string to 10 entries.

My code so far is this and shows all entries ofc.

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("database", $con);

$query="SELECT * FROM news";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {

$datum=mysql_result($result,$i,"newsDatum");
$titel=mysql_result($result,$i,"newsTitel");
$text=mysql_result($result,$i,"newsText");

//Datumsänderung
$datum_array=explode("-",$datum);
$datum=$datum_array[2].".".$datum_array[1].".".$datum_array[0];

echo "<topic2>$datum | $titel</topic2><p>$text</p><div class='line'></div>";

$i++;
}

?>


I want to make a simple menu at the bottom like on google basically.
The database has a unique ID for every entry, so possibly I can make the menu on the bottom based on the ID (e.g. count the number of entries) and... well, i need to search around alot more I guess :p

jatar_k

6:14 pm on Apr 29, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you can use LIMIT
[dev.mysql.com...]

$query="SELECT * FROM news limit 10";

jrs77

9:48 am on May 1, 2010 (gmt 0)

10+ Year Member



Thanks for the information.

I've done it with...

if ($i == 10) break;


for now.

Readie

10:52 am on May 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you use the LIMIT SQL clause you'll actually be making your script more efficient as it will be pulling less records into memory.

jrs77

11:13 am on May 1, 2010 (gmt 0)

10+ Year Member



Oh, I didn't know that... still learning.

Thanks mate.