...

View Full Version : PHP help for a script I'm writing (I'm a beginner)!



jodan
03-28-2003, 06:46 PM
If a coder can help in any way I would appreciate it. Anyway to the point:

I am trying to write a PHP news script, that functions much like a forum would (as in saving news posts as arrays in files named as ID#'s, etc.). I can call each of the file's contents to print flawlessly in a separate .php file; however, the order it prints the files is "1.php", "2.php", "3.php", and on up (printing the news items in order of oldest post to the most recent post (printed last). I cannot for the life of me reverse the order of printing. I need to print the larger # (ex. 3.php) PRIOR to printing 2.php and so on...

My funtions.php contains the following code:



function getdata($name){

$dataarray=@file($name);
for($n=1;$n<count($dataarray);$n++){$dataarray2[$n-1]=trim($dataarray[$n]);}
return $dataarray2;
}

function show_news()
{

if ($dir = @opendir("posts")) {
$n = 1;
while (($file = readdir($dir)) !== false) {
if ($file == "." || $file == "..") continue;

$postsarray = getdata("posts/$n.php");

print "Author: $postsarray[1]<br>";
print "Subject: $postsarray[2]<br>";
print "Body: $postsarray[3]<br><br>";

$n++;

}
}
@closedir(posts);
}

?>

The function show_news() is called from news.php to (obviously) show the news.
"posts" is the directory that the news files (1.php, 2.php, etc.) are stored.


If I need to further explain anything please let me know, I will do so happilly. Thanks in advance for any help...:thumbsup:

Spookster
03-28-2003, 07:22 PM
Here is a handle little array function that you can use:

http://www.php.net/manual/en/function.array-reverse.php

jodan
03-28-2003, 09:37 PM
I appreciate the assistance; however, its still not working correctly...
Just so you know, I did look through php.net and even tried the function you listed prior to posting here (I did so only because I could not figure out why they weren't working correctly myself or with the help at PHP.net).

The problem is this: when I use $postsarray = array_reverse(getdata("posts/$n.php" )); it reverses the array (containing the message info) inside of each file ($n), it doesnt actually show the files themselves in reverse order.

Using $postsarray = array_reverse(getdata("posts/$n.php" )); it prints, for example:
______________________________________

1.php
Author: $body
Subject: $subject
Body: $author

2.php
Author: $body
Subject: $subject
Body: $author
______________________________________

If I just use: $postsarray = getdata("posts/$n.php" ); it prints the data correctly but still the files in the wrong order, for example:
______________________________________

1.php
Author: $author
Subject: $subject
Body: $body

2.php
Author: $author
Subject: $subject
Body: $body
______________________________________

Any other ideas you may have?

Phool
03-29-2003, 12:09 AM
function getdata($name){

$dataarray=@file($name);
for($n=1;$n<count($dataarray);$n++){$dataarray2[$n-1]=trim($dataarray[$n]);}
return $dataarray2;
}

function show_news()
{

if ($dir = @opendir("posts")) {
$n = 1;
while (($file = readdir($dir)) !== false) {
if ($file == "." || $file == "..") continue;

$postsarray = getdata("posts/$n.php");

print "Author: $postsarray[1]<br>";
print "Subject: $postsarray[2]<br>";
print "Body: $postsarray[3]<br><br>";

$n++;

}
}
@closedir(posts);
}

?>

from what i can see you want 2.php to display before 1.php and so on?

You need to add a line that will count the amount of files in the dir BEFORE you begin opening them. Then you could just say open and work backwards?

$n needs to be the amount of files you want to open. Then you just $n = $n - 1; instead of $n++ at the end of the loop.

jodan
03-29-2003, 01:30 AM
thanks man I figured something out now (thanks to your idea).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum