Your browser (Internet Explorer 6) is out of date. It has known security flaws and may not display all features of this and other websites. Learn how to update your browser.
X

Code School

Part of being a good web developer is keeping up with new technologies, programming languages, and the communites that surround them. Often that's easier said than done since learning new stuff can sometimes take a very long time.

Lately I've been trying to pick up backbone.js, and while there is a lot about it online its often scattered or incomplete. There's a book coming out, but for now it's only partially complete. I just needed something to give me an idea of how it works, and get a demo up and running so I can see if I want to pursue it further.

While looking around for screencasts (I often learn faster when watching someone code or explain it) I came across a free weekend for Code School. They had just launched their new backbone.js course and it sounded perfect, so I tried it out.

Code School screenshot

A screenshot of a challenge from the node.js course

Code School has been around since 2010, but I had never heard of it. Their about page describes them pretty well:

Code School is all about learning by doing. Our educational courses combine video, coding in the browser, and gamification principles to make learning more fun and therefore more effective. A typical course contains 5 levels, each with a 10-15 minute video, followed by series of code challenges a student must solve to make it to the next level. Once finished each course provides rewards for completion.

So you watch someone explain how the technology works, and then you have to solve small code challenges to continue. You can download the video and the slides if you'd like to review them later, and the code challenges start easy and get a little more difficult as you move on. They do a great job explaining things so it makes sense, and after an hour or two I had a pretty good understanding of backbone. I didn't end up an expert, but I knew enough to go out and dig deeper on my own.

They have about 15 courses right now, and it's only $25 a month. Check it out, I definitely recommend it.

phpQuery

This is the best thing ever. I just used it to scrape a website and it is fantastic, and saved me tons of time. It allows you to use jQuery style selectors in PHP. This is an example where Iâm scraping, but you could also change the page and add things before sending it out to the browser.

You install a PEAR package (you can also just copy the code, but this is the easiest way to keep it up to date):

        pear channel-discover phpquery-pear.appspot.com  
        pear install phpquery/phpQuery 
      

Then you include it in your code:

        require('phpQuery.php');
      

Hereâs an example of scraping form data from a CMS:



<?php
  // $pages is an array of page IDs that I scraped manually
  foreach($pages as $page_ID) {
    $source_html = file_get_contents('http://example.com/cms/?pg_id=' . $page_ID);
    $doc         = phpQuery::newDocumentHTML($source_html);
    $data        = (object)array(); // Create an empty object for our clean data
    $tags        = array();         // An array to hold checkbox values

    // Get all of the checked checkboxes on the page
    foreach(pq('input:checked') as $input) {
      $name   = $input->getAttribute('name');
      $value  = $input->getAttribute('value');
      $tags[] = $value;
    }


    // Get the selected text of all dropdowns
    foreach(pq('select') as $input) {
      $name        = $input->getAttribute('name');
      $data->$name = $doc->find('select[name=' . $name . '] option:selected')->text();
    }


    // Get the names and values of all input fields. Ignore checkboxes.
    foreach(pq('input') as $input) {
      $name  = $input->getAttribute('name');
      $value = $input->getAttribute('value');
      $type  = $input->getAttribute('type');

      if($type != 'checkbox') $data->$name = $value;
    }

    print_r($data); // This is your fancy new object with all of the form data you grabbed
    print_r($tags); // All of the checkbox values
    // TODO: Insert into the database here
    unset($data, $tags);   // Now flush it and do it all over again
  }