Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to divide total money among the users in codeigneter

I am working on one simple program. If I insert two values, rupees and roommate I have to divide rupees/roommate.

How will I get the result? For example I have 5 roommate and I input rupees = 500, and I want it to be display on the page like 100rupee 5times?

// THIS IS THE MODEL
public function insertModel()
{
         $data = array(
                'm_rupee' => $this->input->post('m_rupee'),
                'rommmate' => $this->input->post('roommate')
         );
         $this->db->insert('tbl_money',$data);
                  return;
}


// THIS IS THE CONTROLLER
public function insertDate()
{
       $this->MoneySplitter->insertModel('$data');
       redirect('MoneySpliterController');
} 


// THIS IS THE VIEW
<form method="post" action="<?php echo base_url();>index.php/MoneySpliterController/insertDate">
         <input type="text" name="m_rupee" />
         <input type="text" name="rommmate" />
         <input type="submit" value="save">
</form>
like image 583
bhargav Patel Avatar asked Nov 10 '22 07:11

bhargav Patel


1 Answers

I hope this could help you, please understand the code and try to play with it to suite your requirement. Good luck!

Controller File.

public function index(){
   $data["info"] = $this->mModel->getData();
   $this->load->view('index.php', $data);//$data is the data you want to pass to the view.
}

public function insertData()
{
  $amount= $this->input->post('m_rupee'); //500 rupee
  $roommates = count($this->input->post('roommate')); //5 roommates with data.
  $amountPerPerson = $amount / $roommates; //100 rupee per roomate.

  //Iterate the number of roommates.
  foreach($roommates as $row){
    //Call the function in model that will save the data.
    $this->mModel->insertModel($amountPerPerson, $row);
  }
  redirect('MoneySpliterController'); 
}

Model File.

public function insertModel($amount, $data){
  $tempData = array(
    'm_rupee' => $amount,
    'roommate' => $data 
  );

  $this->db->insert('tbl_money', $tempData);
}

public function getData(){
  return $this->db->get('tbl_money')->result();
}

index.php

<div><?php var_dump($info); ?></div>

Reference:http://www.codeigniter.com/userguide2/tutorial/static_pages.html

like image 149
DevBert Avatar answered Nov 14 '22 21:11

DevBert