Home arrow PHP arrow Page 3 - PHP and JavaScript Interaction: Storing Data in the Client - Part 2

Working in the client side: the JavaScript news ticker - PHP

In the first article in this series, we developed a simple PHP function that dynamically generates a JavaScript array and populates it with external incoming data. In this article, we will develop a real world application using this method, in the form of a news ticker.

TABLE OF CONTENTS:
  1. PHP and JavaScript Interaction: Storing Data in the Client - Part 2
  2. A brief look at the "createJavaScript()" function
  3. Working in the client side: the JavaScript news ticker
  4. Defining the JavaScript functions
  5. A practical approximation: showing the news ticker in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 15
May 09, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Probably a news ticker is one of the simplest and most common applications that we've seen on websites. Certainly, there are many ways to create such a thing, from server-side based solutions to JavaScript or Flash programs; choices are numerous.

However, the main advantage of creating this news ticker (or a similar application) is that we don't need to work directly with JavaScript arrays to update information, since the PHP function handles the process in a transparent way for us. Any updating task is performed at a text file level, even without harming the structural HTML markup. It sounds like I'm marketing the program to you! Not really. Let's start defining the corresponding JavaScript functions to make the news ticker work.

First, we need a file to store our news. Let's create a simple text file, which contains the news that we need to display. The "news.txt" file would look like this:

Google expands its search capabilities adding video searching. Are you ready to submit your personal video?

PHP hits new levels of performance with PHP5. Visit www.php.net to find out more!

Want to be completely protected from Internet attacks? Disconnect your computer right now!

Interview with possible alien creature! He said they're using MySQL to store world domination plans.

Monkey proved to be a lot smarter as a Linux system administrator in weird experiment. Oops, we're lost!

Despite the fact that our breaking news stories are rather funny (except for the last one…just kidding!), the file is extremely simplistic. So, the next step consists of generating the JavaScript array and storing in it the file contents. How do we do it? Here, we invoke our "createJavaScript()" function with the proper parameters:

echo createJavaScript('news.txt','news');

With this single line, we've created the "news" JavaScript array, directly storing the contents of the "news.txt" file into it. 

Having populated the array with our funny news, it's time to look at the necessary JavaScript functions. If you're not very familiar with the DOM (Document Object Model) and its methods, don't worry. The functions are pretty easy to grasp. Trust me.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap

Dev Shed Tutorial Topics: