Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Mar 2003
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question PHP help for a script I'm writing (I'm a beginner)!

    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...

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Here is a handle little array function that you can use:

    http://www.php.net/manual/en/function.array-reverse.php
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    New to the CF scene
    Join Date
    Mar 2003
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    nope, other ideas?

    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?

  • #4
    New Coder
    Join Date
    Mar 2003
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP 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); 


    ?> 
    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.
    LocalHoster.com
    Your Price. Your Features. Your Hosting™

  • #5
    New to the CF scene
    Join Date
    Mar 2003
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thanks

    thanks man I figured something out now (thanks to your idea).


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •