CodeIgniter Forums
how to create funciton or class or??? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: how to create funciton or class or??? (/showthread.php?tid=13720)



how to create funciton or class or??? - El Forum - 12-02-2008

[eluser]li9ht[/eluser]
ive been using lots of query to do this

Code:
$sql5=$this->db->select_sum('total','total');
      $sql5=$this->db->where('date',"2008-11-22");
      $d22=$this->db->get('sale',$sql5);
      $data['d22']=$d22->row();
      
      $sql6=$this->db->select_sum('total','total');
      $sql6=$this->db->where('date',"2008-11-23");
      $d23=$this->db->get('sale',$sql6);
      $data['d23']=$d23->row();
      
      
      $sql7=$this->db->select_sum('total','total');
      $sql7=$this->db->where('date',"2008-11-29");
      $d29=$this->db->get('sale',$sql7);
      $data['d29']=$d29->row();  
      
      $sql8=$this->db->select_sum('total','total');
      $sql8=$this->db->where('date',"2008-11-30");
      $d30=$this->db->get('sale',$sql8);
      $data['d30']=$d30->row();
      
      $sql9=$this->db->select_sum('total','total');
      $sql9=$this->db->where('date',"2008-12-06");
      $d06=$this->db->get('sale',$sql9);
      $data['d06']=$d06->row();
      
      $sql10=$this->db->select_sum('total','total');
      $sql10=$this->db->where('date',"2008-12-07");
      $d07=$this->db->get('sale',$sql10);
      $data['d07']=$d07->row();

the only thing changing is the date. can i create a function and just send the date to the function/class and insert in my index function .. how do it do it.?


how to create funciton or class or??? - El Forum - 12-02-2008

[eluser]JoostV[/eluser]
[EDIT] removed a superfluous or_where

Say you do you query in the index() method of your controller. Then you would get this.
Code:
function index() {
    // Set up the dates to query
    $dates = array("2008-11-22", "2008-12-06", "2008-11-30", "2008-11-29", "2008-11-23");
    // Get results
    $data = $this->_date_query($dates)
}

function _date_query($dates) {
    // Query database
    $this->db->select_sum('total','total');
    foreach($dates as $date) {
        // Set a where statement for each date
        $this->db->or_where('date', $date);
    }
    $query = $this->db->get('sale',$sql7);
    
    // Return results
    if($query->num_rows() > 0){
        return $query->result_array();
    }
    else {
        return false;
    }
}

You had best move the _date_query($dates) function to a model, bacause it handels db actions. (remove the underscore from the function name, in that case.)


how to create funciton or class or??? - El Forum - 12-02-2008

[eluser]li9ht[/eluser]
ok thx.. im trying it.