<?php 

class polls_model extends CI_model

 {

   

	



		function get_polls($rec_no)

		{

		  $this->db->order_by("id", "desc"); 

		  $query=$this->db->get('poll',$rec_no,$this->uri->segment(3)); // select * from posts where module='$type'  limit $rec_no

		  

		  if($query->num_rows() > 0)

		    return $query->result(); 

		     return FALSE;



		}

	   function get_answers($poll_id)

		{

		$this->db->where('pollid',$poll_id);

		$query=$this->db->get('poll_answer'); 

		

		  if($query->num_rows() > 0)

		    return $query->result(); 

		     return FALSE;



		}



		

		function get_num_polls()

		{

		  $query=$this->db->get('poll'); // select * from posts where module='$type' 

		  if($query->num_rows() > 0)

		    return $query->num_rows(); 

		     return FALSE;

		}



		

		function delete_answer($id)

		{

		$this->db->where('pollid',$id);

		$qry =$this->db->delete('poll_answer');

		return $qry;

		}



		function add_new_poll($dt)

		{

	          $dat = array('name'=>$dt['poll']);

             $q=$this->db->insert('poll',$dat); 

             $id = $this->db->insert_id();

             

             $ans = array('pollid'=>$id,'answer'=>$dt['answer']);

             $q=$this->db->insert('poll_answer',$ans);

             

             $ans2 = array('pollid'=>$id,'answer'=>$dt['answer2']);

             $q=$this->db->insert('poll_answer',$ans2);

             

             $ans3 = array('pollid'=>$id,'answer'=>$dt['answer3']);

             $q=$this->db->insert('poll_answer',$ans3);

              if($dt['answer4'])

              {

             $ans4 = array('pollid'=>$id,'answer'=>$dt['answer4']);

             $q=$this->db->insert('poll_answer',$ans4); 

              }

              if($dt['answer5'])

              {         

             $ans5 = array('pollid'=>$id,'answer'=>$dt['answer5']);

             $q=$this->db->insert('poll_answer',$ans5);

             }

             



		}

		

		function update_poll($dt)

		{

	          $dat = array('name'=>$dt['poll']);

	          $this->db->where('id',$dt['poll_id']);

             $q=$this->db->update('poll',$dat);

             

             $ans = array('answer'=>$dt['answer']);

             $this->db->where('id',$dt['ans_id']);

             $q=$this->db->update('poll_answer',$ans);

             

             $ans2 = array('answer'=>$dt['answer2']);

             $this->db->where('id',$dt['ans_id2']);

             $q=$this->db->update('poll_answer',$ans2);

             

             $ans3 = array('answer'=>$dt['answer3']);

             $this->db->where('id',$dt['ans_id3']);

             $q=$this->db->update('poll_answer',$ans3);

             

             $ans4 = array('answer'=>$dt['answer4']);

             $this->db->where('id',$dt['ans_id4']);

             $q=$this->db->update('poll_answer',$ans4); 

                       

             $ans5 = array('answer'=>$dt['answer5']);

             $this->db->where('id',$dt['ans_id5']);

             $q=$this->db->update('poll_answer',$ans5);



		}

	

	



}

