我正在尝试在php“ if语句”内和“ While”循环中显示一些html。 我不断收到意外的文件错误结尾,我知道缺少两个花括号,但是我看不到在哪里正确关闭while循环。 我对编码还是很陌生,所以请保持柔和... :-) 在这里尝试了php语法检查器和其他类似的帖子
<?php include 'includes/header.php';?>
<!-- Navigation -->
<?php include 'includes/navbar.php';?>
<?php
if (isset($_GET['post'])) {
$post = $_GET['post'];
if (!is_numeric($post)) {
header("location:index.php");
}
}
else {
header('location: index.php');
}
$query = "SELECT * FROM posts WHERE id=$post";
$run_query = mysqli_query($conn, $query) or die(mysqli_error($conn)) ;
if (mysqli_num_rows($run_query) > 0 ) {
while ($row = mysqli_fetch_array($run_query)) {
$post_title = $row['title'];
$post_id = $row['id'];
$post_author = $row['author'];
$post_date = $row['postdate'];
$post_image = $row['image'];
$post_content = $row['content'];
$post_tags = $row['tag'];
$post_status = $row['status'];
$img_ext = explode(".", $post_image);
?>
<!-- Page Content -->
<div class="container">
<div class="row">
<!-- Blog Post Content Column -->
<div class="col-lg-8">
<!--First Post -->
<hr>
<p><h2><a href="#"><?php echo $post_title; ?></a></h2></p>
<p><h3>by <a href="#"><?php echo $post_author; ?></a></h3></p>
<p><span class="glyphicon glyphicon-time"></span>Posted on <?php echo $post_date; ?></p>
<hr>
<?php if ($img_ext=="mp4"): ?>
<video controls width="400" height="300">
<source src="allpostpics/<?php echo $post_image; ?>" type="video/mp4">
Video tag is not supported in this browser.
</video>
<?php else : ?>
<img class="img-responsive img-rounded" src="allpostpics/<?php echo $post_image; ?>" alt="900 * 300">
<hr>
<p><?php echo $post_content; ?></p>
</div>
<!-- Blog Sidebar Widgets Column -->
<div class="col-md-4">
<?php include 'includes/sidebar.php'; ?>
</div>
</div>
<!-- /.row -->
<hr>
<!-- Footer -->
<?php include 'includes/footer.php';?>
</div>
<!-- /.container -->
<!-- jQuery -->
<script src="js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>
答案 0 :(得分:0)
从语法上讲,PHP与C相比,与Python或VisualBasic更相似。缩进在这里没有任何意义。代码块需要显式打开和关闭。
有两种类型可以引用if-else块,但您都没有使用。
您可以:
if { ... } else { ... }
示例:
<?php if ($img_ext=="mp4") { ?>
<video controls width="400" height="300">
<source src="allpostpics/<?php echo $post_image; ?>" type="video/mp4">
Video tag is not supported in this browser.
</video>
<?php } else { ?>
<img class="img-responsive img-rounded" src="allpostpics/<?php echo $post_image; ?>" alt="900 * 300">
<hr>
<p><?php echo $post_content; ?></p>
<?php } ?>
if
,endif
示例
<?php if ($img_ext=="mp4"): ?>
<video controls width="400" height="300">
<source src="allpostpics/<?php echo $post_image; ?>" type="video/mp4">
Video tag is not supported in this browser.
</video>
<?php else: ?>
<img class="img-responsive img-rounded" src="allpostpics/<?php echo $post_image; ?>" alt="900 * 300">
<hr>
<p><?php echo $post_content; ?></p>
<?php endif ?>
只需选择要去的那个。
类似地,您必须使用方括号}
或endwhile
关闭while循环,具体取决于您选择的语法。
答案 1 :(得分:0)
您打开了if语句,并且在未关闭时也忘记了关闭它,而在容器关闭后关闭了它
答案 2 :(得分:0)
尝试以下方法:
<?php include 'includes/header.php'; ?>
<!-- Navigation -->
<?php include 'includes/navbar.php'; ?>
<?php
if (isset($_GET['post'])) {
$post = $_GET['post'];
if (!is_numeric($post)) {
header("location:index.php");
}
} else {
header('location: index.php');
}
$query = "SELECT * FROM posts WHERE id=$post";
$run_query = mysqli_query($conn, $query) or die(mysqli_error($conn));
if (mysqli_num_rows($run_query) > 0) {
while ($row = mysqli_fetch_array($run_query)) {
$post_title = $row['title'];
$post_id = $row['id'];
$post_author = $row['author'];
$post_date = $row['postdate'];
$post_image = $row['image'];
$post_content = $row['content'];
$post_tags = $row['tag'];
$post_status = $row['status'];
$img_ext = explode(".", $post_image);
?>
<!-- Page Content -->
<div class="container">
<div class="row">
<!-- Blog Post Content Column -->
<div class="col-lg-8">
<!--First Post -->
<hr>
<p>
<h2><a href="#"><?php echo $post_title; ?></a></h2></p>
<p>
<h3>by <a href="#"><?php echo $post_author; ?></a></h3></p>
<p><span class="glyphicon glyphicon-time"></span>Posted on <?php echo $post_date; ?></p>
<hr>
<?php if ($img_ext == "mp4"): ?>
<video controls width="400" height="300">
<source src="allpostpics/<?php echo $post_image; ?>" type="video/mp4">
Video tag is not supported in this browser.
</video>
<?php else : ?>
<img class="img-responsive img-rounded" src="allpostpics/<?php echo $post_image; ?>"
alt="900 * 300">
<?php endif; ?>
<hr>
<p><?php echo $post_content; ?></p>
</div>
<!-- Blog Sidebar Widgets Column -->
<div class="col-md-4">
<?php include 'includes/sidebar.php'; ?>
</div>
</div>
<!-- /.row -->
<hr>
<!-- Footer -->
<?php include 'includes/footer.php'; ?>
</div>
<!-- /.container -->
<!-- jQuery -->
<script src="js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>
<?php
} // End while
} // End if
注意:您的代码缺少
<?php endif; ?>
,并且末尾有两个大括号。