0

I want to loop through an array of names within my form and check the a property, is_verified for a given ID.

form:

<div class="input">
    <?php echo form_dropdown(
        "is_email_verified[{$email->id}]", [
            0 => 'no',
            1 => 'yes'
        ], $email->is_verified, 'id="is_'. $email->id . '_verified"'
    ); ?>
</div>

print_r($this->input->post()) gives:

"Array ( 
     ...
     [is_email_verified] => Array(
           [123] => 1
      )

Let's say there are more than one e-mails, and I'm looping through these e-mails, checking if the id's (in this case, 123) is_verified bool is set to 0 or 1.

I tried this but it doesn't work:

foreach ($member->emails as $email) {
   if ($this->input->post("is_email_verified[$email->id]") == '1') {

How can I loop through the form values and check against that specific ID in the array?

2 Answers 2

2

You need to get the array from the post data, then access the array via the desired index.

foreach ($member->emails as $email) {
    if ($this->input->post("is_email_verified")[$email->id] == '1') {
Sign up to request clarification or add additional context in comments.

Comments

0

you can try to extract your code a little bit and change it like this. This way it might be easier to debug.

foreach ($member->emails as $email) {

    $postArray = $this->input->post();
    $emails = $postArray['is_email_verified'];
    $verified = $emails[$email->id];

    if($verified == 1) {

    }
}

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.