Jump to content
xisto Community
Corey

Php News Script how to make news script that uses MySQL writen in PHP

Recommended Posts

How to make a News script with PHP + MySQL



So..in this tutorial i will explain how to create PHP news script.

first we have to create the table in My SQL..

with the code below you will do this ( you have to enter it into SQL field ..in PHPmyADMIN)

CREATE TABLE news ( id int(10) unsigned NOT NULL auto_increment, postdate timestamp(14) NOT NULL, title varchar(50) NOT NULL default '', content text NOT NULL, PRIMARY KEY (id), KEY postdate (postdate), FULLTEXT KEY content (content) ) 




>>>> script files

1 file will show the news ,1 file will contain variables and with the third one we can add news.

we'll call the first file "news.php"

<?php$query = "SELECT *," ."DATE_FORMAT(postdate, '%Y-%m-%d') as date " ."FROM news ORDER BY id DESC LIMIT $news_limit"; // 1.$result = mysql_query($query);while($r=mysql_fetch_array($result)) // 2.{echo "<br><table width='100%'><tr bgcolor='$title_cell_color'><td><img src='$bullet'><b>$title</b> posted on $date</td></tr><tr bgcolor='$news_cell_color'><td>$content</td></tr></table><br>";}?>

explaination:

1. inlcuding variables into the script

2. selecting the table in the db,formating date, limiting the number of news the script is going to show.

3. now the script is writing the data form db into tables.



now we have to make another file which can add new news into db.

we will call it "add.php"

<?phpinclude "var.php"; // 1.if ($action==add) // 2.{$title=$_POST['title'];$content=$_POST['content']; // 3.mysql_query("insert into news (title,content) VALUES ('$title','$content')");echo "<a href='index.php'>Home</a>"; // 4.}else // 4.{print "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td><form name=\"form1\" method=\"post\" action=\"add.php?action=add\"><div align=\"center\"><p>Title:<input type=\"text\" name=\"textfield\"></p><p>News content :<textarea name=\"title\" cols=\"50\" rows=\"10\" id=\"title\"></textarea></p><p><input type=\"submit\" name=\"Submit\" value=\"Add\"></p></div></form></td></tr></table>\n";}?>

explaination:

1. includeing variables into the script

2. if the $action id add..then the script is going to add data into the db,and if $action isn't selected,then the scipt is gonna show form for adding news.

3. the script is putting the data that we have entered into variables

4. if $action isn't set to "add" the script is showing a form for adding news



this is the third file....we are gonna define all important variables here..and we're gonna make a connection to db.

<?php////////////////////////////////////////////////////////////////////////////////////////$news_limit="xxxx"; // number of news that script shows$user="xxxx"; // db username$pass="xxxx"; // password$db_name="xxxx"; // database name$bullet="xxxx"; // path to the bullet image$title_cell_color="xxxx"; // bg color for title cell in RGB...(black = #000000)$news_cell_color="xxx"; // bg color for news cell in RGB...(black = #000000)////////////////////////////////////////////////////////////////////////////////////////$db = mysql_connect("localhost", "$user", "$pass");mysql_select_db("$db_name", $db); //?>

You have to change all the variables.



You can download all script files here.

http://forums.xisto.com/no_longer_exists/



..if you have some questions are if you have found a mistake in this script...please let me know. (post here)

Share this post


Link to post
Share on other sites

Great script Corey! Two questions: 1) Can I use some change of this script for a newsletter? I mean, is there a way to send the news genterated and stored in the database? 2) Is there a way to generate some reports (search reports) crossing more than one field, like "some words in the subject", and "sent before X day"? Thanks a lot, I'm a newbie in php! daniel (from Brasil)

Share this post


Link to post
Share on other sites

Can you put the download file in zip instead of rar? I dont wanna download winrar for that....

142427[/snapback]


no problem...

here..

http://forums.xisto.com/no_longer_exists/

 

 

Great script Corey!

  Two questions:

 

        1) Can I use some change of this script for a newsletter? I mean, is there a way to send the news genterated and stored in the database?

 

        2) Is there a way to generate some reports (search reports) crossing more than one field, like "some words in the subject", and "sent before X day"?

 

      Thanks a lot, I'm a newbie in php!

 

          daniel (from Brasil)

142450[/snapback]


1) yes...but afkors..you have to make some changes ...u have to make a script that pulls the data from db and sends it to users via e-mail

 

2) ....maybe...don't know it yet..

Share this post


Link to post
Share on other sites

