$address_query = db_query('......');
$address1 ='';
$address2 ='';
$address3 ='';
$address4 ='';
$address5 ='';
$state = '';
$pincode ='';
$addcategory ='';
$residence ='';
$adddaterpt ='';
foreach($address_query as $adds)
{
$address1 .= $adds->addressline1;
$address2 .= $adds->addressline2;
$address3 .= $adds->addressline3;
$address4 .= $adds->addressline4;
$address5 .= $adds->addressline5;
$state .= $adds->state_name;
$pincode .= $adds->pincode;
$addcategory .= $adds->addcategory_name;
$residence .= $adds->residence_name;
$adddaterpt .= $adds->date_reported;
$addrptdate = substr($adddaterpt, 0, 2) . "-" . substr($adddaterpt, 2, 2) . "-" . substr($adddaterpt, 4, 4);
$report.='<tr>
<table id="address1">
<tr><b>ADDRESS:</b>' . $address1 . $address2 . $address3 . $address4 . $address5 . $state . $pincode . '</tr>
<tr><td><b>CATEGORY:</b>' . $addcategory . '</td>
<td><b>RESIDENCE CODE:</b>' . $residence . '</td>
<td><b>DATE REPORTED:</b>' . $addrptdate . '</td>
</tr>
</table>
</tr>';
}
$report.='</table>';
执行此代码时,我得到了foreach()的最后一个值。其他值不会显示给我。
它在$ address_query中包含4行,但最后一行只显示。
请建议我,我的代码中有任何错误。
答案 0 :(得分:2)
你好朋友实际上你不能在tr标签中使用表标签,并且你没有声明直接用于for-each语句的变量$ report。
试试此代码
$address_query = db_query('......');
$adddaterpt ='';
$report = '<table>';
foreach($address_query as $adds)
{
$adddaterpt .= $adds->date_reported;
$addrptdate = substr($adddaterpt, 0, 2) . "-" . substr($adddaterpt, 2, 2) . "-" . substr($adddaterpt, 4, 4);
$report.='<tbody id="address1">
<tr><td><b>ADDRESS:</b>' . $adds->addressline1 . $adds->addressline2 . $adds->addressline3 . $adds->addressline4 . $adds->addressline5 . $adds->state_name . $adds->pincode . '</td></tr>
<tr><td><b>CATEGORY:</b>' . $adds->addcategory_name . '</td>
<td><b>RESIDENCE CODE:</b>' . $adds->residence_name . '</td>
<td><b>DATE REPORTED:</b>' . $addrptdate . '</td>
</tr>
</tbody>';
}
$report.='</table>';
答案 1 :(得分:0)
我并非100%确定我理解这个问题,但问题可能在于您的连接问题。
在当前代码中,每个循环继续添加到以下变量中。
$address1
$address2
$address3
$address4
$address5
$state
$pincode
$addcategory
$residence
$adddaterpt
因此,在循环结束时,每个变量都堆叠有来自每个先前迭代的信息。
您需要为每个循环重置变量,或者如下例所示,完全删除连接。所以:
foreach($address_query as $adds) {
$address1 = $adds->addressline1;
$address2 = $adds->addressline2;
$address3 = $adds->addressline3;
$address4 = $adds->addressline4;
$address5 = $adds->addressline5;
$state = $adds->state_name;
$pincode = $adds->pincode;
$addcategory = $adds->addcategory_name;
$residence = $adds->residence_name;
$adddaterpt = $adds->date_reported;
$addrptdate = substr($adddaterpt, 0, 2) . "-" . substr($adddaterpt, 2, 2) . "-" . substr($adddaterpt, 4, 4);
}
希望这有帮助!如果没有,请更清楚你的问题。 :)
答案 2 :(得分:0)
you can do it dirctly as well why u r storing the data in variable just directly use it you are in foreach loop already...
foreach($address_query as $adds)
{
$report.='<tr>
<table id="address1">
<tr><b>ADDRESS:</b>' . $adds->addressline1. $adds->addressline2 . $adds->addressline3 . $adds->addressline4 . $adds->addressline5 . $adds->state_name . $adds->pincode . '</tr>
</table>
</tr>';
}