Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top