Search code examples
phpamp-html

Trouble finding PHP syntax error using echo to create amp-html code


I am attempting to echo some amp-html code using PHP

<?php
echo "<section>";
echo "<h3>$title[$i]</h3>";
echo "<amp-video controls width="560" height="315" layout="responsive">";
echo "<source src="https: //example.com/videos/";
    echo $filename[$i];
    echo "_HD.mp4 type="video / mp4 / > ";
    echo " < sourcesrc = "https://example.com/videos/";
    echo $filename[$i];
    echo '"__HD.webm" type="video/webm"/>';
    echo "<div fallback><p>This browser does not support the video element.</p></div>";
    echo "</amp-video>";
    echo "</section>";
?>

I have been unable to find a syntax error, what a I missing? Is there an easier way than using echo.?

Update: Ive edited the code according to the suggestions, but its still throwing up a blank page: Here is the full code:

<?php
        // Create connection
        $con=mysqli_connect("localhost","xx","xxxx","xxxx");

       // Check connection
    if (mysqli_connect_errno($con)) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    //Get number of rows

$sql="SELECT id,title,filename FROM videos";

    $result=mysqli_query($con, $sql);
    $i=1;
    while($row=mysqli_fetch_assoc($result))
    {
        $id[$i] = $row['id'];
        $title[$i] = $row['title'];
        $filename[$i] = $row['filename'];
        $i++;
    }
    // Loop through the results from the database

    for ($i = 1; $i <=count($id); $i++)
    {
?>

<section>
    <h3><?php echo $title[$i];?></h3>
    <amp-video controls width="560" height="315" layout="responsive">
        <source src="https: //example.com/videos/<?php echo $filename[$i];?>_HD.mp4" type="video/mp4" />
        <source src="https://example.com/videos/<?php echo $filename[$i];?>__HD.webm" type="video/webm" />
        <div fallback><p>This browser does not support the video element.</p></div>
    </amp-video>
</section>
}
</amp-accordion>

Solution

  • With so little actual PHP output in your code it might be easier and more readable to just drop out of PHP to output the HTML.

            // Create connection
            $con=mysqli_connect("localhost","xx","xxxx","xxxx");
    
           // Check connection
        if (mysqli_connect_errno($con)) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
        //Get number of rows
    
    $sql="SELECT id,title,filename FROM videos";
    
        $result=mysqli_query($con, $sql);
        while($row=mysqli_fetch_assoc($result)) {
    ?>
    
    <section>
        <h3><?php echo $row['title']; ?></h3>
        <amp-video controls width="560" height="315" layout="responsive">
            <source src="https: //example.com/videos/<?php echo $row['filename']; ?>_HD.mp4" type="video/mp4" />
            <source src="https://example.com/videos/<?php echo $row['filename']; ?>__HD.webm" type="video/webm" />
            <div fallback><p>This browser does not support the video element.</p></div>
        </amp-video>
    </section>
    <?php
        }  // end of while
    ?>
    </amp-accordion>
    <?php