2015-08-05 20 views
5

Tôi đang làm việc trên một chương trình đơn giản. Nếu tôi chèn hai giá trị, rupee và bạn cùng phòng tôi phải chia rupee/bạn cùng phòng.cách chia tổng số tiền trong số những người dùng trong codeigneter

Làm cách nào để nhận kết quả? Ví dụ tôi có 5 bạn cùng phòng và tôi nhập rupee = 500, và tôi muốn nó được hiển thị trên trang như 100phút 5 lần?

// 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> 
+0

hãy dán mã hoàn chỉnh của bạn như bộ điều khiển, mô hình và quan điểm thì tôi sẽ thay đổi điều đó và bạn sẽ nhận được câu trả lời trong sửa đổi vesion –

+0

tôi DID IT AS MODEL, CONTROLLER AND VIEW :) –

+0

bạn có thể tóm tắt điều này "vì vậy tôi nhận được 100 lần 5 lần trên trang của mình không?", Nơi bạn muốn hiển thị điều này, –

Trả lời

4

Tôi hy vọng điều này có thể giúp bạn, hãy hiểu mã và cố gắng chơi với nó để đáp ứng yêu cầu của bạn. Chúc may mắn!

Tệp điều khiển.

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'); 
} 

Tệp mẫu.

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> 

tham khảo: http://www.codeigniter.com/userguide2/tutorial/static_pages.html

+0

sir u có thể vui lòng xây dựng. tôi mới trong codeigneter. làm cách nào tôi có thể hiển thị dữ liệu đó trong chế độ xem. –

+0

Kiểm tra cập nhật của tôi. Cảm ơn! – DevBert

Các vấn đề liên quan