It is currently Sun Apr 28, 2024 2:15 am



Reply to topic  [ 6 posts ] 
another php question 
Author Message
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16650
Location: On a slope
Reply with quote
Post another php question
This is what I'd like to do... if someone could point me toward a tutorial I would be appreciative...

On my news page, I'd like to have all my individual news entries be separate text files in a directory (a directory of their own). I then want the actual news page to pull the 10 most recent text files into the page. I know this is possible with php, but I don't know where to begin to look for an answer.

Additionally, I'd like to make an old news page that pulls all the rest of the entries in (but not the first 10).

Can someone point me in the right direction?

I'd like to do this with flat text files rather than a mysql table, since the entries don't need to be searchable or anything. Plus I want to learn how to do stuff like this.

*edit* One more quick question... is it a bad idea to have a large number of php include statements? Like, including a file which then includes 3 files which in turn include several more? I'm doing a site redesign and I'm trying to make it as modular as possible. If it's a bad idea, I need to know before I get too far into it.

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Thu Oct 30, 2003 1:29 pm
Profile WWW
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
Okay, Satis. It appears to be question I can answer because I've done many similar things in CGI before. Also I now work on one Pollpage in pure PHP and I also do not know damny nothing of mysql so, we face with the same problems. (I think)
You didn't ask for making whole thing for you so, I'll be limited to giving some advices only.

If you search for some manual to learn howto this, I doubt you'll find something exact so easy. If you insist, I recommend you to do some textprocessing works in C/PHP (rather PHP directly).

This is not thing for placing on web. This is just model how it can look.


Satis wrote:
I'd like to do this with flat text files rather than a mysql table, since the entries don't need to be searchable or anything. Plus I want to learn how to do stuff like this.

Satis, I must digress a bit to say you what are your BASE options.

0. I know php-mysql chain is more elegant way to do what you want. Well:
1. You can manage php to put all texts into one single txt file and then do update/edit/observation with PHP/HTML interface.(harder to make, easier to use)
2. You can make only your (old)news page php generators and add texts by simply adding new *.txt file into some directory.(easier to make, barely harder to use)
3. (After you make something) You should think about partial showing news (10 by 10 or n by n or all by all) on history page.
4. (After you make something) You should think about making manager for searching that thing. Like google.
5. (While you make something) You should think about following abstraction: Why 10 news on the top page? You should allow $n numbers on news on the first page. Others are in history. Simple. Just set $n to 5 and you have 5 news on the top page.

In data abstraction and working comfort, you can go far away from just this.


If you open *.tpl files in your XFile directory, you'll see they are pure HTML pieces of HTML pages. If you look better, you'll see they have some markers (like "{POST_MESSAGE}") in there on keyplaces (like date, topic title, post contain etc. etc.)
So, purpose of templates are to divine forum pages to many easy-observing pieces. Marking keyplaces just make it many-purpose-thing. You can do the same:

1. Make main page tamplate. It, simplied, could looks like:
Quote:
<html>
<head>
...
</head>

<body ...>
<big>Clankiller news page</big><br/><br/>
#
</body>
</html>


2. Make news piece template. It, simplied, could look like:
Quote:
<table ...>
<tr>
<td><small>Date: #</small></td><td><small>Time: #</small></td>
</tr>
<tr>
<td colspan="2"><big>#</big></td>
</tr>
<tr>
<td colspan="2">#</td>
</tr>
</table>


Notice I used symbol "#" for keyplaces marking. It is enough long for start. Also, I didn't marked "what is what" (date, time, contain, ...). Instead, I will assume any thing must have exact order in datafile. You have many ways to mark "what is what", but I'll give freedom to your imagination rather.


3. So, then you have to manage php to use these templates to present heap of files or database in one file you make. Here, you'll have one loop in the global domain (later, it can become function). It should send main page template BUT it also should replace symbol "#" with last 10 news. That can be done with one function. Let see:

Quote:
<?php

// put all filenames here, for flexibility
$templatedir = 'tmp/';
$newsdir = 'news/';
$pagetemplate = 'main';
$newstemplate = 'news';
$templateextension = '.dat';
$newsextension = '.txt';

// including sendnews header
include "sendnews.php";

// starting
$ox = fopen( $templatedir . $pagetemplate . $templateextension , "r" );
$c = fgetc($ox);

while(!feof($ox))
{
if($c == "#")
{
sendnews(); // send last 10 news
continue; // skip "#" character
}

echo $c;
$c = fgetc($ox);
}

fclose($ox);

?>


That while is a bit ugly. I'm not sure if this equivalent will work in php (it works in C)
Quote:
...
// starting
$ox = fopen( $templatedir . $pagetemplate . $templateextension , "r" );

while(($c = fgetc($ox)),!feof($ox))
{
if($c == "#")
{
sendnews(); // send last 10 news
continue; // skip "#" character
}

echo $c;
}

