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.

  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



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
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates


Dev Shed Tutorial Topics: