Welcome Guest, Not a member yet? Register   Sign In
Post your controller!
#7

[eluser]t'mo[/eluser]
Here's the code behind something I threw together a few weeks ago: http://didyoucry.com/clshop (yes, I need to get this it's own domain name instead of piggy-backing off another old project of mine).

It's more-or-less a tool that automated the way I look for stuff to buy on craigslist: 1) search for X in my town; if not found in my town, 2) search for X in the next town over by typing in the next town's name in the url.

Code:
<?php

class Site extends Controller
{
  function index() {
    $this->load->view('pageTemplate',
      array('content' => $this->load->view('queryForm', null, true)));
  }

  function search() {
    $query = $this->input->post('query');
    $cities = array();

    if (!empty($query)) {
      $this->load->model('Lookup');

      $zip = $this->input->post('zip');
      if (!empty($zip)) {
        $cities = $this->Lookup->citiesNearZip($zip, $this->input->post('radius'));
      }
      else {
        $cities = $this->Lookup->cities();
      }
    }

    $data = array(
      'cities' => $cities,
      'query' => $query,
      'hasPic' => $this->input->post('hasPic'));
    $cityList = $this->load->view('cityList', $data, true);
    $this->load->view('pageTemplate', array('content' => $cityList));
  }
}

Some notes on my "style":
- database calls do not belong in the Controller
- HTML does not belong in the Controller
- deploy application code up and out webroot; the "if (! defined('BASEPATH'))..." stuff just adds visual clutter
- I'm picky about placement of curly braces, blank lines, spaces, etc.
- too many comments, especially a play-by-play sportscaster-style callout of every f@!#$# little thing really annoys me; the code ought to be clear enough to speak for itself

One critique I have of my own code here is that I'm not validating the input, which I should; look for that in v0.5, I guess. :-) (I should also put in a copyright notice, though I have that in the HTML; I'm lazy) And let me know your critiques of this code too.

Update: mini-rant about comments, copyright notices


Messages In This Thread
Post your controller! - by El Forum - 08-30-2009, 12:19 AM
Post your controller! - by El Forum - 08-30-2009, 01:36 AM
Post your controller! - by El Forum - 08-30-2009, 07:19 AM
Post your controller! - by El Forum - 08-30-2009, 12:31 PM
Post your controller! - by El Forum - 08-30-2009, 12:44 PM
Post your controller! - by El Forum - 08-30-2009, 08:59 PM
Post your controller! - by El Forum - 08-30-2009, 10:07 PM
Post your controller! - by El Forum - 08-31-2009, 02:13 AM
Post your controller! - by El Forum - 08-31-2009, 03:13 AM
Post your controller! - by El Forum - 08-31-2009, 03:19 AM
Post your controller! - by El Forum - 08-31-2009, 04:43 AM
Post your controller! - by El Forum - 08-31-2009, 04:49 AM
Post your controller! - by El Forum - 09-01-2009, 01:05 AM
Post your controller! - by El Forum - 09-01-2009, 09:30 PM
Post your controller! - by El Forum - 09-02-2009, 02:42 AM
Post your controller! - by El Forum - 09-02-2009, 05:34 AM
Post your controller! - by El Forum - 09-02-2009, 09:02 AM
Post your controller! - by El Forum - 09-02-2009, 09:43 AM
Post your controller! - by El Forum - 09-02-2009, 10:13 AM
Post your controller! - by El Forum - 09-02-2009, 01:13 PM
Post your controller! - by El Forum - 09-03-2009, 06:40 AM
Post your controller! - by El Forum - 11-03-2009, 10:20 PM
Post your controller! - by El Forum - 11-03-2009, 11:02 PM
Post your controller! - by El Forum - 11-03-2009, 11:33 PM
Post your controller! - by El Forum - 11-04-2009, 12:09 AM
Post your controller! - by El Forum - 11-04-2009, 03:36 AM
Post your controller! - by El Forum - 11-04-2009, 07:14 AM



Theme © iAndrew 2016 - Forum software by © MyBB