URL variables using JavaScript

November 5, 2008

// Create variable is_input to see if there is a ? in the url
var is_input = document.URL.indexOf(‘?’);

// Check the position of the ? in the url
if (is_input != -1)
{
// Create variable from ? in the url to the end of the string
addr_str = document.URL.substring(is_input+1, document.URL.length);

// Loop through the url and write out values found
// or a line break to seperate values by the &
for (count = 0; count < addr_str.length; count++)
{
if (addr_str.charAt(count) == “&”)
// Write a line break for each & found
{
document.write (“<br>”);
}
else
// Write the part of the url
{
document.write (addr_str.charAt(count));
}
}

}
// If there is no ? in the url state no values found
else
{
document.write(“No values detected”);
}

Are you a good Developer!!

October 15, 2008

Good ‘ means having qualities that are desirable and distinguishing. From a developer’s perspective, these qualities are both technical as well as non-technical. Here are some qualities that a developer should possess if he/she aims at becoming a ‘good’ developer.

1. Good Communication skills- In the software world, people usually define good communication skills as fluency in a spoken language. That’s not really what it is.It is how effectively you are able to communicate with others. As a’good’ developer, you should be able to express yourself well, listenwell, as well as manage the communication process well.

2. Positive Attitude- ‘Your attitude determines your altitude’. I am sure you must haveheard that phrase. From a developer’s context, this means believe in you, stop blaming others and adapt to changes without fretting. Remember that you always get what you want because you always want what you get.

3. Read Books- Read plenty of them to get a good idea of different technologies. Reading books gives you quick and handy insight into a technology. You should choose books that are usually written by known professionals who recommend best practices and different methods of solving a business problem using the technology. During the course of time, you will learn to develop your own methods. Who knows you may one day write a book!!

4. Practice, Practice and Practice- I have seen a lot of developers having good amount of theoretical knowledge. They read a lot of books and technical material. However when it comes to applying that knowledge, they take a back foot. That is because they do not practice. Efficiency and effectiveness in yourwork can only be attained if you practice code. The only way you canmake a good developer of yourself is to practice, and then practice some more.

5. Follow Patterns and Best Practices-
Patterns & practices reflect technical guidance, technicalproblem domain and engineering practices based on real-world experienc es. The process to learn it is gradual, but once done; it cansave you a lot of time and efforts and enable you to work smartly.Follow a Code Design Guideline. Always use a code analysis tools that will evaluate and analyze your code.

6. Discussion/Newsgroup- Participating in communities develops the quality of leadership and contribution, both ingredients of success. Having participated in a good technical discussion on the community leaves you with a feeling of self-satisfaction . It also increases your knowledge, as you find smart people who can validate your solutions, as you validate theirs. It also teaches you to appreciate suggestions. Do not forget to ‘pat someone on the back’ if he/her has made a positive contribution. Believe me it makes all the difference.

7. Knowledge of Networking and Databases- People may contradict this point, but during my career as adeveloper, I have realized that a good developer should know the basics of networking and databases. Almost all the solutions that we create, involve interactions with networks and databases. Having knowledge of these two, helps you write better code and saves you on a lot of time too.

8. Blog/ Write Articles- How many of us can remember everything? I cannot. So I document them.It helps me to reference stuff when I need them. Over and above, it also helps me get some good feedback from readers and shows me multiple approaches to do the same stuff. I have received a lot of feedback about my work, some good and some not so good. However, I do validate them and learn from this process. It develops the developer in you.

9. KISS- Keep Implementations/ Approaches Short and Simple. Do not overcomplicate things by using jargons, which people find it hard tounderstand. The key is to simplify your designs and avoidover-engineering things.

10. Think as a Tester- Developers and Testers; two sets of people from different camps,ready to take on each other. I have observed that the intersection ofthe two produces good results. It does not harm taking off thedeveloper’s hat for some time and putting on the tester’s hat. In fact,in the long run it helps you reduce bugs/defects in your code. Youdevelop a mindset of about breaking your code, when you are creatingone.

11. Consistency is the name of the game -Do you hop jobs too often or are bitten by the ‘salary’ bug? If yes,then it’s time for you to sit down, relax and plan. Invest time in thinking and let your decisions not be spontaneous. To move ahead, you need a solid foundation and that comes with consistency.

12. Attend technology seminars and events- If there is one hosted in your city, make sure you take out time to attend one. Most of them are free and provide a valuable source of information about new technologies.

