0

I am devolping a web application using symfony framework. I have a problem in forms. Here is my code:

$value = array(); 

foreach ($myArray as $value) {

$fieldNameAppend    ='<input type="radio" name="'.$value.'Cleaning'.$id.'"  value="'.$value.'Cleaning'.$id.'" id="'.$value.'Cleaning'.$id.'" class="inputFields">'.$value.'';   
}   

print_r($fieldNameAppend);

In the loop i got all data in allValues variable.But when I access outside the loop i got only one value.

Please help

2
  • Yes, you didn't contatenate the string to $fieldNameAppend :) you just assign last string in the loop. Commented Sep 21, 2015 at 12:12
  • You use symfony yet you build forms by hand. Why's that? Commented Sep 21, 2015 at 12:56

3 Answers 3

1

its because u keep overwriting $fieldNameAppend.

YOu can try it this way to put all the inputs in the same string (notice the .=)

    fieldNameAppend = '';
foreach ($myArray as $value) {
    $fieldNameAppend    .='<input type="radio" name="'.$value.'Cleaning'.$id.'"  value="'.$value.'Cleaning'.$id.'" id="'.$value.'Cleaning'.$id.'" class="inputFields">'.$value.'';   

}
echo $fieldNameAppend;

or to make a array:

    fieldNameAppend = array();

foreach ($myArray as $value) {

        $fieldNameAppend[]    ='<input type="radio" name="'.$value.'Cleaning'.$id.'"  value="'.$value.'Cleaning'.$id.'" id="'.$value.'Cleaning'.$id.'" class="inputFields">'.$value.'';   

    }
print_r($fieldNameAppend);
Sign up to request clarification or add additional context in comments.

Comments

0

You may need to concatenate ' .= ' the variable to get all the dynamic inputs.

$fieldNameAppend    .='<input type="radio" name="'.$value.'Cleaning'.$id.'"  value="'.$value.'Cleaning'.$id.'" id="'.$value.'Cleaning'.$id.'" class="inputFields">'.$value.'';   

1 Comment

ensure $fieldNameAppend is assigned the empty string before the loop
0

try this code

 $strevalue = array();

  foreach ($myArray as $value) {

        $strevalue['value']    ='<input type="radio" name="'.$value.'Cleaning'.$id.'"  value="'.$value.'Cleaning'.$id.'" id="'.$value.'Cleaning'.$id.'" class="inputFields">'.$value.'';   
        $val[] =$strevalue;

  }
  print_r($val);

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.