0

I have this table:

emailtype:
emailtypeID      emailtype
1                 primary
2                 secondary
3                 old

I have this code to show emails in the input form:

  $sql = "SELECT * from emailtype";
  $result = $conn->query($sql);
  while($row = $result->fetch_assoc()) { 
         echo 'Email <input type="hidden" name="emailtype[]" id="" value="' . $row["emailtypeID"] . '"/>'  . $row["emailtype"];
        echo '<input type="text" name="email[]" id="" /><br />';            }  

GOAL:

I would like to repeat twice the emailtypeID = 2 or emailtype = secondary, so that I can enter two email addresses with the secondary ID.

Is it possible in the while loop?

Thanks!

SOLUTION:

For anybody who needs it, this is the new code as per the Marc B suggestion.

 while($row = $result->fetch_assoc()) { 
      echo 'Email <input type="hidden" name="emailtype[]" id="" value="' . $row["emailtypeID"] . '"/>'  . $row["emailtype"];
      echo '<input type="text" name="email[]" id="" /><br />';
    if($row["emailtype"] == 'secondario'){
        echo 'Email <input type="hidden" name="emailtype[]" id="" value="' . $row["emailtypeID"] . '"/>'  . $row["emailtype"];
         echo '<input type="text" name="email[]" id="" /><br />';
    }   

}

3
  • 4
    if (id == 2) { output extra stuff }? Commented Jan 22, 2016 at 16:18
  • thanks! Marc! Sorry, but where exactly should go the if condition? Commented Jan 22, 2016 at 16:22
  • @Marc B Thanks! I edited the question with your solution! Commented Jan 22, 2016 at 16:31

1 Answer 1

1

Please use this:

while($row = $result->fetch_assoc()) { 
      echo 'Email <input type="hidden" name="emailtype[]" id="" value="' . $row["emailtypeID"] . '"/>'  . $row["emailtype"];
      echo '<input type="text" name="email[]" id="" /><br />';
    if($row["emailtype"] == 'secondario' || $row["emailtypeID"]==2){
        echo 'Email <input type="hidden" name="emailtype[]" id="" value="' . $row["emailtypeID"] . '"/>'  . $row["emailtype"];
         echo '<input type="text" name="email[]" id="" /><br />';
    }   

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

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.