2015-01-12 2 views
1

안녕하십니까, 우선 모든 포럼에 친절한 인사를 전합니다. 나는 에 문제가있어 도움이 필요합니다. 제게는 정보, 텍스트, 일러스트레이션을 도와 케이크를 만들 때 을 수행 할 때 문제가되는 것은 아직 초보자이고 조금은 조금 더 이것이 더 복잡합니다. 내보기cakephp에 저장 한 후 입력 필드의 합을 계산하는 방법은 무엇입니까?

폼보기 :

http://www.subirimagenes.com/imagedata.php?url=http://s2.subirimagenes.com/otros/9234902vistaprevia.jpg

예 :

형태 : 3 * 8,250 = 24,750 01,235 16,형태 : 4 * 5.50 = 22 형태 : 1 * 4.90 = 4.90 형태 : 총 2 * 12.90 = 25.8

는 : 72.50

내가 뭘 원하는 작성된 금액을하다 양식에 다음 에게 총 필드

DB보기

012,351,641의 계산과 장소를 할

http://www.subirimagenes.com/imagedata.php?url=http://s2.subirimagenes.com/otros/9234906db.jpg

----------보기/add.ctp .........

<div class="ventas form"> 
    <?php echo $this->Form->create('Venta'); ?> 
    <fieldset> 
    <legend><?php echo __('PROCESO DE COMPRA'); ?></legend> 
    <?php 
    echo $this->Form->input('nombre'); 
    echo $this->Form->input('apellido'); 
    echo $this->Form->input('cedula'); 
    echo $this->Form->input('direccion'); 
    echo $this->Form->input('mail'); 
    echo $this->Form->input('telefono'); 
    echo $this->Form->input('tarjeta'); 
    echo $this->Form->input('numtarjeta'); 
    echo __('<legend>SELECCIONE SU PELICULA</legend>'); 
    echo $this->Form->input('cartelera_id',array('label' => 'Seleccione su pelicula')); 
    echo $this->Form->input('cant_adulto', array('label' => 'Cantidad de boletos - Precio normal $ 7,50')); 
    echo $this->Form->input('cant_niño', array('label' => 'Cantidad de boletos - Precio niños/ancianos $ 5,50')); 
    echo $this->Form->input('cant_discapacitado', array('label' => 'Cantidad de boletos - Precio discapacitados $ 4,90')); 

echo __('<legend>SELECCIONE SU COMBO</legend>'); 
    echo $this->Form->input('combo_id'); 
    echo $this->Form->input('numcombo', array('label' => 'Cantidad de combos - Precio discapacitados $ 12,90')); 
    echo $this->Form->input('total'); 
    ?> 
    </fieldset> 

<?php echo $this->Form->end(__('Guardar')); ?> 

</div> 

....... 컨트롤러 .... ...

class VentasController extends AppController { 
/** * Components 
* * @var array 
*/ public $components = array('Paginator'); 
/** * index method 
* * @return void 
*/ 
    public function index() { 
     $this->Venta->recursive = 0; 
     $this->set('ventas', $this->Paginator->paginate()); 
    }/** 
* view method 
* 
* @throws NotFoundException 
* @param string $id 
* @return void 
*/ 
    public function view($id = null) { 
     if (!$this->Venta->exists($id)) { 
      throw new NotFoundException(__('Invalid venta')); 
     } 
     $options = array('conditions' => array('Venta.' . $this->Venta->primaryKey => $id)); 
     $this->set('venta', $this->Venta->find('first', $options)); 
    } 
/** 
* add method * 
* @return void */ 
    public function add() { 
     if ($this->request->is('post')) { 
      $this->Venta->create(); 
      if ($this->Venta->save($this->request->data)) { 
       $this->Session->setFlash(__('La venta se almacenó correctamente.')); 
       return $this->redirect(array('action' => 'index')); 
      } else { 
       $this->Session->setFlash(__('La venta no se almacenó correctamente. Por favor, Intente de nuevo.')); 
      } 
     } 
     $carteleras = $this->Venta->Cartelera->find('list',array('fields'=>'pelicula')); 
     $this->set(compact('carteleras')); 
     $combos = $this->Venta->Combo->find('list',array('fields'=>'nombre')); 
     $this->set(compact('combos')); 
    } 
/** * edit method 
* * @throws NotFoundException 
* @param string $id 
* @return void 
*/ public function edit($id = null) { 
     if (!$this->Venta->exists($id)) { 
      throw new NotFoundException(__('Invalid venta')); 
     } 
     if ($this->request->is(array('post', 'put'))) { 
      if ($this->Venta->save($this->request->data)) { 
       $this->Session->setFlash(__('La venta se almacenó correctamente.')); 
       return $this->redirect(array('action' => 'index')); 
      } else { 
       $this->Session->setFlash(__('La venta no se almacenó correctamente. Por favor, Intente de nuevo.')); 
      } 
     } else { 
      $options = array('conditions' => array('Venta.' . $this->Venta->primaryKey => $id)); 
      $this->request->data = $this->Venta->find('first', $options); 
     } 
     $carteleras = $this->Venta->Cartelera->find('list'); 
     $this->set(compact('carteleras')); 
     $combos = $this->Venta->Combo->find('list'); 
     $this->set(compact('combos')); 
    } 
/** 
* delete method * 
* @throws NotFoundException 
* @param string $id 
* @return void 
*/ public function delete($id = null) { 
     $this->Venta->id = $id; 
     if (!$this->Venta->exists()) { 
      throw new NotFoundException(__('Invalid venta')); 
     } 

귀하의 관심

답변

0
public function vent() { 
    if ($this->request->is('post')) { 
     $this->Venta->create(); 
     $this->request->data['Venta']['total'] = ($this->request->data['Venta']   
     ['cant_adulto'] * 7.5) + ($this->request->data['Venta']['cant_niño'] * 
     5.5) + ($this->request->data['Venta']['cant_discapacitado'] * 4.9)+ 
     ($this->request->data['Venta']['numcombo'] * 12.9) ; 
    if ($this->Venta->save($this->request->data)) { 

    $this->Session->setFlash(__('La venta se almacenó correctamente.')); 
    $inserted_id=$this->Venta->id; 
    return $this->redirect(array('action' => 'view', $inserted_id)); 
    } else { 
    $this->Session->setFlash(__('La venta no se almacenó correctamente. Por 
    favor, Intente de nuevo.')); 
    }} 
    $carteleras = $this->Venta->Cartelera- 
       >find('list',array('fields'=>'pelicula')); 
    $this->set(compact('carteleras')); 
    $combos = $this->Venta->Combo->find('list',array('fields'=>'nombre')); 
    $this->set(compact('combos')); 
} 
0

를 사용하여 모델의 beforeSave() 콜백 주셔서 대단히 감사합니다. 거기에서 계산을하고 현장에 써라. 이 질문에 대답하기위한

Example taken from the book:

public function beforeSave($options = array()) { 
    if (!empty($this->data['Event']['begindate']) && 
     !empty($this->data['Event']['enddate']) 
    ) { 

     $this->data['Event']['begindate'] = $this->dateFormatBeforeSave(
      $this->data['Event']['begindate'] 
     ); 
     $this->data['Event']['enddate'] = $this->dateFormatBeforeSave(
      $this->data['Event']['enddate'] 
     ); 
    } 
    return true; 
} 

public function dateFormatBeforeSave($dateString) { 
    return date('Y-m-d', strtotime($dateString)); 
} 
+0

감사합니다. 도와 주시겠습니까? beforesave() 기능을 시도하지만 논리를 이해하지 못해서 4 일 동안이 작업을 해보려고했습니다. –

+0

설명서 및 제공된 예제에서 이해할 수없는 것은 무엇입니까? 계산을하고 전체 필드에 결과를 설정하면 완료됩니다. 예제 코드는 날짜를 변경하는 것과 정확히 일치합니다. – burzum

관련 문제