0

This question probably has a simple solution. Here's what I am doing:

I am dynamically creating checkbox inputs with a PHP loop.

<td><input type=\"checkbox\" class=\"current\" name=\"User[$row[0]]\" value=\"1\" ></td>

This snippet, name=\"User[$row[0]]\", is part of an array that is passed via post upon submission. Rather than using User[], I define the associative array by using $row[0]. $row[0] allows me to use a unique identifier that is really a UserId - it is guaranteed to be unique. The value of an instance of a submitted array is either 1 or 0 (true or false). This is a result of print_r(said array):

Array ( [1] => 1 
        [12] => 0
        [31] => 1 
        [4] => 1 )

What I need to do is use the User Id (which happens to be the identifier of a component of the array, i.e. 1, 12, 31...) in a SQL query. For example.

UPDATE...WHERE UserId = array identifier

At the same time, I need to use the value of that component (0 or 1) in that same query.

Any input is appreciated.

2 Answers 2

2

When you process your array, use a foreach loop:

foreach($results as $key => $value)
{
    $sql .= "UPDATE...WHERE UserId = $key;";
}

This lets you not only use the value of each array element, but also its key. See the php manual for more on foreach.

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

1 Comment

Thanks! I now know yet another use for this function.
1

Use foreach loop with key=>value pair

foreach($arr as $key=>$val) {
    $sql = "UPDATE table_name SET column = $val WHERE UserId = $key";
}

Comments

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.