I inherited a zencart mod and can't figure out what's wrong. The customer selects a product and an attribute (model#). This is then sent to another form that they complete. When they submit the form, the product and the attribute should be included in the email sent.
At this time, only the product is coming through. The attribute just says "array." The interesting part is, when I delete the line that prints the attribute, the products_options_names will print out. So I know that both the product and the products_options_names are working. The attribute is the only thing that is not working right.
Here's what I believe to be the significant code. This is the page that has the form, so the attribute should already be passed to the form.
//Begin Adding of New features
//$productsimage = $product['productsImage'];
$productsname = $product['productsName'];
$attributes = $product['attributes'];
$products_options_name = $va开发者_如何转开发lue['products_options_name'];
$arr_product_list[] = "<strong>Product Name:</strong> $productsname <br />";
$arr_product_list[] .= "<strong>Attributes:</strong> $attributes <br />";
$arr_product_list[] .= "<strong>Products Options Name:</strong> $products_options_name <br />";
$arr_product_list[] .= "---------------------------------------------------------------";
//End Adding of New features
} // end foreach ($productArray as $product)
?>
Above this, there is another section that has attributes:
<?php
echo $product['attributeHiddenField'];
if (isset($product['attributes']) && is_array($product['attributes'])) {
echo '<div class="cartAttribsList">';
echo '<ul>';
reset($product['attributes']);
foreach ($product['attributes'] as $option => $value) {
?>
Can anyone help me figure out what is wrong? I'm not sure if the problem is on this page or if the attribute isn't being passed to this page.
TIA
Your first snippet includes a potential issue. You're adding new items to the array $arr_product_list
while also trying to use the string concatenation .=
operator. This, of course, is confusing to PHP, since you're trying to add to a string that does not yet exist.
I believe the intent of your code is to build an HTML snippet and then add that to the array, which you could do with the following:
$str = "<strong>Product Name:</strong> $productsname <br />";
$str .= "<strong>Attributes:</strong> $attributes <br />";
$str .= "<strong>Products Options Name:</strong> $products_options_name <br />";
$str .= "---------------------------------------------------------------";
$arr_product_list[] = $str;
You're likely getting errors from PHP about this, but perhaps they're suppressed. Double-check your php.ini
and make sure your error_reporting
, display_errors
, display_startup_errors
and log_errors
settings are correct.
I realize this is probably too late to help with this particular problem, but I'm answering in hopes someone else stumbles upon this post with a similar issue.
The reason you're seeing "Array" printed out, is because the value stored in $products with the key 'attributes' is actually an array. The value in $product['attributes'] is an array type, that you're storing into $attributes. This means that the $attributes variable is an array, and so when you try to concatenate it with this line:
$arr_product_list[] .= "<strong>Attributes:</strong> $attributes <br />";
the interpreter doesn't know how to serialize the Array object, so defaults to printing out the object's type, which as we've determined, is an Array.
If there is only a single value in the $attributes array you care for, you would want to gather that value out prior to building your string with something like this:
var $attributes = $products['attributes'];
var $attributeValue = $attributes['my_target_value_key'];
**(where 'my_target_value_key' is the key of the value you wish to pull out)*
Then you can cat it out just as you were trying before, just replace $attributes in the string with $attributeValue.
The second block of code works because you can see it checks if the value is set and is an array type with this line:
if (isset($product['attributes']) && is_array($product['attributes'])) {
Then then loops through each item in the returned array with this line:
foreach ($product['attributes'] as $option => $value) {
精彩评论