13. Jack of all or Master of One?- Well that’s a difficult one to answer. In today’s scenario, you have to master more than one technology. Practically it is quiet difficult to do so, but the good ones do it. However the key is adaptability overhere. If you are good at any one technology and confident in it, givenan opportunity, it would be easier for to relate to a new technology ina short period of time. Try it out as it enables you to compare technologies and make decisions, once you have worked with them.

14. Stop complaining- Did the software fail or are the testers giving you a tough time byfinding a lot of bugs? A natural tendency for a lot of developers is toreact and then overreact in such situations. While the reaction isnatural, it may not be desirable. Analyze why the software failed orwhy was the bug reported. It is going to be a learning experience and will help you in your future projects.

At the end, just remember that you are not going to remain a programmer forever. So once you are satisfied and get a feeling that you have proven yourself as a good programmer, it’s time to re-program yourself.Expand your interests. Development is just a part of the process.Understanding the users and business is in itself an art, one should aim for and should master over a period of time. I hope you liked the article and I thank you for reading it.

What Is RSS? Why RSS? How to use with PHP?

September 26, 2008

What is RSS?

RSS (Rich Site Summary) is a format for delivering regularly changing web content. Many news-related sites, weblogs and other online publishers syndicate their content as an RSS Feed to whoever wants it.

RSS syndication is one of the most common TLAs around (TLA stands for Three Letter Acronym). RSS as an acronym has stood for various things, but the current standard is: Really Simple Syndication. This is the most recent variation of this very common and very useful standard.

Back when the Internet was young(er), a piece of software called Pointcast pushed data to a screensaver application on a user’s computer, providing news updates of all kinds. Eventually browser developers such as Netscape and Microsoft worked to create something similar to this immensely popular service. Netscape produced the most widely accepted variant and that eventually was released into the development wilds of the Internet, to eventually become the RSS of today.

RSS distributes recently updated information to many receivers, much like a broadcast system. Once you have a substantial number of users, the RSS feed acts like a beacon to draw your users back to look at updates. It is little wonder that RSS has increased in popularity and use among content providers, as it provides a much needed method of maintaining an audience’s attention.

When you see the icon in Figure 1 you can bet that an RSS feed is available on that site. This icon is the de-facto standard icon representing the availability of RSS for updates at a site. The curved lines represent radio waves, a symbol of the broadcast nature of the RSS feed.

Why RSS? Benefits and Reasons for using RSS

RSS solves a problem for people who regularly use the web. It allows you to easily stay informed by retrieving the latest content from the sites you are interested in. You save time by not needing to visit each site individually. You ensure your privacy, by not needing to join each site’s email newsletter. The number of sites offering RSS feeds is growing rapidly and includes big names like Yahoo News.

How can I use RSS?

Your site has content that you want to get out to the masses, which is why you put it on the Internet in the first place. Once a substantial number of users know about your site and content, will they come back each day to check for updates? Probably not. Of all the sites you frequent, do you go to each one daily to check for updates? Probably not. This is where RSS comes in.

For your users, RSS can be a huge benefit, especially if they value opinions or news listed on your site. Without having to return to your site frequently, they will know exactly when you update or add content, allowing them to save time and effort, and they won’t miss anything either!

Content generation isn’t a problem, if you incorporate RSS feeds to fuel content aggregation for your own site. If you pull data off a feed and include it in your site, it can add a good amount of content to your site with only a little bit of time investment.

Personally, I like to use RSS to gather feeds from filtered results from various sites such as Craigslist (www.craigslist.org). A little trick I use is for shopping for used electronics. You can set up a site search and then RSS the resulting page. If you set up a feed for a search for cameras within a certain price range, you can see when anyone posted a camera for sale within your price range, on your RSS feed! Gives you a big advantage when you are trying to be the first bidder!

Where does PHP come in?

This article assumes that you have some experience using PHP already and can use a function to pass a variable and return a result. PHP has many functions that make short work of XML both in and out of an application.

First you want to take information from a locally stored data source, a content management system, blog or any content which fits the format of an Internet document and put that out as a feed to your users. You will need to get this data, format it into an RSS object, and serve requests for it.

Portions of your site require additional content and rather than go out into the world seeking additional content documents for your site, you can take advantage of the multitudes of RSS feeds already prepared. You will use XML_RSS to get and handle these feeds for your site.

XML_RSS() is a PEAR package to help you get through the more complex tasks of interpreting an XML RSS file more easily. PEAR is an open source library of PHP functions which is free for your use and under continual development. You might already have PEAR installed with your PHP installation, but you might need to install it for this article (see Resources for a link). XML_RSS() is simply a function, which given the location of an RSS feed, will load the XML of the feed into an array, ready for your use in your PHP application. The elements of the array will have named keys, associated with the elements and attributes of the RSS file read.

