简体   繁体   English

什么会导致字段返回PHP中的数组?

[英]what would cause a field to return array in php?

I'm working on modifying an existing shopping cart application so I'm fumbling through this. 我正在修改现有的购物车应用程序,所以我在摸索。 Using PHP 5 and mySql. 使用PHP 5和mySql。

The code sends an email message to the client and to the user that shows which product they are interested in. The page is rendering the product and the attributes correctly. 该代码向客户端和用户发送电子邮件,以显示他们感兴趣的产品。该页面正确呈现了产品和属性。 When the email is sent the product portion works but the attributes don't work. 发送电子邮件后,产品部分起作用,但属性不起作用。

It should show the attributes, but it just has the word "Array." 它应该显示属性,但只包含单词“ Array”。

The email portion sends this: 电子邮件部分发送此:

Product Name: (whatever the product is) 产品名称:(无论产品是什么)

Attributes: Array 属性:数组

Products Options Name: (whatever product option is) 产品选项名称:(无论产品选项是什么)

This code is used to generate the product info in the email: 此代码用于在电子邮件中生成产品信息:

$message .= '<p style="font:bold 14px/25px Verdana, Arial, Helvetica, sans-serif;
margin:0; padding:0;"><strong>List of Products</strong></p><br /><br />' . "\n";
if (is_array($arr_product_list)){
        foreach($arr_product_list as $value) {
            $message .= '<strong>' . $value . '</strong><br />' . "\n";
        }
}

This is the code used to display the products on the rendered page: 这是用于在渲染页面上显示产品的代码:

$productsname = $product['productsName'];
$attributes = $product['attributes'];
$products_options_name = $value['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[] .=
"---------------------------------------------------------------";

And here's what is showing on the rendered page where they should choose the product and attribute: 这是呈现的页面上显示的内容,他们应该在其中选择产品和属性:

<div class="wrapperAttribsOptions">
  <h4 class="optionName back"><label class="attribsSelect" for="attrib-14">SPK
  Model</label></h4>
<div class="back">
  <select name="id[14]" id="attrib-14">
   <option value="43">SPK-4</option>
</select>
</div>

I guess where I'm really confused is I don't see where anything is called attrib-14 or id14 in the code that sends the email so I'm having problems figuring this out. 我想我真正感到困惑的是,在发送电子邮件的代码中没有看到什么叫做attrib-14或id14,因此我很难弄清楚这一点。

Any help would be appreciated. 任何帮助,将不胜感激。

Thanks! 谢谢!

By using xyz[] in the form name, you are turning the resulting xyz variable in PHP into an array. 通过在表单名称中使用xyz[] ,可以将PHP中生成的xyz变量转换为数组。 It takes all the xyz values from the form and creates an array of them. 它从表单中获取所有xyz值并创建它们的数组。

You would have to resolve the array in php, eg using 您将不得不在php中解析数组,例如使用

 foreach($attributes as $field)
  $arr_product_list[] = "$field<br />";

or use a different notation for your fields: 或为您的字段使用其他符号:

<select name="id_14" id="attrib-14">

however, that way you have to deal with however many id_* fields there are in your POST input. 但是,这样一来,您必须处理POST输入中有多少id_*字段。

It seems that products store their attributes as an array, which makes sense since there can be many. 产品似乎将其属性存储为数组,这很有意义,因为可以有很多。

Try changing this line: 尝试更改此行:

$arr_product_list[] .= "<strong>Attributes:</strong> $attributes <br />";

to

$arr_product_list[] .= "<strong>Attributes:</strong>".join(', ', $attributes)."<br />";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM