我正在尝试为我的网站编写一个购物车,我想显示一个表格,每个项目都有一行,包括部分#,名称,数量,价格和小图片。除了图像之外,一切都显示得很好,图像出现在桌子的外面(上面),而不是放在我放置它的单元格中。这是代码:
//create a table for the items in the cart
print("<table class='cart_table'>");
print("<tr class='first_row'>
<td> Item No. </td>
<td> Item Name </td>
<td> Quantity </td>
<td> Price </td>
<td> Image </td>
</tr>");
//for each item in the cart
foreach ($_SESSION['cart'] as $value) {
//create a new product object for the item
$product = new LG_Product($_SESSION['cart'][$i]['item']);
print("<tr>");
print("<td> " . $product->itemID . " </td>");
print("<td> " . $product->productName . " </td>");
print("<td> " . $_SESSION['cart'][$i++]['qty'] . " </td>");
print("<td> $" . $product->productPrice . " </td>" );
print("<td>" . $product->showPrimaryImage('small') . "</td>");
print("</tr>");
}
//end the table
print("</table>");
以下是获取图片的功能:
public function showPrimaryImage($variant) {
if($variant == "medium") {
print ("<img style = \" max-width:400px;
max-height:400px;
display: block;
margin-left: auto;
margin-right: auto;\"
src=\"" . WEBSITE_URL . PATH_DYNAMIC . "/products_" . $this->productNumber . "_1_" . $variant . ".jpg\"
alt=\"" . $this->productName . "\">
");
}
else if($variant == "large") {
print ("<img style = \" max-width:600px;
max-height:600px;
display: block;
margin-left: auto;
margin-right: auto;\"
src=\"". WEBSITE_URL . PATH_DYNAMIC . "/products_" . $this->productNumber . "_1_" . $variant . ".jpg\"
alt=\"" . $this->productName . "\">
");
}
else if($variant == "small") {
print ("<img style = \" max-width:100px;
max-height:100px;
display: block;
margin-left: auto;
margin-right: auto;\"
src=\"". WEBSITE_URL . PATH_DYNAMIC . "/products_" . $this->productNumber . "_1_" . $variant . ".jpg\"
alt=\"" . $this->productName . "\">
");
}
}//end function showPrimaryImage()
当我在浏览器中查看源时,会出现呈现图像不在对中的代码。当我在浏览器源中查看红色警告时,它会显示:
开始标记&#34; img&#34;在&#34;表&#34;
中看到
此处显示图片的屏幕截图:
这是我的PHP代码输出的来源(在浏览器中查看时,整个标记语句为红色):
<table class='cart_table'>
<tr class='first_row'>
<td> Item No. </td>
<td> Item Name </td>
<td> Quantity </td>
<td> Price </td>
<td> Image </td>
</tr>
<tr>
<td> BQ2819 </td>
<td> A WWII Period U.S. Theater-made Fighting Knife with Sheath </td>
<td> 1 </td>
<td> $270.00 </td>
<img style = " max-width:100px;
max-height:100px;
max-height:100px;
display: block;
margin-left: auto;
margin-right: auto;"
src="http://www.mywebsite.com/dynamic/products_2056_1_small.jpg"
alt="A WWII Period U.S. Theater-made Fighting Knife with Sheath">
<td></td>
</tr>
<tr>
<td> BQ2819 </td>
<td> A WWII Period U.S. Theater-made Fighting Knife with Sheath </td>
<td> 1 </td>
<td> $270.00 </td>
<img style = " max-width:100px;
max-height:100px;
display: block;
margin-left: auto;
margin-right: auto;"
src="http://www.mywebsite.com/dynamic/products_2056_1_small.jpg"
alt="A WWII Period U.S. Theater-made Fighting Knife with Sheath">
<td></td>
</tr>
</table>
答案 0 :(得分:6)
对于这一行:
print("<td>" . $product->showPrimaryImage('small') . "</td>");
你有一个字符串连接,这意味着你将连接调用showPrimaryImage
的返回值。但是,showPrimaryImage
并未返回任何内容。因此,程序首先输出图像的html(来自showPrimaryImage
调用),然后从外部打印输出<td></td>
。
您需要将print更改为showPrimaryImage
函数
答案 1 :(得分:-2)
function showPrimaryImage($variant) {
if($variant == "medium") {
return img;
} else if() {
//...
}
}