PHP news feed script. Your own news feed in PHP


I Googled it for you. I have been looking for a long time for a plugin that will allow me to quickly and easily create such a simple effect, without gigantic amounts of code and cunning intricacies. And I found it and I'm very glad about it. In fact, I’m also writing this post for myself, as a bookmark so that I can look here the next time I need to make an image gallery with autoscrolling.

Writing html for the image feed

There's a classic here. We use a ul list and wrap it in a div. We will set a plugin on this div, so it needs an identifier. In my case it's a class.

It is important that the images are the same size. Otherwise it will not look so impressive. Therefore, you will have to strain and process them.

Preparing JavaScript for the Image Carousel

You need to connect jQuery. I think everyone knows how to do this. You also need to include the jcarousel Lite library. She does everything.

After loading the page, you need to set the plugin to our image feed and a miracle will happen!

$(function() ( $(".mycarousel").jCarouselLite(( auto: 2000, speed: 1000, circular: true, visible: 4 )); ));

This is all! Really. Nothing more is needed. No need to connect or configure themes. There is no need to make any controls or write additional code. You have standard code list of images and a single call to the plugin with parameters. This is how it should be. I'm delighted, to be honest.

You can read about the possible parameters and their values ​​in the original on the plugin page. Well, I’ll briefly tell you why.

Possible gallery options

btnPrev - identifier defining the "Back" button. This is necessary if you are doing scrolling control using buttons.

btnNext - identifier for the "Forward" button.

btnGo - you can navigate by element numbers. When you click on an element, the carousel should scroll to a specific item. Read more in the documentation.

mouseWheel - true/false value that determines whether the carousel will scroll when the mouse is scrolled. Requires mousewheel plugin.

auto - true/false value that determines whether the tape will spin by itself.

speed - scrolling speed in milliseconds.

easing - scrolling effect. You can create a bouncing effect. Requires the easing plugin.

vertical - maybe you need the pictures to rotate vertically?

circular - will the tape stop when it reaches the last picture or will it spin in a circle?

visible - how many elements are visible

start - which element to start with

scroll - how many elements to scroll through

beforeStart - callBack function called before the animation starts

afterEnd - callBack function called at the end of the animation

What do all information sites have in common? What is required on the main page? If you were thinking about the news, you were right! Today we will figure out exactly how to make your own news feed with an admin panel and protection in PHP. I’ll say in advance that this is not a difficult task at all, but it won’t take two minutes either, so stock up on everything you need (beer, cola, juice, chips...) and go! First, let's think about the structure of the news feed. Of course, we will use mysql, but what would we do without it! The data will be stored in a mysql database, which we will call mynews. Which? I'll explain now. Let's place a table called news in the mynews database. And in the table - this is what:

News_id - MediumInt length 9 AutoIncrement, this will be the primary key.
Ntext - news text, type Text.
Ntitle - news title, type varchar, length 255
Ndate is a date, varchar, length 255
Nuser - and this is the name of the author of the news, varchar, length 255.

How to create all this? Read the previous texts on this site, everything is there necessary information. And from myself I can recommend an excellent, very convenient program Mysql-Front, in which all tables are created visually. But let's move on. Now let's figure out what scripts need to be written. I suggest writing the following: admin.php, createnews.php (this script will create the news), tnews.php (will display the news). You could write a script to delete news, but you can do this yourself after completing the exercise. So, let's begin. First, let's write the createnews.php script. It won't be very clear for now, but everything will fall into place after you look at the admin.php listing.

Listing 1. Createnews.php

"". addslashes($ntitle)."" // This line is needed to ensure that the quotes are displayed correctly in the title.

As you can see, varchar is used instead of a mysql date, it’s easier. Now, in order for everything in the considered script to fall into place, you need to write an admin panel. Let's write.

Listing 2. Admin.php

Adding news Adding news News title:
Your name:
Text

Obviously used simple html form. So why create a PHP script? Because we didn’t think about safety at all. The security of the admin panel can be ensured by denying access to random users, not those who know the login and password. An attacker will be able to find out the password only then (and not earlier) when he gains access to the script, i.e. to the directory. Change admin.php as follows:

Listing 3. Modified admin.php

Adding news Adding news News title:
Your name:
Text

Let's take a look at the written script. At the very beginning, the current login and password are assigned to two variables, then the entered ones are checked. If the entered data contains " (empty), or is not equal to what is stored in the variables, the sad message " for ][aker] is displayed Invalid name user or password." Otherwise, "Authorization was successful" is displayed and goes html code. The input form is described at the very end.

That's it with the admin panel. Now let's move on to the script that will display the news feed.

Listing 4. tnews.php:

What should you pay attention to here? Firstly, upon request

$request = "SELECT ntext , ntitle , ndate , nuser FROM news ORDER BY ndate DESC LIMIT 15";

It says this: display news sorted by date, and show only the first 15 news on the page. mysql function _fetch_array($result) returns an array which is written to the $row variable. For those who don’t know what & nbsp it is, I say: it’s free space. All that remains to be done is to insert tnews into some ready-made page, for example like this:

Include("./tnews.php")

That's all I wanted to tell you. If you have any questions, feel free to ask them on our website.

Typically, the process of developing a dynamic news feed requires the use of a back-end development language. Obviously, everything can be very simple if you are familiar with Rails or PHP, but we would like to introduce you to a method for displaying an RSS feed with using jQuery. The problem is how to access ajax requests with external server, and then convert that XML information into something that is easy to read.

We'll focus on Google's Feed API, which is much easier to work with than it looks. There are several options here that allow you to play around with some of the options and access other templates. We only need to display the first page of posts in the feed as title + URL. Once you get this script working, you can easily issue appearance, and even partially include content in the news feed. Below you can check out the demo to get an idea of ​​what we will be working on today.

Let's get started

The first step is to develop a basic document with an integrated jQuery library. We'll use a few functions found on Stack Overflow to help us get the RSS feed working. Typically, when writing a web application like this, you'll need to access cross-domain scraping through a back-end development language. For web developers, the problem is usually solved by PHP.

Since we are using the Feed API here, it will take the place of the script on the server side. This way, everything can be read via jQuery Ajax and then converted to JSON, which will be much easier to parse. Let's take a look at the header of our document, which includes jQuery as well as a custom parser.js script.

Automated jQuery RSS Feed Demo

The advantage of our code is that we can fill everything when javascript help. All internal RSS elements on the pages will be written into the HTML, but they will not appear in the document source code. All content will be published dynamically, and you can quickly edit feed sources, which can be changed every time the page is refreshed.

Automated jQuery RSS Feed Listing

This is the entire contents of the body of the document. Each .feedcontainer class is used to apply the same styles to headers and links. ID names are specified when jQuery help, and this way we have the ability to run our own RSS functions and then fetch new content using the .html() method.

RSS parsing in jQuery

We can skip most of the CSS styling as it is very simple. Take a look at our style sheet if you want to see how alignment is implemented. But let's move on to parser.js, where we can see two functions, one of which is specifically responsible for RSS-JSON conversion. The other is responsible for ensuring that the “title” attribute in the RSS feed is automatically displayed in uppercase.

