Php 将for每个子句添加到变量中
我有以下变量$prod_list_contents,它是html和php方法的混合体,用于显示数据库中的产品:Php 将for每个子句添加到变量中,php,Php,我有以下变量$prod_list_contents,它是html和php方法的混合体,用于显示数据库中的产品: $prod_list_contents .= '<div style="width:100%;">'; $prod_list_contents .= ''; while ($listing = tep_db_fetch_array($listing_query)) { $rows++; $prod_list_cont
$prod_list_contents .= '<div style="width:100%;">';
$prod_list_contents .= '';
while ($listing = tep_db_fetch_array($listing_query)) {
$rows++;
$prod_list_contents .= '
<div class="cat_prod_box">
<div class="image">
<div class="cat_image_table">
<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing['products_id']) . '">' . tep_image(DIR_WS_IMAGES_CAT . $listing['products_image'], $listing['products_name'], 255, 340, 'class="cat_image_round"') . '
<div class="hidden_thing">Click to View</div>
</a>
</div>
</div>
<div class="cat_product_info" >';
$prod_list_contents .= '<div class="span_style_num">Style: '. $listing['products_model'] . '</div>';
$prod_list_contents .= '<div class="span_colors">Colors: red, blue</div>';
$prod_list_contents .= '<div class="span_product_name"><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing['products_id']) . '">' . $listing['products_name'] . '</div></a>';
$prod_list_contents .= '<div class="span_price">CAD ' .$currencies->display_price($listing['products_price'], tep_get_tax_rate($listing['products_tax_class_id'])) .'</div>' ;
$prod_list_contents .= '</div></div>';
}
$prod_list_contents .= ' </div>' .
' '
;
echo $prod_list_contents;
$prod\u list\u内容='';
$prod_列表_内容='';
while($listing=tep\u db\u fetch\u数组($listing\u query)){
$rows++;
$prod_列表_内容。='
';
$prod_list_contents.='Style:'。$listing['products_model'].';
$prod_列表_内容='颜色:红色、蓝色';
$prod_列表_内容='';
$prod_list_contents.='CAD'.$CUMENTERS->显示价格($listing['products_price'],tep_get_tax_rate($listing['products_tax_class_id'))。';
$prod_列表_内容='';
}
$prod_列表_内容=''。
' '
;
echo$prod_list_内容;
我试图将颜色表合并到通过同一变量显示的同一个div中。我的颜色表是通过以下php代码派生的:
<div >
<?php
$ctr = 0;
$clr=1;
foreach($products_options_array as $products_options_array2) {
$ctr++;
//if it is a color image, or a color hex
if($products_options_array2['color_image']!='')
{
$clr_sec = "style=\"background-image:url(images/pattern/".$products_options_array2['color_image'].") !important; height:28px; width:28px;\"" . "class=\"testy\"";
}
else {
$clr_sec = "style=\"background-color:".$products_options_array2['color_code']." !important; height:28px; width:28px;float:left;\"" . "class=\"testy\"";
}
?>
<div <?php echo $clr_sec;?>> </div>
<?php
$clr++;
} ?>
</div>
我已经尝试过将它添加到prod_list_contents变量中,但我认为我做不到,因为这里的php不是方法。需要分号等等。还有别的方法吗?还是我可以把它加进去,我只是在装傻?如果我正确理解了你的问题,你想在$prod\u列表内容中添加$clr\u sec 换衣服
<div <?php echo $clr_sec;?>> </div>
嘿,谢谢。你确实理解对了。但是因为没有for each循环,所以我只得到$clr_sec变量echo'dwhat中的最后一种颜色?只需将$prod_list_内容放在foreach循环之后。另外,尽量不要将太多代码与HTML混合使用。这确实是一个糟糕的做法。我尝试在for each循环之后添加它,但是我认为for each循环在那时已经“完成”运行,因此$clr_sec达到最终状态。我还尝试将while循环包装到foreach循环中,但没有成功。请使用PHP变量存储有关产品的信息,但不要将所有HTML放在一个变量中。当您从数据库中检索值时,只需回显所需的HTML即可。
$prod_list_contents.='<div '.$clr_sec.'</div>';