Basic functions to get the data out

Assuming you have a database of choice, you will create a connection as normal, and generate a page displaying the XML laid out in a user readable fashion.

Sending the page to the requestor

Now that you have the data all well formatted in your own code, you need to make sure you hand the data out properly so when someone inputs your URL into their reader, they will get the XML RSS feed they expect (see Listing 2).

RSS applications

A good number of applications, many of them free, can read an RSS feed and many of them allow you to aggregate the feeds. The aggregation features allow a user even further refinement over the amount and nature of content they receive. Each reader has different features, designed to help make sense of the incredible amount of information coming from the Internet.

Some examples are Thunderbird and Firefox by Mozilla, Internet Explorer 7 and upcoming versions of Office by Microsoft and many others, as close to you as the nearest search engine. With all the various ways to get and read feeds, it is very likely that you will find something that suits you. Unless of course you are a picky software developer and want to write your own! This article will get into that soon enough!

Back to top

How can I use RSS?

Your site has content that you want to get out to the masses, which is why you put it on the Internet in the first place. Once a substantial number of users know about your site and content, will they come back each day to check for updates? Probably not. Of all the sites you frequent, do you go to each one daily to check for updates? Probably not. This is where RSS comes in.

For your users, RSS can be a huge benefit, especially if they value opinions or news listed on your site. Without having to return to your site frequently, they will know exactly when you update or add content, allowing them to save time and effort, and they won’t miss anything either!

Content generation isn’t a problem, if you incorporate RSS feeds to fuel content aggregation for your own site. If you pull data off a feed and include it in your site, it can add a good amount of content to your site with only a little bit of time investment.

Personally, I like to use RSS to gather feeds from filtered results from various sites such as Craigslist (www.craigslist.org). A little trick I use is for shopping for used electronics. You can set up a site search and then RSS the resulting page. If you set up a feed for a search for cameras within a certain price range, you can see when anyone posted a camera for sale within your price range, on your RSS feed! Gives you a big advantage when you are trying to be the first bidder!

The RSS 2.0 standard format

The RSS standard defines and contains the content of a feed. These feeds can be from any data source, defining Internet documents and in a very basic sense, make up a list of links and their descriptions.

Look at the RSS format in Listing 1, which uses a sample document from the NASA “Liftoff News” feed.
Listing 1. A sample RSS 2.0 document

<?xml version="1.0"?>
<rss version="2.0">
  <channel>
    <title>Liftoff News</title>
    <link>http://liftoff.msfc.nasa.gov/</link>
    <description>Liftoff to Space Exploration.</description>
    <language>en-us</language>
    <pubDate>Tue, 10 Jun 2003 04:00:00 GMT</pubDate>
    <lastBuildDate>Tue, 10 Jun 2003 09:41:01 GMT</lastBuildDate>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <generator>Weblog Editor 2.0</generator>
    <managingEditor>editor@example.com</managingEditor>
    <webMaster>webmaster@example.com</webMaster>

    <item>
      <title>Star City</title>
      <link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp</link>
      <description>How do Americans get ready to work with Russians aboard the
        International Space Station? They take a crash course in culture, language
        and protocol at Russia's Star City.</description>
      <pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate>
      <guid>http://liftoff.msfc.nasa.gov/2003/06/03.html#item573</guid>
    </item>

    <item>
      <title>Space Exploration</title>
      <link>http://liftoff.msfc.nasa.gov/</link>
      <description>Sky watchers in Europe, Asia, and parts of Alaska and Canada
        will experience a partial eclipse of the Sun on Saturday, May 31st.</description>
      <pubDate>Fri, 30 May 2003 11:06:42 GMT</pubDate>
      <guid>http://liftoff.msfc.nasa.gov/2003/05/30.html#item572</guid>
    </item>

    <item>
      <title>The Engine That Does More</title>
      <link>http://liftoff.msfc.nasa.gov/news/2003/news-VASIMR.asp</link>
      <description>Before man travels to Mars, NASA hopes to design new engines
        that will let us fly through the Solar System more quickly.  The proposed
        VASIMR engine would do that.</description>
      <pubDate>Tue, 27 May 2003 08:37:32 GMT</pubDate>
      <guid>http://liftoff.msfc.nasa.gov/2003/05/27.html#item571</guid>
    </item>

    <item>
      <title>Astronauts' Dirty Laundry</title>
      <link>http://liftoff.msfc.nasa.gov/news/2003/news-laundry.asp</link>
      <description>Compared to earlier spacecraft, the International Space
        Station has many luxuries, but laundry facilities are not one of them.
        Instead, astronauts have other options.</description>
      <pubDate>Tue, 20 May 2003 08:56:02 GMT</pubDate>
      <guid>http://liftoff.msfc.nasa.gov/2003/05/20.html#item570</guid>
    </item>
  </channel>