Function parseRSS(url, container) ( $.ajax(( url: document.location.protocol + "//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&callback=?&q=" + encodeURIComponent(url), dataType: "json", success: function(data) ( //console.log(data.responseData.feed); $(container).html(""+capitaliseFirstLetter(data.responseData.feed.title )+""); $.each(data.responseData.feed.entries, function(key, value)( var thehtml = ""+value.title+""; $(container).append(thehtml); )); ) )); )

This parseRSS() function was posted in a similar question on Stack Overflow and is a great solution that only works with jQuery. We will still need a server, since the .ajax() method cannot happen without an HTTP stream. But you won't need support for any other backend development languages. Overall we can trust Google job with this resource, as it is indispensable for developers.

The function code is quite simple and understandable if you are familiar with .ajax(). First, we include a URL in the request, which is a link to Google remote Feed API. We need the dataType to be set to JSON and after a successful ajax request we can run new feature to display HTML content. Please note that the parameter we are entering is called data, and it contains a JSON response.

Response Loop

Also note that we left a comment around the console.log(data.responseData.feed) code in case you want to display the result of the entire JSON content. You can show more than just the title and URL. But we specifically gave an example this way. You have the opportunity to add other content at your own discretion, directly using this function.

Inside the successful ajax request function, we have built in a .each() loop that iterates through the item within the feed. Note that this array can be accessed from data.responseData.feed.entries, which we convert to a key-value pair. Everything is very simple here, since we only need one string variable to contain the HTML. Then, using jQuery .append(), all the content is added to the page.

Title is not included in the .each() loop because it must be added with each result in the feed. But it also uses another custom function designed to uppercase the first letter of each javascript string. Some RSS feeds only provide headers in lowercase, which can look frivolous and, at the very least, strange. Let's take a look at some very short code for the function.

Function capitaliseFirstLetter(string) ( return string.charAt(0).toUpperCase() + string.slice(1); )

Remember that RSS feeds can provide much more more information for publication. Sometimes you can even use post thumbnails embedded in XML. This will allow you to display these images inside your RSS application. The possibilities here are almost endless.

Let's summarize

RSS feeds today are still very popular among web developers, as they represent great method transferring data between websites. This allows online marketers to easily post updates to Twitter or Facebook directly from the site via RSS feed. We recommend that you download source our example, and study it carefully. In addition, you can understand whether you can use similar functionality in your own websites and projects.

Sources in the archive.

Do you have a website or just a personal page? We will help make them more interesting!

Especially for this purpose, every day we find and select the most interesting news from many popular sources. You can post on your website (by saving a link to the site) news that corresponds to the theme of your site - only those materials that will be of interest to your visitors.

News selection is carried out by topic, source, keywords. Additionally, we can filter only those news that have the most views, user comments and the highest ratings.

It's simple!

Setting up and installing the code will take no more than 5 minutes.

To install our news on your website, you do not need to have your own server, know PHP/Perl, MySQL/MS SQL/PostgreSQL - as a rule, a minimal knowledge of HTML and CSS is enough! Don’t worry even if you are not strong in HTML and CSS - you can just choose already ready-made option news feeds, all you have to do is copy and paste the code. If necessary, we will help you do this too. Feel free to ask questions, write about your suggestions and wishes: (ICQ) 75932371 or (e-mail) info@site

Once you place the code, you no longer have to waste time searching and selecting articles - the latest and most interesting news on a topic that interests you and your visitors will appear on your site automatically.

How it works?

Everything is very simple - now, right here, step by step, you will set up a news feed (select a topic, number of news, etc.), then select a display option, customize (if desired) the design to your liking, check the appearance of your news feed and - hooray! - All you have to do is copy the code to your page.

So, let's go!

Step 1: Customize your news feed
Collect news


Auto Technologies Web Culture Music Cinema Sports Finance St. Petersburg Russia In the world Politics From life Weapons


No more in the feed5 10 15 latest news
Show in every announcement

Display optionstandard alternative, unrated as on Twitter, light as on Twitter, dark, with date visible (except IE) carbon, with date visible (except IE) your own

News feed DOM structure for creating your own style: div#nn_news

Russian character encodingUTF-8 Windows-1251

Step 2: Get placement codes and check the appearance

In accordance with your settings, a code was created that you can embed into your page - and you already have the news!

If you feel strong, have some knowledge of HTML and CSS (or just like to experiment), you can change the contents of these windows manually and immediately see how your news feed:

The code that determines how the news feed will look - it is advisable to insert it between and your page. But as a rule, you can place it anywhere in the HTML, for example, close to where your news feed will be:

Call code - set it where the news feed will be displayed:

Important: a new window will open in which you will see what your news feed will look like!

RSS is a family of XML formats designed to describe news feeds, article announcements, changes in blogs, etc. Information from various sources presented in RSS format can be collected, processed and presented to the user in a form convenient for him by special programs - aggregators

Wikipedia

On many sites you will see an or button, which means that the site has an RSS feed available. This service is convenient for site readers, because with the help of Rss aggregators, for example, Yandex.Lenta or Kanban.ru, you can collect your own news feed and read news from different sites in one place or receive them to your e-mail using the Rss2Email service. Read more about RSS, for example, in Computerra.

This article is not about using RSS feeds and aggregators, but about how to quickly create a news feed on a website using the RSS.PHP and MySql program.

Initial conditions

So, you have a website that has PHP support and a working base MySql data(by the way, a very common combination). From the database in which your blog texts or news announcements are stored we will receive data for our feed. The feed will be built dynamically, directly upon request, so all you need is the corresponding script. You can write it yourself, but you can take a ready-made one

A little theory

First, a little theory. For our feed we will use the RSS 2.0 format; for those who are interested, I direct you to the description of the format that you will find. And we will not use all the elements, but only those that we really cannot do without:

Element Description
title Channel title.
link Link to the main page of the site.
description Description of the channel.
language The language in which the channel is written. For example, ru - Russian
copyright Copyright
managingEditor Email channel editor.
webMaster Webmaster email.
pubDate Date of publication of information in the channel.
lastBuildDate Time of last channel change.
category Determines the category to which the channel belongs.
Item The actual content of the channel. There must be at least one element.

Item

Element Description
title Heading
link Link to full text
description annotation
category Category
pubDate Date of publication of the article. Some aggregators will not display an article if the specified date has not yet arrived. But not all.

Since RSS is a subset of XML, we should end up with something like this: If you change anything in the script, check it for compliance with the standard using the Feed Validator or RSS Validator services
Starting the program

The program consists of the following files: rss.php - launch file, rss.inc - included file with a description of the class, conn.inc - file with a description of the database connection parameters.

To run the program, change the database connection data in the conn.inc file, change the feed parameters in the rss.php file and write to any convenient catalog on your web server, at least in the root. Don't forget to check the data structure MySql databases, for a program you can use, for example, this:

CREATE TABLE BLOG (ID int(11) NOT NULL default "0", title varchar(50) default NULL, description text default NULL, link varchar(200) default NULL, date date default NULL, category varchar(100) NOT NULL default "", ab varchar(10) NOT NULL default "", PRIMARY KEY (ID)) TYPE=MyISAM;

after that you will have a tape with the address

What's next?

Now don’t forget to put a button in a visible place and add the code between the tags to the pages of your site:
so that browsers understand that you have a feed. And, of course, register the feed in search engine, for example, http://blogs.yandex.ru/add.xml and in the catalog http://blogs.yandex.ru/add-catalogue.xml

You can download the program for creating RSS feeds for free, Rss.php (3 Kb)







2024 gtavrl.ru.