# Search for data in a table, from a CONTROLLER, and place result into a Variable for later display

**URL:** <https://discourse.cakephp.org/t/search-for-data-in-a-table-from-a-controller-and-place-result-into-a-variable-for-later-display/3092>\
**Category:** Need Help\
**Created:** [August 25, 2017, 12:24pm UTC](https://discourse.cakephp.org/t/search-for-data-in-a-table-from-a-controller-and-place-result-into-a-variable-for-later-display/3092 "2017-08-25T12:24:30Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![organul](https://avatars.discourse-cdn.com/v4/letter/o/82dd89/32.png) [@organul](https://discourse.cakephp.org/u/organul)\
**Post date:** [August 25, 2017, 12:24pm UTC](https://discourse.cakephp.org/t/search-for-data-in-a-table-from-a-controller-and-place-result-into-a-variable-for-later-display/3092/1 "2017-08-25T12:24:30Z")

</div>

Dear Saviours!

I am new to Cakephp. I am currently using Cakephp2 and I have the following issue;

**THE STORY**

In a Controller called GSTickets I have the code that prints PDF receipts.  
This receipts lack a piece of data.

The data wanted, is a value that I must search from within a table called TIME

There are 2 tables related to find this value:  
TIME  
TICKET\_BOOKING  
Ticket\_booking stores in each record a SERVICE\_ID which gets writen with the value of [TIME.id](http://TIME.id) once we save the ticket.  
The SQL query that would give me this value for a given Ticekt\_booking\_id = '10’  
would be this:

_SELECT time.suplemento FROM `time`,`tickets_booking` WHERE tickets\_booking.ticket\_id = ‘10’ AND tickets\_booking.service\_id=[time.id](http://time.id);_

**THE PROBLEM**

the guys that programmed this are not available, and I have to do the job.  
this controller has 3,000 lines and I do not know how to implement this change, and I tried hard.  
I get lost in variables and concepts and ways of quering the data base to pass the value to a variable to be able to write it in the pdf tickets.

I do not seem to integrate how to do this simple query in the cakephp 2 ways …

I am attching the controller piece of code that does this

Thank you in advance!  
Jordi.  
private function \_\_pdfTicketBookingInquilinios($ticket\_id){

```
    $conditions=array(
        'TicketInquilino.ticket_id' =>$ticket_id
    );
    
    $inquilinos=$this->_controller->TicketInquilino->find('all',
        array(
            'joins'=>array(
                
                array(
                        'table' => 'inquilino', 
                        'alias' => 'Inquilino', 
                        'type' => 'INNER', 
                        'conditions'=> array('Inquilino.id = TicketInquilino.inquilino_id')
                ),
                array(
                        'table' => 'tickets',   
                        'alias' => 'Ticket',    
                        'type' => 'INNER', 
                        'conditions'=> array('Ticket.id = TicketInquilino.ticket_id')
                ),
                array(
                        'table' => 'tickets_facturacion',   
                        'alias' => 'TicketFacturacion', 
                        'type' => 'INNER', 
                        'conditions'=> array('Ticket.id = TicketFacturacion.ticket_id')
                ),
                array(
                        'table' => 'tickets_clocks', 
                        'alias' => 'TicketClock',  
                        'type' => 'INNER', 
                        'conditions'=> array('Ticket.id = TicketClock.ticket_id')
                )
                
            ),
            'conditions' => $conditions ,
            'fields' => array('Ticket.*','Inquilino.*','TicketFacturacion.*','TicketClock.*'),
            'group' => array('TicketInquilino.id'),
        )
    );
    
    foreach($inquilinos as $row){
        // añado esto pero no funciona --- > $suplemento == $row['Time']['suplemento'];
        $this->__pdfCabecera($ticket_id);
        $this->__pdfRoom($ticket_id);
        
        $this->_pdf->Write(0, " \n");
        $this->_pdf->SetFont('courierB', '', 10, '', 'false');
        $this->_pdf->Write(0, 'Detalle', '', 0, 'L', true, 0, false, false, 0);
        $this->_pdf->SetFont('courierB', '', 10, '', 'false');
        $this->_pdf->Write(0, " \n");
        $style = array('width' => 0.1, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0));
        $this->_pdf->SetFont('courierB', '', 10, '', 'false');
        // get current vertical position
        $y = $this->_pdf->getY();
        $this->_pdf->Line(5, $y, $this->_pdf_width-5, $y, $style);
        
        $html ='<br/>
            <table width="100%" cellspacing="0" cellpadding="0" border="0">
            <tr>
                <th width="33%">Inquilino</th>
                <th width="33%">Tiempo</th>
                <th width="33%">Puntos</th>
            </tr>';
        $html .='<tr>
                    <td>' .$row['Inquilino']['apodo']. '</td>
                    <td>' .$row['TicketClock']['time']. '</td>
                    <td>' .$row['TicketFacturacion']['puntos_subtotal']/count($inquilinos) . '</td>
                </tr>';
    
        $html .='</table>';
        $this->_pdf->writeHTML($html, true, false, true, false, '');
        $this->_pdf->SetFont('courierB', '', 10, '', 'false');
        $this->_pdf->Write(0, 'Total : '.$row['TicketFacturacion']['puntos_subtotal']/count($inquilinos) .'','', 0, 'L', true, 0, false, false, 0);
        $this->__pdfCopia($row['Inquilino']['apodo']);
        //$sup = $this->request->data['Time']['service_id'];
                   
        $this->__pdfCopia('hola');
    }
    
}
```
