我正在构建一些问题。
我有两个表,一个是工作表,另一个是工作数据表。工作表存储有关工作号,工人等的信息。工作数据表存储工作号(链接到工作表表)以及与之相关的工作等。例如:
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
我的问题是,虽然我可以打印工作表详细信息(名称等),但它只返回数据库中的一组记录,并将它们放入每个工作表中。因此,第一个工作表中的数据也会显示在第二个工作表中,即使它应该拥有自己的数据。
我在Controller中的代码是:
$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
我该怎么做才能解决这个问题?
答案 0 :(得分:1)
在控件中进行此更改
foreach ($jobnum as $jn) {
// Job Data
$jobrec[] = $this->Jobdata->find('all', array('conditions' => array('Jobdata.jobsheetid' => $jn), 'recursive' => 2));
$this->set('jobrec', $jobrec);
}
此视图中的更改
<?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++;
} ?>