Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Website Design/Showcase (http://www.chiefdelphi.com/forums/forumdisplay.php?f=64)
-   -   PHP Question (http://www.chiefdelphi.com/forums/showthread.php?t=29027)

Joshua May 10-06-2004 18:32

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.

PHP Code:

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


Max Lobovsky 10-06-2004 19:07

Re: PHP Question
 
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.

Raven_Writer 10-06-2004 19:52

Re: PHP Question
 
Quote:

Originally Posted by HHSJosh
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.

...

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)

Joshua May 11-06-2004 15:42

Re: PHP Question
 
Quote:

Originally Posted by maxoblovsky
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.

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.

Quote:

Originally Posted by Raven_Writer
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)

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.

Raven_Writer 11-06-2004 15:49

Re: PHP Question
 
Quote:

Originally Posted by HHSJosh
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:
PHP Code:

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



Joshua May 11-06-2004 16:25

Re: PHP Question
 
Quote:

Originally Posted by Raven_Writer
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:

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:
PHP Code:

<?
$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>\n";
  }
else
  {
  }
$counter $counter 1;
}
?>


Joshua May 11-06-2004 20:37

Re: PHP Question
 
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.

Raven_Writer 11-06-2004 20:46

Re: PHP Question
 
Quote:

Originally Posted by HHSJosh
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:

PHP Code:

// ** 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>

Joshua May 11-06-2004 20:49

Re: PHP Question
 
Thanks Raven_Writer, for all your help. :D

Raven_Writer 11-06-2004 20:50

Re: PHP Question
 
Quote:

Originally Posted by HHSJosh
Thanks Raven_Writer, for all your help. :D

No problemo.

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


All times are GMT -5. The time now is 15:24.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi