无法在表格单元格中插入图像

时间:2014-08-04 00:06:24

标签: php html

我正在尝试为我的网站编写一个购物车,我想显示一个表格,每个项目都有一行,包括部分#,名称,数量,价格和小图片。除了图像之外,一切都显示得很好,图像出现在桌子的外面(上面),而不是放在我放置它的单元格中。这是代码:

//create a table for the items in the cart
 print("<table class='cart_table'>");
 print("<tr class='first_row'>
        <td>&nbsp;Item No.&nbsp;</td>
        <td>&nbsp;Item Name&nbsp;</td>
        <td>&nbsp;Quantity&nbsp;</td>
        <td>&nbsp;Price&nbsp;</td>
        <td>&nbsp;Image&nbsp;</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>&nbsp;" . $product->itemID . " </td>");
    print("<td>&nbsp;" . $product->productName . " </td>");
    print("<td>&nbsp;" . $_SESSION['cart'][$i++]['qty'] . " </td>");
    print("<td>&nbsp;$" . $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;

中看到

此处显示图片的屏幕截图: enter image description here

这是我的PHP代码输出的来源(在浏览器中查看时,整个标记语句为红色):

    <table class='cart_table'>
    <tr class='first_row'>
        <td>&nbsp;Item No.&nbsp;</td>
        <td>&nbsp;Item Name&nbsp;</td>
        <td>&nbsp;Quantity&nbsp;</td>
        <td>&nbsp;Price&nbsp;</td>
        <td>&nbsp;Image&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;BQ2819 </td>
        <td>&nbsp;A WWII Period U.S. Theater-made Fighting Knife with Sheath </td>
        <td>&nbsp;1 </td>
        <td>&nbsp;$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>&nbsp;BQ2819 </td>
        <td>&nbsp;A WWII Period U.S. Theater-made Fighting Knife with Sheath </td>
        <td>&nbsp;1 </td>
        <td>&nbsp;$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>

2 个答案:

答案 0 :(得分:6)

对于这一行:

print("<td>" . $product->showPrimaryImage('small') . "</td>");

你有一个字符串连接,这意味着你将连接调用showPrimaryImage的返回值。但是,showPrimaryImage并未返回任何内容。因此,程序首先输出图像的html(来自showPrimaryImage调用),然后从外部打印输出<td></td>

您需要将print更改为showPrimaryImage函数

的return语句

答案 1 :(得分:-2)

function showPrimaryImage($variant) {
  if($variant == "medium") {
    return img;
  } else if() {
    //...
  }
}