Monday, January 9, 2012

Looping through select fields and getting the selected text using jQuery

I spent a little time wrestling with this and I thought it may come in useful to pop on to the blog. In my situation I had to loop through a number of select items as I needed to post some data. I needed to do this as I had to pass 3 sets of information from each select / drop down box. It had to be done this way because I was using the dropdown boxes for a number of different functions, some needed to pull the text and some needed to pull the value.
  • The name of the select box
  • The value of the select box
  • The text of the selected item
$('select').each(function(){
     var selected_text = $(this).children('option:selected').text();
})
Happy programming :)

Labels: , ,

Friday, December 24, 2010

The Flexibility of PHP

I have been using PHP for 4 years now and I like to think I know it pretty well by now. It's a great language, it is open source, has regular updates and fixes, and has a great online manual.

What you also get when you talk to other developers who use other technologies is that it is non-complied and uses the 'Spaghetti' coding style where you have SQL in with your code, along with the formatting of the results returned from the database and all your HTML.

Well, for them and other people who are looking to learn a Server side Web Programming language, PHP is not sub standard, PHP is flexible - let me explain. People are also under the opinion that PHP is an easier language to learn than many other languages and yes it can be, but only because it is flexible and allows you to explore it from a number of different angles.

First, let's take a look at the 'Spaghetti' option, yes it is a little in efficient and yes it CAN be hard to edit if you have to come back to it in the future. But if you are new to programming and web development then it is the perfect way to start. You can learn the other (multi-tier) methods when you feel a little more comfortable. When you first learn to program, learning using a procedural manner is a great way to learn a language, the way it behaves and how logic determines specific conditions. Once you have these skills mastered, you can then start to apply them to more advanced methods, i.e. Object Oriented Programming.

For those who have never seen a bit of PHP 'Spaghetti' Code it looks a little like this:

<?php
    $SQL = "SELECT field1 FROM table";
    $query= mysqli_query($databaseLink,$SQL);
    while($row = mysqli_fetch_array($query,MYSQL_ASSOC)) {
       echo '<div class="tableResult">'.$row['field1'].'</div>';
   }
?>

The other option is to use a multi-tiered approach which could use Object Oriented Methods. When you use this approach, it works around the basis of separating code into different sections. When I first learnt OOP I used a two tier approach as I was a freelance developer and as such I thought that it was the best way to go. My two tier approach could very be different from another developer's 2 tier approach, but again, this is the flexibility of PHP. My 2 approach worked on the basis of keeping my Application Logic and my presentation in 1 tier and my business and data logic in another tier. When I created an application, I would follow this method and it served me well, across numerous (and large) applications. If I wanted to use a class, I would invoke it in the logic based file, the results would be sent back and the logic based file would process, format and output the results. The next example uses a separate database class but premise is the same, that the database and application classes from the controller or or input decision file.

//database class
class database {
public function retrieveResults ($SQL) {
$rows = array();
$query= mysqli_query($database,$SQL);
while($row = mysqli_fetch_array($query,MYSQL_ASSOC)) {
$rows[] = $row;
}
return $rows;
}
}

//application class
class application {
public function getTableData() {
$SQL = "SELECT field1 FROM table";
$useDatabaseClass = new database();
$results = $useDatabaseClass->retrieveResults($SQL);
return ($results);
}
}

//calling script
$useApplication = new application();
$getData = $useApplication->getTableData();

foreach ($getData as $row) {
echo '<div class="tableResult">'.$row['field1'].'</div>';
}

Then there is the 3 tier approach - one popular name for it is the MVC approach. This is where you separate application logic, data handling and presentation in to their three separate components. This is useful to use when you on any project, but I found it of most useful since I have been working in a design agency, as I work on multiple projects on any one time and front end guys who work there need to access many of the view files. Please note that the way you call scripts in reality would probably not look like the above, these are just for illustration purposes. In reality, if you use an MVC framework or you create your own, you would more likely extend your base classes rather invoking them using the new keyword.

The three tier is very similar to the two tier approach. Except that the $getData array is passed through to another file which outputs the results using the foreach loop. That's it, the hard work is really done when you move from one tier to two tiers, the third tier (the view tier) is really just a minor extension of the controller tier, which in my opinion works in tandem with the Model or Business Tier. The view or presentation file can be loaded in a number of ways, using include() or require() or even eval().

A few last words, when you start down your PHP developer career, do not let anyone tell you which method to use, weigh up the situation and make an informed decision. I like to build my online applications using 3 tiers using codeigniter as the MVC framework, but I still write single standalone scripts when I feel the situation needs it

Labels: , , ,

Wednesday, June 2, 2010

What is it be a web developer?

This piece is not about the history of the web, more about how the web grows and how new technologies constantly change the nature of what it means to be a web developer.

The Internet is growing (nothing new there with that statement). When a new idea or piece of technology springs up, developers are needed to be able to write software for it so that the new innovation gains ground and popularity.

The term 'web developer' came about because the web became the most sought after use for the Internet. When this happened people wanted to access information quickly and reliably. As a result web developers started programming on systems and servers that could dish out this information in a better way. With the integration of databases and server side coding, it allowed websites to move on from the stateless nature of HTML that is when Tim Berners-Lee developed it.

