简体   繁体   中英

Unable to retrieve results from database using a variable gained from a query in CakePHP 2

I've hit upon a problem in something I'm building.

I have two tables, one is a Job Sheet table and another one is a Job Data table. The Job Sheet table stores information regarding the job number, worker etc. The Job Data table stores the job number (which is linked to the Job Sheet table) as well as jobs etc relating to it. So for example:

Job Sheet
---------------------------
| ID | Jobnum |   Worker  |
---------------------------
| 1  |  1234  | J.Bloggs  |
| 2  |  5678  | J.Smith   |
---------------------------

Job Data
----------------------------
| ID | Jobnum |    Work    |
----------------------------
| 1  |  1234  | Light bulb |
| 2  |  1234  | Painting   |
| 3  |  1234  | Decorating |
| 4  |  5678  | Wood Work  |
| 5  |  5678  | Cleaning   |
----------------------------

My problem is that when I go to print the Job Sheet out on to the screen, I want to be able to print the details of the Jobsheet, and then for each job sheet print it's related data, like this:

Job Sheets To Be Printed
--------------------------------------------------------

Job Sheet ID - 1
Job Number - 1234
Worker - J.Bloggs

Work Undertaken
---------------
Light Bulb
Painting
Decorating

***********

Job Sheet ID - 2
Job Number - 5678
Worker - J.Smith

Work Undertaken
---------------
Wood Work
Cleaning

My problem is that, while I can print the Job Sheet details (name etc), it only ever comes back with the one set of records from the database, and puts them in to each job sheet. So the data from the first Job sheet is also displayed in the second job sheet, even though it should have it's own data.

My code in the Controller is this:

    $jobsheets = $this->Jobsheet->find('all', array('conditions' => array('Jobsheet.contract' => $contractid), 'recursive' => 2));
    $this->set('jobsheets', $jobsheets);

    foreach ($jobsheets as $js) {

        $jobnum[] = $js['Jobsheet']['jobnum'];

    }

    print_r($jobnum);

    foreach ($jobnum as $jn) {

        // Job Data
        $jobrec = $this->Jobdata->find('all', array('conditions' => array('Jobdata.jobsheetid' => $jn), 'recursive' => 2));
        $this->set('jobrec', $jobrec);

    }       

And in the view, I have the following code:

        <?php foreach ($jobsheets as $jobsheet) { ?>
            <p>Job Sheet ID - <?php echo $jobsheet['Jobsheet']['id']; ?></p>
            <p>Job Number - <?php echo $jobsheet['Jobsheet']['jobnum']; ?></p>
            <p>Worker - <?php echo $jobsheet['Jobsheet']['worker']; ?></p>
            <p>Work Undertaken</p>
            <?php foreach ($jobrec as $jb) { ?>
                <p><?php echo $jb['Jobdata']['work'];?></p>
        <?php } } ?>

So at the moment, it's displaying the following:

Job Sheets To Be Printed
--------------------------------------------------------

Job Sheet ID - 1
Job Number - 1234
Worker - J.Bloggs

Work Undertaken
---------------
Light Bulb
Painting
Decorating

***********

Job Sheet ID - 2
Job Number - 5678
Worker - J.Smith

Work Undertaken
---------------
Light Bulb
Painting
Decorating

What could I do to fix this?

make this change in control

 foreach ($jobnum as $jn) {

            // Job Data
            $jobrec[] = $this->Jobdata->find('all', array('conditions' => array('Jobdata.jobsheetid' => $jn), 'recursive' => 2));
            $this->set('jobrec', $jobrec);

        } 

and this change in view

 <?php $i=0; ?>
    <?php foreach ($jobsheets as $jobsheet) { ?>

                <p>Job Sheet ID - <?php echo $jobsheet['Jobsheet']['id']; ?></p>
                <p>Job Number - <?php echo $jobsheet['Jobsheet']['jobnum']; ?></p>
                <p>Worker - <?php echo $jobsheet['Jobsheet']['worker']; ?></p>
                <p>Work Undertaken</p>
                <?php foreach ($jobrec[$i] as $jb) { ?>
                    <p><?php echo $jb['Jobdata']['work'];?></p>
            <?php }
           $i++;
 } ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM