如何为此代码添加更多值以供显示?
例如:我向不同供应商的购物车添加了5件产品。
function getVendors()
{
foreach($_SESSION["products"] as $product) {
$org[$product["member_display_name"]][] = $product["p_name"];
}
return (!empty($org)) ? $org : array();
}
foreach (getVendors() as $vendor => $prods) {
echo "<li>Vendor Name : {$vendor} </li>";
echo "<li>" . implode("</li><li>",$prods) . "</li>";
}
我的代码:
Vendor Name : TEST1
P1
Vendor Name : TEST2
P2
P3
Vendor Name : TEST3
P4
Vendor Name : TEST4
P5
此代码的输出如下:
$product["member_payment"]
这就是我想要的
我想添加Vendor Name : TEST1
P1
Payment to Vendor : 000-111-111
Vendor Name : TEST2
P2
P3
Payment to Vendor : 000-111-222
Vendor Name : TEST3
P4
Payment to Vendor : 000-111-333
Vendor Name : TEST4
P5
Payment to Vendor : 000-111-444
以便显示如下:
<form name="sentMessage" id="contactForm" action="mailer.php" method="post" novalidate>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Name</label>
<input type="text" name="name" class="form-control" placeholder="Name" id="name" required data-validation-required-message="Please enter your name.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Email Address</label>
<input type="email" name="email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Phone Number</label>
<input type="tel" name="phone" class="form-control" placeholder="Phone Number" id="phone" required data-validation-required-message="Please enter your phone number.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Message</label>
<textarea rows="5" name="message" class="form-control" placeholder="Message" id="message" required data-validation-required-message="Please enter a message."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<br>
<div id="success"></div>
<div class="row">
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-default">Send</button>
</div>
</div>
</form>
答案 0 :(得分:1)
恕我直言,你需要重新调整你的阵列。在$ org中添加一对额外的键,因此您可以从迭代器
访问它function getVendors() {
foreach($_SESSION["products"] as $product) {
$org[$product["member_display_name"]]['product'][] = $product["p_name"];
}
$org[$product["member_display_name"]]['payment'] = $product["p_payment"]; //where $product['p_payment'] is a string like '000-111-222'
return (!empty($org))? $org : array();
}
foreach(getVendors() as $vendor => $prods) {
echo "<li>Vendor Name : {$vendor} </li>";
if (isset($prods['product'])) echo "<li>".implode("</li><li>",$prods['product'])."</li>";
if (isset($prods['payment'])) echo "<li>Payment to Vendor : ".$prods['payment']."</li>";
}
我正在回答&amp;在我头顶打字..希望你得到一般的想法。