As time has moved on the web has gone from strength to strength. The other huge leap forward was advent of Javascript, a programming language that allowed coders to manipulate the pages that users saw in their browsers. This allowed websites to start behaving more the more traditional applications that ran on PCs and other computers. Of course it was only matter of time when the two started to inter mingle to give us even better applications and lo and behold Ajax sprang up when Microsoft created the XMLHttpRequest Object which allowed developers to send asynchronous requests to servers without the page reloading, thus enhancing even further the web experience. Ajax was designed to work with both XHTML and XML so that information could be injected and updated quickly and using less resources. Why reload the whole page when you only need to change the information in a single element? XML and the many web services created to use it allowed people to integrate information from website to another in a standardised way and crossed the divides that sprung up from the use of different systems.

The web didn't stop there over time, newer ideas and innovations continued to spring up all over the shop and applications became more and more complex. Social Media started to come in to the main stream and integration between web sites and applications became more and more common. MySpace and Facebook started to come to fruition and the web continued to make new ground with many people that had considered the web to be a fad with those 'young people'.

With the advent of Ajax it allowed applications to be made and resemble desktop programs, but there was a problem that became apparent as time went on. As with most javaScript functionality it had its problems with the numerous browsers that were available in the market. It was also quite code intensive and lots of testing was usually needed to make sure code with all browsers. That is where my topic pops up and that topic is jQuery. jQuery is a javaScript framework and has ironed out many of the problems that made javaScript such a pain to work with in the first place. DOM traversal, Ajax calls and element selection was made easier and standardised, no more cross compatibility testing to make sure things worked in firefox and IE. Debugging and development time has become vastly decreased, something of which, if you are a business can be passed on when you quote your customers for work.

So what is the future for the web. Well in my opinion, the future is becoming smaller. Well it is terms of the web. Mobile technology is becoming more and more common and I think that is where a lot of the future innovations will lie. Apple and Google have both moved into the mobile phone market and making a lot of people think about using their mobile device as the primary device for accessing the Internet. I myself have bought a mac mini to develop applications for iPhone as I want to keep with up with how the market is changing. I haven't actually created an app yet as I have too many commitments in other areas but the area between desktop applications and web applications are coming together quicker than ever, something of which I think will change the web and the Internet forever.

Labels: , , , , , ,

How long does it take to become a web developer

This is almost like the question 'how long is a piece of string', because in my experience you can never stop learning.
The real question is how long does it take to become proficient as a web developer and my answer, in my opinion is about 2 years. This is obviously not a definitive answer and is subject to change, especially if you know a programming language for another discipline. The 2 year time frame should not put you off, but it will take you this amount of time to really truly understand all the ins and outs of web programming.
First let's take a look at what you technologies have to learn to become a web developer. When you start down this road, you need to understand how the web works and why it does what it does. What happens when a request is made by the user, how a server responds and how the output is generated by the browser.
The basic model for this piece of user interaction is like so:
The User Requests data via their browser -> The web server responds and sends HTML (Hypertext Markup Language - the language your browser understands) to your computer/device which then in turns displays it to you. That is it, the basic web request. A few other bits and pieces are sent but we'll look at those a little later.
So let's look at the above and break it down. The HTML this is the basic building block of the web and you cannot create a web page without knowing how write HTML. So this is obvious place to start, learning HTML is not that hard and you should be able to start knocking pages together within a week or so. The other part of learning how to write HTML is the ability to learn something called Cascading Style Sheets (CSS). CSS is a technology that allows a page's content (the HTML) and the page's design to be separated in to different elements. This allows for a more structured site, easier design updates and less cluttered mark up. Learning CSS can take a little longer to learn HTML as it has many different aspects to it. There are great many tutorials and books out there and any decent book that teaches you HTML should teach you CSS, if it doesn't, pick a different book. I have already written different post regarding books for beginners, so please take a look there for my recommendations. You should be able to grasp a good amount of CSS in a few weeks/months for you to be able to start knocking web pages together, it can take a couple of years to master CSS as there are so many ways of doing things. i.e. using different methods to solve different issues, different properties for different elements and coupled with the fact that it is constantly being updated by the W3c, it gives CSS and long a drawn out learning curve.
The previous paragraphs, really dealt with the browser/client side of what websites and how they work. The next sections really deal with how a server sends a page to the user. At a basic level a server responds with HTML based on a request from the user. These pages can be static - where a page's content is hard coded in to the request page or dynamic - where a page is constructed and delivered according to a set of parameters. Dynamic pages are still served in HTML, this is the only language a browser can intepret, the difference with a dynamic page is that the page's content can be manipulated by the server when it sends the information.
This is where you really start to become a 'web developer', when you start to learn a language that runs on a web server and is executed at run time. There are a great many choices when it comes to choosing the language, my personal preference is PHP, many others like ASP.net or Java - all work in a similar way and all perform similar tasks though PHP allows you to code using both procedural and object oriented approaches (see other posts of mine regarding this). This part of the learning curve is going to be the longest. You should be able, once you have grabbed the principles of programming, to start programming in PHP and even start creating some dynamic pages within a couple of months, even less if you are doing it full time. Believe me, if you stick with it and you like coding, when you start creating programs, see people using them, then you really feel an excellent sense of worth. However to truly understand the language of PHP is going to take a while, you may be able to create some decent scripts quickly, but like I said before it will probably take you a couple of years, before you get to the point where you are truly comfortable with language. An example of this is where you get to the point that you know that no matter what a customer/colleague asks you to do, you know that you will be deliver what ever they need.
Once last thing, before I sign off. People often ask 'Am I am scripter or a programmer ?' This is a large can of worms and I do not wish to reopen it as it has been argued to and fro by many people. In my opinion, a script is something that runs on a single page, an application is something that runs across many pages and does multiple tasks geared towards a set of goals or objectives.


Labels: , , , , , , ,