PHP Question

I am currently working on a news posting script for my team’s site using PHP. I’ve got everything down for posting and adding news, but I need a way to have only the most recent posts added. I tried to create a while loop so that only the 5 most recent posts show on the index page, but it didn’t work. If anyone can help me figure it out, help would be greatly appreciated. And i simplified the part of the code that displays the post.


<?
$data = file('news.txt');
$data = array_reverse($data);
foreach($data as $element) {
    $element = trim($element);
    $pieces = explode("|", $element);
    echo $pieces[0] . $pieces[1] . "
";
    echo "<hr>
";
}
?>

I’m not sure exactly how your code was intended to work, but it appears that you check each line of news.tct for “|” which are used to divide that line into two parts ($pieces[0] and [1]) that are then just outputted concatenatedly so the output is just the text without the pipes. I think maybe you intended “|” to seperate different entires but you didn’t realize that file() reads each line into a new element in the array.

If at all possible, would you be able to use a database like MySQL?

If not, then why not just write the newest news information at the top of the file? I believe there’s a way to set the file pointer to the beginning of the file before you write.

(I can’t supply code for doing the file stuff…I’ve had issues with doing that in the past)

Sorry that I didn’t elaborate on that, the file news.txt which contains all of the news looks like this:

Date|Poster’s Name|News Title|News Story

And yes, the | is used to separate each section.

I do have access to three MYSQL databases, but I haven’t yet learned how to interface with MYSQL yet. Also, I use the array_reverse() function to read the newest news from the top of the file, I just need some way where I can just get the newest five stories to display.

Hm…maybe seperate each item by underscores or something, and scan the file for it, and each time it’s found, fill an array w/ the info and also use a counter. An example:


// number of news items read
$num_news = 0;
 
// global array defined here
 
// open file for reading
$file = fopen("news.txt", "r");
 
// read file
$stuff = fread($file);
 
// loop through the reading
while(($num_news < 5) && ($stuff != eof())){
/* store news items in an array...I'm not sure on howto do this, so... */
}

Thanks, I’ll try that, really the while(($num_news < 5) && ($stuff != eof())) is what I was looking for, just not really sure where to put it in the code.

EDIT: I’ve got it, rudimentary as it is:


<?
$counter = 0;
$data = file('news.txt');
$data = array_reverse($data);
foreach($data as $element)
{
if($counter < 5)
  {
      $element = trim($element);
      $pieces = explode("|", $element);
      echo $pieces[0] . $pieces[1] . $pieces[2] . $pieces[3];
      echo "<hr>
";
  }
else
  {
  }
$counter = $counter + 1;
}
?>

Now I have a new question. I have set up a user authentication page, which asks for a username and password. If the correct username and password are given, I want the browser to redirect to a certain page. Hwoever, I don’t know the PHP function (if there is one) that will redirect to a new page. I’ve searched around but I can’t find anything.

Here’s some code to do it:


// ** BELOW CODE ABOVE THE <HTML> TAG!! **
$log = $_POST'login'];
 
$user = $_POST'username'];
$pass = $_POST'password'];
 
if(($log) && ($user == /* stored username */) && ($pass == /* stored password */)){
  header('Location: http://the.address.to.the.redirection.com');
} else{
  // login script
}

log = _POST’login’]; - this is storing the value of “login” in a variable “$login”. login is the name of the submit button of the form.

user = _POST’username’]; - stores the entered username (in the textbox) into a variable. username is the name of the textbox.

pass = _POST’password’]; - same as above, but subtitute the obvious.

The reason for having the if(…) case above the <HTML> tag is because since <HTML> is a header, you can’t send a header after a another header (it’s hard for me to explain, so just look up the “header()” function in the PHP doc.).

Your form should look something like this (assuming the form is located on a page called: “login_admin.php”):

<form action=“login_admin.php” method=“POST”>
<input type=text name=username><br>
<input type=password name=password><br>
<input type=submit name=login value=“Login”>
</form>

Thanks Raven_Writer, for all your help. :smiley:

No problemo.

I’ve been doing this kinda thing so many times, I’ve got it all memorized. lol.