将两个输入绑定在一起(文本和隐藏)? [英] Tie two inputs together (text & hidden)?

查看:144
本文介绍了将两个输入绑定在一起(文本和隐藏)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有一个循环,显示一个数量框并包含一个隐藏的字段,其中包含产品名称。

我希望这些数据绑定在一起, 100个输入用户改变了输入90的数量,然后希望将隐藏的输入90与其绑定。

然后这给我数量和产品名称对于大于零的项目。

 <?php if(get_field('sizes')){
while(the_repeater_field('sizes')){?>
< input type =textname =quantity []value =0> <?php the_title(); ?>
< input type =hiddenname =product []value =<?php the_title();?>>
<?php}}?>

我想将这两者联系在一起,以便回显以下内容:




  • 1 x产品1

  • 10 x产品三

  • 20 x产品八



只有数量超过零才能输出数量和产品名称






这是实际使用的代码:

 < ?php if(get_field('sizes')){?> 
<?php while(the_repeater_field('sizes')){?>
< tr>
< td width =150>< p><?php echo the_sub_field('size'); ?>< / P>< / TD>
< td width =30align =right>
< p>
< input type =textclass =quantityname =quantity []style =width:15px; text-align:center!IMPORTANT; margin-left:10px;值= 0 >
< input type =hiddenclass =productinputname =product []value =<?php echo the_title();?> - <?php echo the_sub_field('size' );?>>< / td>
< / p>
< / td>
< / tr>
<?php}?>
<?php} else {?>
< tr>
< td width =150>< p>数量< / p>< / td>
< td width =30align =right>
< p>
< input type =textclass =quantityname =quantity []style =width:15px; text-align:center!IMPORTANT; margin-left:10px; value =0><?php echo the_sub_field('size'); ?>
< input type =hiddenclass =productinputname =product []value =<?php echo the_title();?>>
< / p>
< / td>
< / tr>
<?php}?>

然后创建准备好用电子邮件输出的代码:

  $ quantities = array_combine($ _ POST ['product'],$ _POST ['quantity']); 
foreach($ quantity as $ product => $ quantity){
if($ quantity> 0){
$ productresults =$ quantity x $ product;


$ / code $ / pre

$ hr

这是我正在处理的页面。如果您点击Get Quotation,第二步就是上面的代码。






@ Sn0opy

  foreach($ _ POST ['quantity'] as $ check){
if($ check> 0){
$ quantityresults。= $ check。\\\
;
}
}

echo $ quantityresults;


解决方案

您可以查看产品的数量是否非零以决定是否显示。



一般 foreach 对于这项工作来说是一个尴尬的工具,要走的路是用循环,并使用相同的计数器将它们索引到两个数组中。但是,在这种特定情况下,您可以轻松地将两个数组转换为一个键,其中键是产品名称,数量是使用 array_combine

  $ quantities = array_combine($ _ POST ['product'],$ _POST ['quantity']); 

然后,您可以轻松地迭代 foreach
$ b $

  foreach($ quantity as $ product => $ quantity){
if($ quantity> 0) {
echo$ quantity x $ product< br>;
}
}


I have a loop below that is showing a quantity box and includes a hidden field containing the product name.

I want these to be tied together so if out of 100 inputs the user changes the quantity of input 90, then and want the hidden field input 90 to be tied to it.

This then gives me the quantity and the product name for items that have more than zero.

<?php if(get_field('sizes')) {
while(the_repeater_field('sizes')) { ?>
   <input type="text" name="quantity[]" value="0"> <?php the_title(); ?>
   <input type="hidden" name="product[]" value="<?php the_title(); ?>">
<?php } } ?>

I want to tie these two together so it would echo out the following:

  • 1 x Product One
  • 10 x Product Three
  • 20 x Product Eight

How can I output the quantity AND product name only if the quantity is more than zero?


This is the actual code used:

    <?php if(get_field('sizes')) { ?>
    <?php while(the_repeater_field('sizes')) { ?>
        <tr>    
            <td width="150"><p><?php echo the_sub_field('size'); ?></p></td> 
            <td width="30" align="right">
                <p>
                    <input type="text" class="quantity" name="quantity[]" style="width:15px;text-align:center!IMPORTANT;margin-left:10px;" value="0">
                    <input type="hidden" class="productinput" name="product[]" value="<?php echo the_title(); ?> - <?php echo the_sub_field('size'); ?>"></td>
                </p>
            </td>
        </tr>
    <?php } ?>
    <?php } else { ?>
        <tr>            
            <td width="150"><p>Quantity</p></td>
            <td width="30" align="right">
                <p>                
                    <input type="text" class="quantity" name="quantity[]" style="width:15px;text-align:center!IMPORTANT;margin-left:10px;" value="0"><?php echo the_sub_field('size'); ?>
                    <input type="hidden" class="productinput" name="product[]" value="<?php echo the_title(); ?>">
                </p>
            </td>
        </tr> 
    <?php } ?>

This is then creating the code ready to be output in an email:

$quantities = array_combine($_POST['product'], $_POST['quantity']);
foreach ($quantities as $product => $quantity) {
    if ($quantity > 0) {
        $productresults = "$quantity x $product";
    }
}


This is the page I'm working on. If you click on "Get Quotation", the second step is the code above.


@Sn0opy

foreach($_POST['quantity'] as $check) {
    if($check > 0) {
        $quantityresults .= $check."\n";
    }
}

echo $quantityresults;

解决方案

You obviously need to iterate over both arrays in tandem so that you can see if a product's quantity is nonzero in order to decide if it should be displayed.

In general foreach is an awkward tool for this job, and the way to go is with a for loop and indexing into both arrays using the same counter. However, in this specific case you can easily transform your two arrays into one where the keys are product names and the quantities are the values using array_combine:

$quantities = array_combine($_POST['product'], $_POST['quantity']);

You can then easily iterate with foreach:

foreach ($quantities as $product => $quantity) {
    if ($quantity > 0) {
        echo "$quantity x $product<br>";
    }
}

这篇关于将两个输入绑定在一起(文本和隐藏)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