Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9290845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:30:47+00:00 2026-06-18T20:30:47+00:00

I have a loop below that is showing a quantity box and includes a

  • 0

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T20:30:48+00:00Added an answer on June 18, 2026 at 8:30 pm

    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>";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scenerio: I have some divs populated through foreach loop like below <div id=div1><input type=hidden
I have the below but want to only loop through lines in #lines which
I have the code below and I want to loop this every 5 seconds.
I have code below that is getting called from a while loop so it's
I have an xml document that I am creating using the loop below and
I have a for loop that I want to exit like this: function MyFunction()
I have a foreach loop (see below) that encodes the values to htmlentities() and
I have a jquery images scroller below that just scrolls horizontally left/right. The loop
I have this VBA below that is designed to loop through each WS and
I have python code below that will loop through a table and print out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.