0

I wrote a simple script to display content to the user in the requested order using 4 arrays of equal length built concurrently, but for some reason the content string built from this is only being concatenated with the final item in the array regardless of the number of requests.

This is the part of my code that is causing the issue:

$snippet="<td id = \"content\">";
for($i=0;$i<count($content_types);$i++){
    $type = $content_types[i];
    if($type == "Video")
    {
        $pos_str = ((strlen($content_pos[i]) > 0) ? "class=\"".$content_pos[i]."\"" : "");
        $file_type = $content_file_types[i];
        $snippet .= "<video ".$pos_str." width=\"320\" height=\"240\" controls>
                            <source src=\"".$content[i]."\" type=\"video/".$file_type."\">
                            Your browser does not support the video tag.
                        </video>";
    }
    if($type == "Audio")
    {
        $pos_str = ((strlen($content_pos[i]) > 0) ? "class=\"".$content_pos[i]."\"" : "");
        $file_type = $content_file_types[i];
        $snippet .= "<audio ".$pos_str." controls>
                            <source src=\"".$content[i]."\" type=\"audio/".$file_type."\">
                            Your browser does not support the audio tag.
                        </audio>";
    }
    if($type == "Text")
    {
        $snippet .= "<p class=\"content_text\">
                    ". urldecode($content[i]) ."
                </p>";
    }
    if($type == "Image")
    {
        $pos_str = ((strlen($content_pos[i]) > 0) ? "class=\"".$content_pos[i]."\"" : "");
        $snippet .= "<img src=\"
                    ".$content[i]."
                \" ".$pos_str."></img>";
    }
}
$snippet .="</td>";

Each code segment for the different content types works correctly, it is only the concatenation of these values into a single string that is not functioning.

The two external '$snippet' concatenations that occur are represented in the output along with the final concatenation within the loop.

I've been working on this for hours and have not been able to figure out why only the final item is being added to the string.

I'm probably just too close to the code and therefore unable to see the obvious mistake that I've made.

Any help would be appreciated.

Thanks in advance.

1 Answer 1

2

You missing $ symbol at all the places you have used variable $i.

$type = $content_types[$i];
                       ^

Note: Add error_reporting(E_ALL & E_STRICT); on top of the file it will show you errors & notices in such cases.

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

3 Comments

That was it. Thank you very much. I knew it was something simple I was missing. And thanks for the advice about error reporting. Very helpful.
If my answer solved your problem, click the big checkbox to accept it as the answer.
Sorry about that. This is my first time posting a question here and I wasn't aware that that was how to mark the question as answered. Thanks again.

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.