3

is there any way I can get the iterate through a lot of input type number's and only print out the values that have a value greater than 0?

Let's say I have 10

        <form action="displayInfo.php" method="post">


    <table cellpadding="6">
      <tr>
        <th>Description</th>
        <th>Price</th>
        <th>Weight</th>
        <th>Image</th> 
        <th>Quantity</th>
      </tr>
    <!-- PHP LANGUAGE -->
    <?PHP
        $sql = "SELECT * FROM parts";
        $q = $conn->query($sql) or die("ERROR: " . implode(":", $conn->errorIndo()));

        while( $row = $q->fetch(PDO::FETCH_ASSOC))
        {

            echo '<tr>' .
                '<td>' . $row['description'] . '</td>' .
                '<td>' . $row['price'] . '</td>' .
                '<td>' . $row['weight'] . '</td>' . 
                '<td> <img src="' . $row[pictureURL] .'" alt="' . $row[number] . '" style="width:50px;height:50px;">' .
                '<td> <input type="number" name = "' . $row[number] ." id= "'. $row[number] . '"  value="0">' .
                '</td>' . '</tr>';
        }
        echo "</table>";

    ?> 
    </table> <br>



    <input type="button" id="submit" value="Submit" />
    </form>

So they are dynamically created with an id that have values 1,2,3,...10. The values are then updated through user input.

Is there anyway I can catch the data passed by the submit button for the values entered by the user that are greater than 0? If so, how would I also pass the description or even the $row[number] along with the value that is associated with it. the id of the input type number = the $row[number] as displayed in the code.

5
  • For what reason do you not want the 0 value being posted? Commented May 2, 2017 at 8:13
  • Just for display purposes so the input type has a value stored in it. So it shows the user that a number would Ideally go into the cell. Commented May 2, 2017 at 8:21
  • 1
    To begin with, form controls that do not have a name attribute are not even sent to the server. Commented May 2, 2017 at 8:30
  • Apologies I accidentally forgot I have an id and a name attribute. Commented May 2, 2017 at 8:48
  • 1
    Stop outputting die statements on SQL failure, this will disrupt user experience and will output dangerous error details to potential hackers. Commented May 2, 2017 at 8:55

1 Answer 1

3

The simplest way is probably to use a PHP feature, explained at How do I create arrays in a HTML <form>?

In short, you rename the form controls to use a common prefix, add square brackets to make PHP convert them into array and then loop data as any other array:

'<td> <input type="number" name = "row[' . $row[number] . ']" id= "'. $row[number] . '"  value="0">' .

I've also fixed a missing quote but I presume it isn't in your actual code.

Then:

foreach ($_POST['row'] as $number => $value) {
}

And since it's an array you can use the usual goodies, e.g.:

$rows = array_filter($_POST['row'], function($val){
    return is_numeric($val) && $val>10;
});

Demo

Sign up to request clarification or add additional context in comments.

2 Comments

If I may ask, the code under your "Then:" would be in the file that the form goes to, but what from what I gather you are getting the row array that was passed and you are using the number that would be in the array and setting it equal to a new variable called value. Now you create a new variable called rows and go through the array row by creating a function with the passed in variable val which returns val if it is a number and is greater than 10. Is $val supposed to be $value? Thank you for helping. I'm still a beginner so I'm just trying to understand what exactly is going on.
They're independent suggestions, you don't possibly need to do both. foreach is a PHP flow control structure, you can read about it in the manual. Also, make sure you understand what arrays are.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.