我正在尝试使用PHP回显一些amp-html代码
<?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>";
?>
我找不到一个语法错误,我遗漏了什么?有比使用echo更简单的方法吗?
更新:我已经根据建议编辑了代码,但仍然出现了一个空白页面:以下是完整的代码:
<?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>
发布于 2018-09-04 02:58:29
由于您的代码中实际的PHP输出如此之少,放弃PHP来输出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
发布于 2018-09-04 02:52:11
问题确实出在语法的使用上。您应该尝试做的是,当您回显html时使用单引号,以及您可以在html中添加的任何其他内容,您可以使用双引号。
<?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/'.$filename[$i].'_HD.mp4" type="video / mp4 / >"';
echo '<source src="https://example.com/videos/'.$filename[$i].'__HD.webm" type="video/webm"/>"';
echo '<div fallback><p>This browser does not support the video element.</p></div>';
echo '</amp-video>';
echo '</section>';
?>
这样就更容易发现语法错误,因为页面要么显示它必须显示的内容,要么显示代码。
https://stackoverflow.com/questions/52154718
复制相似问题