<table width="503" height="278" border="1">
<tr align="center" valign="middle">
<td height="34" align="center" valign="middle" id="side"><strong>TEST</strong></td>
<td align="center" valign="middle"><strong>METHOD</strong></td>
<td align="center" valign="middle"><strong>COMPEDIA</strong></td>
<td align="center" valign="middle"><strong>SPECIFICATION</strong></td>
<td align="center" valign="middle"><strong>DETERMINED</strong></td>
<td align="center" valign="middle" id="side"><strong>REMARKS</strong></td>
</tr>
<?php foreach ($tests_requested_display2 as $value2) ?><!--has values to populate the compedia and specification cells of the table-->
<?php foreach ($tests_requested_display as $value) : ?> <!-- creates 3 dynamic rows-->
<tr>
<td height="56" align="center" valign="middle" id="side"><?php echo $value->name ?></td>
<td align="center" valign="middle">Weight</td>
<td align="center" valign="middle"><textarea name="compedia[]" cols="10" ><?php echo @$value2->compedia;?></textarea></td>
<td align="center" valign="middle"><textarea name="specification[]" cols="10"><?php echo @$value2->specification;?></textarea></td>
<td align="center" valign="middle">None Deviate</td>
<td align="center" valign="middle" id="side">COMPLIES</td>
</tr>
<?php endforeach; ?>
</table>
带$value
的第一个foreach数组包含3个值,这些值创建一个如上定义的表,但有3个和6个列。我有第二个foreach数组$ value2有数据填充compedia和规范列,如果我回显$value2->compedia
和$value->specification
,它只返回六个值中数组中的最后一个值,如果我添加end foreach与$value
数组相同,它返回一个奇怪的结果。我错过了什么?
更多信息:
print_r($tests_requested_display)
返回:
Array ( [0] => stdClass Object ( [name] => Dissolution ) [1] => stdClass Object ( [name] => Assay ) [2] => stdClass Object ( [name] => Uniformity of Weight ) ) Array ( [0] => stdClass Object ( [name] => Dissolution ) [1] => stdClass Object ( [name] => Assay ) [2] => stdClass Object ( [name] => Uniformity of Weight ) ) Array ( [0] => stdClass Object ( [name] => Dissolution ) [1] => stdClass Object ( [name] => Assay ) [2] => stdClass Object ( [name] => Uniformity of Weight ) )
和print_r($tests_requested_display2)
返回:
Array ( [0] => stdClass Object ( [name] => Assay [compedia] => Caps [specification] => Tabs ) [1] => stdClass Object ( [name] => Dissolution [compedia] => Tabs [specification] => Caps ) [2] => stdClass Object ( [name] => Uniformity of Weight [compedia] => TabsCaps [specification] => CapsTabs ) ) Array ( [0] => stdClass Object ( [name] => Assay [compedia] => Caps [specification] => Tabs ) [1] => stdClass Object ( [name] => Dissolution [compedia] => Tabs [specification] => Caps ) [2] => stdClass Object ( [name] => Uniformity of Weight [compedia] => TabsCaps [specification] => CapsTabs ) ) Array ( [0] => stdClass Object ( [name] => Assay [compedia] => Caps [specification] => Tabs ) [1] => stdClass Object ( [name] => Dissolution [compedia] => Tabs [specification] => Caps ) [2] => stdClass Object ( [name] => Uniformity of Weight [compedia] => TabsCaps [specification] => CapsTabs ) )
我期望的结果作为一个例子:
<table width="503" height="278" border="1">
<tr align="center" valign="middle">
<td height="34" align="center" valign="middle" id="side"><strong>TEST</strong></td>
<td align="center" valign="middle"><strong>METHOD</strong></td>
<td align="center" valign="middle"><strong>COMPEDIA</strong></td>
<td align="center" valign="middle"><strong>SPECIFICATION</strong></td>
<td align="center" valign="middle"><strong>DETERMINED</strong></td>
<td align="center" valign="middle" id="side"><strong>REMARKS</strong></td>
</tr>
<tr>
<td height="56" align="center" valign="middle" id="side">Assay</td>
<td align="center" valign="middle">Weight</td>
<td align="center" valign="middle"><textarea name="compedia[]" cols="10">Caps</textarea></td>
<td align="center" valign="middle"><textarea name="specification[]" cols="10">Tabs</textarea></td>
<td align="center" valign="middle">None Deviate</td>
<td align="center" valign="middle" id="side">COMPLIES</td>
</tr>
<tr>
<td height="56" align="center" valign="middle" id="side">Dissolution</td>
<td align="center" valign="middle">Weight</td>
<td align="center" valign="middle"><textarea name="compedia[]" cols="10">TabsCaps</textarea></td>
<td align="center" valign="middle"><textarea name="specification[]" cols="10">CapsTabs</textarea></td>
<td align="center" valign="middle">None Deviate</td>
<td align="center" valign="middle" id="side">COMPLIES</td>
</tr>
<tr>
<td height="56" align="center" valign="middle" id="side">Uniformity</td>
<td align="center" valign="middle">Weight</td>
<td align="center" valign="middle"><textarea name="compedia[]" cols="10">Tabs</textarea></td>
<td align="center" valign="middle"><textarea name="specification[]" cols="10">Caps</textarea></td>
<td align="center" valign="middle">None Deviate</td>
<td align="center" valign="middle" id="side">COMPLIES</td>
</tr>
</table>
答案 0 :(得分:0)
所以语法对我来说有点怀疑,但请记住,我对PHP很新,所以它可能没问题。但是,这里有一个代码的修改版本,可以为您解决问题:
<?php foreach ($tests_requested_display2 as $value2) { ?>
<?php foreach ($tests_requested_display as $value) { ?>
<tr>
<td height="56" align="center" valign="middle" id="side"><?=$value->name?></td>
<td align="center" valign="middle">Weight</td>
<td align="center" valign="middle"><textarea name="compedia[]" cols="10" ><?=@$value2->compedia?></textarea></td>
<td align="center" valign="middle"><textarea name="specification[]" cols="10"><?=@$value2->specification?></textarea></td>
<td align="center" valign="middle">None Deviate</td>
<td align="center" valign="middle" id="side">COMPLIES</td>
</tr>
<?php } ?>
<?php } ?>
但它仍然无法解决这个问题:
如果我回显$ value2-&gt; compedia和$ value-&gt;规范,它只返回六个值中数组中的最后一个值
因为在迭代内部列表的所有三个值之前不会迭代该列表。因此,从这个角度来看,你可以交换循环,你会得到三组六行,或你可以包装td
标签,并为你想要的六个值构建正确的字符串值。目前还不清楚根本你真正期待的事情。