Welcome Guest, Not a member yet? Register   Sign In
What's a good technique for optional args?
#1

[eluser]stormbytes[/eluser]
So I'm writing a series of module-methods (Catalog_mod) which retrieve records from a database table (Assets). The default record set contains the entire catalog. That is, all the records in the Assets table. However, when displayed on a web page, each asset-record has a "category" field, a "manufacturer" field, and so forth. These fields are hyperlinked so that clicking on them will retrieve a limited recordset.

For instance, assuming the web page displays records for china. Clicking on "Perl" category will reload the page, showing only Assets of the "Perl" category. Similarly, clicking on a manufacturer-link will show a listing of Assets by that manufacturer.

The only difference between the query that retrieves ALL the records in the Assets table, and that which restricts the query to a particular value in a given field, is a "Where()" statement.

So.. Right now, my (general) query looks like this:

Code:
class Catalog_mod extends Model {

    function __construct() {
        parent::Model();
    }
    
    function get() {
      
      $q = $this->db->select("
                
                a.`asset_id` AS `cat`,
                a.`catalog_title` AS `cat_title`,
                a.`catalog_desc` AS `cat_desc`,
                cg.asset_category AS `category`
                
                ", FALSE);
            
      $q = $this->db->join('Asset_Categories cg','a.category_id = cg.asset_category_id','inner');
      
      $q = $this->db->get('Assets a');
      
      if( $q->num_rows() > 0 ) {
        
        foreach ( $q->result() as $row ) {
          $data[] = $row;
        }
        return $data;
      }
      
    }

Now as I said, the only difference between this query, which retrieves *all* records in the Assets table, and the query that'd fetch only those assets that conform to a specific category, is a 'where(field = value) clause. So rather then creating a series of methods, each repeating the entire query, I'm wondering if I could write a method that simply 'adds' the where() clause to the get() method above?

Alternatively, I thought about re-writing the above query with an optional "field = value" paramater, but I'm not entirely sure how to setup the default "select all" value, syntactically.

I'm pretty excited about this... Developing my first CI application. Frankly I'm really curious to see the suggestions that folks might post to contend with this situation Smile

Thanks in advance!
#2

[eluser]tonanbarbarian[/eluser]
couple ways you can do this

probably the easiest is to have an optional parameter to the get method that contains any where values

Code:
function get($where = array()) {
      
      $q = $this->db->select("
                
                a.`asset_id` AS `cat`,
                a.`catalog_title` AS `cat_title`,
                a.`catalog_desc` AS `cat_desc`,
                cg.asset_category AS `category`
                
                ", FALSE);
            
      $q = $this->db->join('Asset_Categories cg','a.category_id = cg.asset_category_id','inner');

      if (count($where))
            foreach ($where as $field=>$value)
                  $this->db->where($field,$value);
      
      $q = $this->db->get('Assets a');
      
      if( $q->num_rows() > 0 ) {
        
        foreach ( $q->result() as $row ) {
          $data[] = $row;
        }
        return $data;
      }
then when you call it
Code:
$this->catalog->get();
returns all the records
Code:
$this->catalog->get(array('a.category_id'=>1));
would return all of the records for category id 1
#3

[eluser]stormbytes[/eluser]
Wow.. very cool! Smile

How simple!

Gonna give that a short right now... Thanks for posting




Theme © iAndrew 2016 - Forum software by © MyBB