On the subject of dtygel's suggestions:1. Could this be done using the PHP mail() function?2. A search could be done by asking the user for a search term in a text field and asking what to search in e.g Date, Title etc and also placing that in a variable. Then selecting the appropriate results from the SQL database.The search could also help with the email, to ensure that only the current months news is mailed.Just a thought :rolleyes:

Share this post


Link to post
Share on other sites

Hi all, rvalkass and corey showed a way, but I'm a terrible programmer (sorry...), so I would prefer to find some solution. My problem is the following: I'd like to send "automagically" the latest news in my website (which is still under construction :rolleyes: ); and also, I would like to let the user make some searchs on the news sent in the past. I took a look at karlo's site, but didn't find the news script he mentioned. Now, about using the mail() function: wouldn't I be limited to the hourly limit in the server of e-mails sent? Or is the mail() function out of this account? Would it be better to use, say, the phplist software? Thanks folks: I'm understanding a little bit more about php since I came accross this topic :lol: Daniel

Share this post


Link to post
Share on other sites

As the mail() function sends emails from an address you specify I belive you would be limited to the servers limits. I belive that PHPList also uses the mail function so would have the same problems. I think a mySQL query soemthing like the one below would allow for a search funtion:

mysql_query("SELECT title, content FROM news WHERE title='$search' ");

This would get all news entries where the title is equal to the search term. The variable $search would be a text box containing the search term. I may have got this code wrong, so please correct me. I am also unsure of whether the term has to exactly match that in the database, or just contain it. Everyone, please feel free to correct me or change this code. Thanks.

Share this post


Link to post
Share on other sites

Great, Valkass! Thank you for the answers. Well, how do I get rid of the servers limits? Can mailman do that? Or should I contract a mailing service? (some suggestion?) About the script: that was a suggestion of a simple search. I would like to give the user the possibility to cross between fields (maybe the AND syntax...): a cross-fields search. Daniel

Share this post


Link to post
Share on other sites

Hey there. Since this is a news script, does it display text in a set format? Like if you've entered a text with lots of paragraphs in the database, does it still display that format once you've "QUERIED" it? With spaces between each paragraph, I mean. :rolleyes:

Share this post


Link to post
Share on other sites

Well, how do I get rid of the servers limits? Can mailman do that? Or should I contract a mailing service? (some suggestion?)

 

About the script: that was a suggestion of a simple search. I would like to give the user the possibility to cross between fields (maybe the AND syntax...): a cross-fields search.

 

    Daniel

146529[/snapback]


The mail limit sets a number of emails that can be sent per time period, usually something like one every 30 seconds. I think it would be possible to send one email to lots of addresses, therefore it would only appear as one email to the server. If the addresses are listed as one BCC (Blind Carbon Copy) the same email will be sent to a set of addresses without them each knowing eachothers address.

 

<?php//Your email address$serveremail = 'YourEmail@server.com';//List of recipients$bcc = 'address1@email.org' . ', ';$bcc .= 'address2@email.net' . ', ';$bcc .= 'address3@email.co.uk'; //The last address does not need a comma$subject = 'Update';$message = '//The message goes here//This would be the script to call the latest news';//List of headers so it can be read by many mail systems$headers = 'MIME-Version: 1.0' . "\r\n";$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";// Headers containing email addresses$headers .= 'To: PutYourEmailAddressHere' "\r\n"; //This will send a copy to yourself so you know it has been sent$headers .= 'From: PutYourEmailAddressHereAgain' . "\r\n";$headers .= 'Bcc:' . $bcc . "\r\n";mail($serveremail, $subject, $message, $headers);?>

Again, I haven't done any PHP coding for a while, so feel free to correct it and give any comments. I also don't have hosting at the moment :rolleyes: so I can't test it.

 

The search could use if statements according to check boxes being selected or not to search certain fields, but I don't know how to get the AND, OR etc operators to work. Sorry.

Share this post


Link to post
Share on other sites

dtygl: Yes it is possible to do this you just must have more than one WHERE clause.

$query = "SELECT * FROM `news` WHERE `timestamp`<='$searchDate' AND `text` LIKE '$searchString'";

Share this post


Link to post
Share on other sites

Hey there.  Since this is a news script, does it display text in a set format?  Like if you've entered a text with lots of paragraphs in the database, does it still display that format once you've "QUERIED" it?  With spaces between each paragraph, I mean.  :rolleyes:

146530[/snapback]


yes afkors;)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.