1

$ext is an array

$noofformats = count($data['formats']);
for($x = 0; $x < $noofformats; $x++){ 
    echo $select = "<option value=\"" . $x . "\">" . $ext[$x] . "</option>";
}

When I run this script I get the desired result. Result 1:

<option value="0">webm</option><option value="1">m4a</option> and so on.....

This works because it echos the result everytime it runs, so I get the string with every value of $noofformat possible. But I wish to define $select as the result shown above. What happens is everytime function loops the newer result is defined instead of being added to the previous value, resulting in only the last value

<option value="33">mp4</option> 

I wish

echo $select;

to give a result like Result 1

2 Answers 2

2

What happens right now is that every iteration, $select gets overwritten. You could continually concatenate the markup string first, then finally echo in the end:

$noofformats = count($data['formats']);
for($x = 0; $x < $noofformats; $x++){ 
    $select .= "<option value=\"" . $x . "\">" . $ext[$x] . "</option>";
        //  ^ continually concatenate values while the loop is running
}

echo $select;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for replying so quickly :D
@KaziLotus yes you could just add the concatenation operator to append each string into it instead of overwriting it each iteration
1

Use

$noofformats = count($data['formats']);
for($x = 0; $x < $noofformats; $x++){ 
    echo $select .= "<option value=\"" . $x . "\">" . $ext[$x] . "</option>";
}

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.