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>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With