</rss>

Where does PHP come in?

This article assumes that you have some experience using PHP already and can use a function to pass a variable and return a result. PHP has many functions that make short work of XML both in and out of an application.

First you want to take information from a locally stored data source, a content management system, blog or any content which fits the format of an Internet document and put that out as a feed to your users. You will need to get this data, format it into an RSS object, and serve requests for it.

Portions of your site require additional content and rather than go out into the world seeking additional content documents for your site, you can take advantage of the multitudes of RSS feeds already prepared. You will use XML_RSS to get and handle these feeds for your site.

XML_RSS() is a PEAR package to help you get through the more complex tasks of interpreting an XML RSS file more easily. PEAR is an open source library of PHP functions which is free for your use and under continual development. You might already have PEAR installed with your PHP installation, but you might need to install it for this article (see Resources for a link). XML_RSS() is simply a function, which given the location of an RSS feed, will load the XML of the feed into an array, ready for your use in your PHP application. The elements of the array will have named keys, associated with the elements and attributes of the RSS file read.

Getting data out

Now that you know what the RSS data format is, you can look at the data you want to hand out to the world, and put it in that format. Thankfully PHP has some powerful RSS and XML handling features to speed your development along. Like many of the common Web standards, PHP has a number of great functions ready for use in this application.

For this article, you will pull data out of a database, using "" and format it into an RSS feed. You will set it up so that it looks for the most recent additions to your dataset whenever the RSS feed is called upon and returns a fresh RSS to the requester.

The feed can come from any data source on your site, but in the end you need to make sure there is enough data that the person receiving the RSS feed will be able to use the data. At a minimum the URL name and description is needed. Any data that is published on your site can be turned into a feed.

You will use PHP to connect to your Web application database, pull updated information out, and format it into an XML RSS document.

Basic functions to get the data out

Assuming you have a database of choice, you will create a connection as normal, and generate a page displaying the XML laid out in a user readable fashion.

Sending the page to the requestor

Now that you have the data all well formatted in your own code, you need to make sure you hand the data out properly so when someone inputs your URL into their reader, they will get the XML RSS feed they expect (see Listing 2).
Listing 2. The complete RSS.php

<?php

$database =  "nameofthedatabase";
$dbconnect = mysql_pconnect(localhost, dbuser, dbpassword);
mysql_select_db($database, $dbconnect);
$query = "select link, headline, description from `headlines` limit 15";
$result = mysql_query($query, $dbconnect);

while ($line = mysql_fetch_assoc($result))
        {
            $return[] = $line;
        }

$now = date("D, d M Y H:i:s T");

$output = "<?xml version=\"1.0\"?>
            <rss version=\"2.0\">
                <channel>
                    <title>Our Demo RSS</title>
                    <link>http://www.tracypeterson.com/RSS/RSS.php</link>
                    <description>A Test RSS</description>
                    <language>en-us</language>
                    <pubDate>$now</pubDate>
                    <lastBuildDate>$now</lastBuildDate>
                    <docs>http://someurl.com</docs>
                    <managingEditor>you@youremail.com</managingEditor>
                    <webMaster>you@youremail.com</webMaster>
            ";

foreach ($return as $line)
{
    $output .= "<item><title>".htmlentities($line['headline'])."</title>
                    <link>".htmlentities($line['link'])."</link>

<description>".htmlentities(strip_tags($line['description']))."</description>
                </item>";
}
$output .= "</channel></rss>";
header("Content-Type: application/rss+xml");
echo $output;
?>

OUTPUT

<?xml version="1.0"?>
    <rss version="0.97">
        <channel>
            <title>Our Demo RSS</title>
            <link>http://www.tracypeterson.com/RSS/RSS.php</link>
            <description>A Test RSS</description>
            <language>en-us</language>
            <pubDate>Mon, 13 Nov 2006 22:46:06 PST</pubDate>
            <lastBuildDate>Mon, 13 Nov 2006 22:46:06 PST</lastBuildDate>
            <docs>http://someurl.com</docs>
            <managingEditor>you@youremail.com</managingEditor>
            <webMaster>you@youremail.com</webMaster>
    <item rdf:about="http://www.tracypeterson.com/">
            <title>This is Tracy's Web Page!</title>
            <link>http://www.tracypeterson.com/</link>
            <description>This is a demonstration of how to get PHP to work for