fclose($ox);
...



4. So, when we talks about sendnews(), that it more complicated thing.

First, there you have to open dbase and find 10 last entires (easy if all files are in one file) OR to find last 10 made files. You have much ways to do both of these things:
Quote:
(1) You can put each news in database between tags "`" and "`", or in one row or between two boundaries like "-------asdfvafe-vwa--------", ... . When you add news, you simple add new thing to the top of file and when snow news, you interpret top10. (when you show history, you show from 11-20,21-30, ...).

If you do this, have on your mind you must put date, time and title somewhere in news, maybe in some tag or between "`" and "`" or similar.

Quote:
(2) You can make one directory with very many files. One file is one news. Simple. I really do not know how to read date whes file is made or to get list of files in directory thru php without VERY slowness so, I'll leave that things to Pig if he will. If not, I'll search thru manual soon.

But, I have one solution for this without reading date of filemake or listing files of directory. You can name files like 1.txt, 2.txt, 3.txt, etc.

1.txt is oldest, 2.txt is newer, etc.

Then, you also have two ways:
1. When someone request page creating. Your PHP will search if exist 1.txt, 2.txt, ... and find last *.txt. Then they will send you (for ex.) 53.txt, ..., 44.txt.
2. You can make one file for keeping number of news. And then read from that file how last news is named. Don't forget to update it after any adding.


Second: So, you'll also have 1 template and 10 news. You have two-three-four ways.
Quote:
1. First is to open template file 10 times for each news file.
2. Second is to open template file, use it for one news and then seek start of file (I really don't know how it works in C/PHP) and then use it again etc.
3. Third is to open template file, load it into string and then use that string how many times you wish.


One example for sendnews.php, solution with many *.txt files wrote:
<?php
//-------------------------------

// ... some code to set number of news to last news (global procedure)
// for ex. (loading it from some file)

$nonewsfile = 'no.txt';

$ax = fopen($templatedir . $nonewsfile, "r");
$numberofnews = 0 + fread($ax, filesize ($templatedir . $nonewsfile));
fclose($ax);

//-------------------------------

function sendnews()
{
for($i=0;$i<10;$i++)
{
// template
$ox = fopen( $GLOBALS['templatedir'] . $GLOBALS['newstemplate'] . $GLOBALS['templateextension'], "r");

//~~ you can add:
//~~ if($GLOBALS['numberofnews']-$i > 0) { // to prevent bad things when you have less than 10 news.

// function sends next file
$ix = fopen( $GLOBALS['newsdir'] . ($GLOBALS['numberofnews']-$i) . $GLOBALS['newsextension'], "r"); // notice that $i here allways show number of already flushed news

$c = fgetc($ox); // again, test if up described while works
while(!feof($ox))
{
if($c == "#")
{
while(!feof($ix) && ($c1 = fgetc($ix))!="`") // flush one data piece (date, time, ...)
{
echo $c1;
}

continue; // skip mark char "#"
}
echo $c;
$c = fgetc($c);
}

fclose($ix);
//~~ }

fclose($ox);
}
}
?>



Third: With recent algorithm, one news file (1.txt for ex.) should look:

Quote:
October, 10. 2003.`11:45am (gmt+5)`Ye olde inn`This is text about Ye olde inn ...
`October, 10. 0003.`00:00am (gmt-2)`Something went wrong`If someone care for english now ...

...

`January, 24. 1983.`16:00am (gmt-1)`Happy birthday`GFreeman's birthday is today.


You should think about overall standardization first.


That's all for start.
You can go mooore deeper into abstraction and comfort doings. When you finish it, moderating your site should be easy as in this forums. (either you manage database by yourself or let that to mysql)
As for the including, I think you should feel free to make how much levels (or line includes) you need.

Let me know if I can help you more.

_________________
++


Sat Nov 01, 2003 2:34 am
Profile WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16650
Location: On a slope
Reply with quote
Post 
thanks gfreeman, I wasn't expecting help from you. :) Indeed, what you laid out were basically my options. The C is gibberish to me, but hey, that's fine.

In fact, I just solved my problem yesterday. I didn't have a chance to post about it, but I got it working.

What I did was make a directory /news with a bunch of text files. Each file is named [date].txt, like 20030823.txt, that way the date is part of the filename and I can easily sort it. The code is here:

Code:
<?php
//Grabbing the list of files in $dir, as well as the number of files ($filecount).
$dir=("news");
$handle = opendir("$dir");
  while (false !== ($file = readdir($handle))) {
    if(is_file($dir."//".$file)) {
      $filelist[] = $file;
      $filecount = count($filelist);
    }
  }

//Sorting the files into reverse numerical order.
rsort($filelist,SORT_NUMERIC);


//Including only the 10 most recent (highest numbered) files.
$i = 0;
while ($i < 10) {
  include ("$dir/$filelist[$i]");
  $i++;
}
?>

I'm not quite done yet, obviously.  What I'm also doing is defining a variable for 'new news' or 'old news'.  This is done in the container that holds news.txt.  Anyway, once I get that sorted out, there will be an if/else statement that will either include the first 10 (already written about) or everything except the first 10.  That way both new and old news will be accessible from the same page.  :)

Anyway, thanks for the help.  If I have any more questions, I'll definetely ask.  But I think I'm doing ok for right now.

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Sat Nov 01, 2003 2:12 pm
Profile WWW
Duke
User avatar

Joined: Mon Mar 31, 2003 8:59 am
Posts: 1358
Location: right behind you
Reply with quote
Post 
Don't do .txt files. Don't be a pussy. This is a very easy project to start learning MySQL. Plus, there are tons of things you can do with a database other than search it for a text string.

1. If you store your news in a database then you all you have to do is store the date it was entered as one of the fields in the entries row. Then you can just selectively get the 10 most recent news post, or whatever.

2. There would be no manual movement of files. Archiving, etc would be automatic.

3. files are much harder to work with properly than a database.

4. A database would be much more effecient. Why use programming if you are going to castrate the concept. That's like buying car so you can store your gas in it. If you want to store your gas, get a gas can. If you are using a car, then fucking drive it.

This is a great tutorial to get started with PHP/MySQL:
http://www.sitepoint.com/article/228

So that you have a framework of what you need to do, I will diagram the process below. When you hit a question, ask. BTW, I'm not going to help you do this with txt files. That's just a waste of both of our time.

Use phpmyadmin or other db utility to create a table in your database. The table should have a prefix to distinguish it from any other projects in the same database (such as you found with phpbb). I would use ck_, but it is your choice. You will need a field for the text, id, and date. The table might look like this:
ck_news
=========
ck_news_id (int)
ck_news_post (text)
ck_news_date (varchar)

everything should be obvious, but make sure that when you are populating the ck_news_date field, that you use a unix timestamp
Put a bunch of crap in there to test it as well.

When you build the page that you want to have the news, it would be easiest to just create a news.php file and include it on your main page, rather than do the programming on the main page itself (btw, includes, and mulitple includes are no problem - include 100's if you want).

On news.php, connect to the database (see the tutorial above). Then you need to create a simple query like:
$result = mysql_query("SELECT * FROM ck_news ORDER BY ck_news_date DESC LIMIT 0,10");
maybe that should be 1,10... don't recall off the top of my head. You'll figure it out.

The result is nothing more than a mathematical place holder of the results found in your query. There is no data directly accessible from it. You need to create a loop to manually extract the rows from it.
Then you need to get the data from the query:
while( $row = mysql_fetch_array($result) )
{
echo("posted on: " . date("Y.m.d H:i", $row[ck_news_date]) . "<br><br>$row[post]");
}

Boom! done. If you want to see all of them, just don't specify a limit.

I gotta go, ask more questions as they come, or post if I wasn't clear. If you find me online (IM'ing) then I'll explain in excruciating detail what you are actually doing).


Sat Nov 01, 2003 2:32 pm
Profile YIM WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16650
Location: On a slope
Reply with quote
Post 
Thanks Pig. I completely understand what you're talking about... I got a lengthy exhortation on the advantages of MySQL from a PHP friend I have.

In fact, I plan on eventually doing it as a MySQL table. My main reason for going with flat text files was just to learn how it would be done and get a better handle on staight PHP. Once I have all that working, I'll probably just rewrite it all to use MySQL instead.

In fact, I'm basically done with the flat text thing. The only thing I'm working on right now is setting a variable that displays either the first 10 posts (default) or all but the first 10 (when you click the 'older news' button).

When I get to the point of sticking it into MySQL, I'll probably write a .htaccess protected form to automate inserting new news entries.

Oh, and for the record, my news page is index2.html, which is basically

some header stuff like title
include header
include news.txt
include footer

news.txt is where all the actual processing goes on. :) I'm making this sucker so modular I can do anything with it without having to rebuild it from scratch. At least, that's the plan. I'll let you know if I have any more questions.

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Sat Nov 01, 2003 7:28 pm
Profile WWW
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
Pig wrote:
A database would be much more effecient. Why use programming if you are going to castrate the concept. That's like buying car so you can store your gas in it. If you want to store your gas, get a gas can. If you are using a car, then fucking drive it.


Disagree, barely. I gonna to tell what you mean thru my scope.

A database would be much more effecient on WEB and similar envs. Why use programming if you are going to castrate the concept? That's like buying airplane so you can drive it on the street. If you want to drive airplane, go to the airfield. If you gonna drive on the street, then fuckin' buy a car.

_________________
++


Mon Nov 03, 2003 2:46 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 6 posts ] 

Who is online

Users browsing this forum: No registered users and 68 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware.