我在我正在构建的东西中遇到了一个问题。
我有两个表,一个是 Job Sheet 表,另一个是 Job Data 表。作业表表存储有关作业编号、工人等的信息。作业数据表存储作业编号(链接到作业表表)以及与之相关的作业等。例如:
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 |
----------------------------
我的问题是,当我将作业表打印到屏幕上时,我希望能够打印作业表的详细信息,然后为每个作业表打印相关数据,如下所示:
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
我的问题是,虽然我可以打印作业表详细信息(名称等),但它只会从数据库中返回一组记录,并将它们放入每个作业表中。因此,第一个工作表中的数据也会显示在第二个工作表中,即使它应该有自己的数据。
我在控制器中的代码是这样的:
$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);
}
在视图中,我有以下代码:
<?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 } } ?>
所以目前,它显示以下内容:
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
我能做些什么来解决这个问题?