your RSS feed.</description>
        </item><item rdf:about="http://www.tracypeterson.com">
            <title>This is Tracy's site again!</title>
            <link>http://www.tracypeterson.com</link>
            <description>Again, this is a demonstration of the power of PHP
coupled with RSS.</description>

        </item></channel></rss>

Bringing data in

You will use the XML_RSS() functions to get your RSS feeds into your PHP scripts, ready for use like any other array. Just like a query to a database, you will have an array, ready to use as you see fit.

In this case, you will connect to the RSS.php and load up a copy, displaying it in an unordered list (see Listing 4).

showfeed.php

<?php
require_once "XML/RSS.php";

$rss =& new XML_RSS("http://www.tracypeterson.com/RSS/RSS.php");
$rss->parse();

echo "<h1>Headlines from <a
href=\"http://www.tracypeterson.com/RSS/RSS.php\">Tracy
        Peterson's Site</a></h1>\n"; echo "<ul>\n";

foreach ($rss->getItems() as $item) {
  echo "<li><a href=\"" . $item['link'] . "\">" . $item['title'] .
"</a></li>\n";
}
echo "</ul>\n";
?>

The example shown in Listing 4 comes directly from the PEAR manual, and I used it because it so concise. Let’s go through it line by line and see that it really only uses a couple of the methods available to the XML_RSS() class, the constructor and parse(). Parse simply renders the output as the array that I mentioned before.

First, you use the require_once() function to load the RSS.php file from your PEAR installation. If PEAR is set up properly and XML_RSS installed, it will find this include file correctly and you will now have the XML_RSS object ready for your use. Next, you create a new object called $rss, which is the result of passing the URL to the feed to your XML_RSS constructor.

You simply use the parse() method to return the values in the RSS feed. The first echo line begins to set up the basic HTML you use to make the RSS feed human readable. In this case you announce that the unordered list is a list of headlines from my site!

The foreach() statement gets each item element from the parsed feed, using the getItems() method as a new array $items. Each of the array elements are named after the actual XML tag they are contained within. In this case you only use link and title, in a moment you will add description to explore this point. Each time the foreach loop processes, it will move to the next element until the entire RSS feed is laid out in this fashion.

Agile PHP

September 25, 2008

Java background shifting to PHP5. Here are some quick info for Java geeks who wants to use PHP5 (there are many reason why should you use PHP 5, I won’t go in detail into that).

1.You can continue using your favourite Eclipse IDE for PHP5 programming. Use PHPEclipse http://phpeclipse.sourceforge.net/update/releases

2.PHP5 is a full blown language, so like ANT, you have a make/build tool called Phing. This is based on ANT so you can migrate your ANT script for Java to build a PHP5 project. Yes …with little restrictions.

3.If you have been designing Enterprise style Java application having fine grained tiers like Web tier, Business Tier and Data tier …you can continue following the same approach!!
4.If you are coming from ORM (Object Relational Mapping) background like Hibernate, good news …you have plenty of choice in PHP5. My favourite is Doctrine. Those Doctrine developers are the smartest kid on the block. Doctrine is the best thing that has happened to PHP world. I will be writing shortly a Quick Learn article on my use of Doctrine in an application.
5.Now does there a Unit testing framework like Junit in PHP5. Yes … you have PHPUnit which is also part of XUnit family, also there is the simplest testing framework called SimpleTest, SimpleTest is sweet one, it gets you up & running in few seconds. So you can use TDD (Test Driven Development) and build high quality PHP app.
6.In case you are looking for a framework like Spring Framework in Java/J2EE, then there is Zend Framework. Though Zend Framework can’t be compared to Spring in totality but it is built using loose coupling concepts … it doesn’t dictate you and you can follow your own design & architecture.

7.How about MVC framework like Struts? There are plenty of choice in PHP5 like Symfony, CodeIgnitor, CakePHP. But I liked Zend Framework’s View, Controller approach as it allows me to use my own design and architecture approach and not tightly integrate the modules.
8.So what about Tempalting framework like Velocity and Freemarker in java…is there anything like this in PHP5 ? Yes, there is Smarty, which is programmer’s first choice.

Using the above framework on your Eclipse IDE, you can rapid develop application in PHP5, increase testability, and improve productivity.

There are great framework out there but my approach in this article has been to choose only those API/framework which matches with what I have been following in Java world for years.

Hello world!

September 24, 2008

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!


Follow

Get every new post delivered to your Inbox.