for循环功能的问题

时间:2013-09-27 11:48:50

标签: php foreach

我在显示帖子时遇到问题。

运行:

  • Apache ver。 2.2.19(Unix)
  • PHP版本5.2。*

显示错误:“解析错误:语法错误,第22行意外T_STRING index.php”

下面是php脚本:

<?php
include('includes/connect_to_mysql.php');   
?>
<?php include_once('functions/functions.php');?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>magazin</title>
<link href="../style.css" rel="stylesheet" type="text/css" />


</head>

<body>


<?php 


$posts = get_posts(); 

?>


<?php 

foreach( $posts as $post){

  <article>

       <div class="three columns alpha thumbnail">

               <figure><img src="images/<?php echo $post[`posts_id`]; ?>.jpg" alt="<?php echo $post[`posts_title`]; ?>" /></figure>

     </div><!--three-->

          <div class="seven columns omega">

           <h2><a href = "single.php?id=<?php echo $post[`posts_id`]; ?>"><?php echo $post[`posts_title`]; ?></a></h2>

              <p class="meta">Posted by<a href = "<?php echo $post[`posts_id`]; ?>"><?php echo $post[`posts_user`]; ?><?php echo $total_comments; ?></a></p>

           <p><?php echo $post[`contents`]; ?></p>
    </div><!--seven--><hr/>

 </article><!--blog post-->

    }
<?php 
  } 
  ?> 

</body>
</html>

5 个答案:

答案 0 :(得分:1)

foreach( $posts as $post){

  <article>

问题出在这里。

你不能像这样在PHP中使用HTML。

通过这样做来解决它:

foreach( $posts as $post) { ?>

<article>

并在必要时重新打开PHP标记。

另一种方法是在HTML上使用引号:

foreach( $posts as $post) {

   echo '<article>';

答案 1 :(得分:0)

如果没有回显或打印出来,您无法直接在PHP中输出HTML。这表明您可能需要阅读有关PHP基础知识的更多信息。你可能意味着这个结构:

<?php if (something) { ?>

<div>html output here</div>

<?php } ?>

答案 2 :(得分:0)

您忘记在?>

之前关闭<article>

以下是更正后的代码: 您忘记在?>

之前关闭<article>

以下是更正后的代码:

    <?php
    include('includes/connect_to_mysql.php');   
    ?>
    <?php include_once('functions/functions.php');?>


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>magazin</title>
    <link href="../style.css" rel="stylesheet" type="text/css" />


    </head>

    <body>


    <?php 


    $posts = get_posts(); 

    ?>


    <?php 

    foreach( $posts as $post){
// HERE PHP tag wasn't closed.    
?>
      <article>

           <div class="three columns alpha thumbnail">

                   <figure><img src="images/<?php echo $post[`posts_id`]; ?>.jpg" alt="<?php echo $post[`posts_title`]; ?>" /></figure>

         </div><!--three-->

              <div class="seven columns omega">

               <h2><a href = "single.php?id=<?php echo $post[`posts_id`]; ?>"><?php echo $post[`posts_title`]; ?></a></h2>

                  <p class="meta">Posted by<a href = "<?php echo $post[`posts_id`]; ?>"><?php echo $post[`posts_user`]; ?><?php echo $total_comments; ?></a></p>

               <p><?php echo $post[`contents`]; ?></p>
        </div><!--seven--><hr/>

     </article><!--blog post-->
    <?php
        }
    <?php 
      } 
      ?> 

    </body>
    </html>

答案 3 :(得分:0)

您需要在foreach( $posts as $post){

之后附上php标记

答案 4 :(得分:0)

使用html代码时必须关闭php标记 像那样

foreach行之后的行“?&gt;”

<?php 

    foreach( $posts as $post){
    ?>

      <article>

           <div class="three columns alpha thumbnail">

                   <figure><img src="images/<?php echo $post[`posts_id`]; ?>.jpg" alt="<?php echo $post[`posts_title`]; ?>" /></figure>

         </div><!--three-->

              <div class="seven columns omega">

               <h2><a href = "single.php?id=<?php echo $post[`posts_id`]; ?>"><?php echo $post[`posts_title`]; ?></a></h2>

                  <p class="meta">Posted by<a href = "<?php echo $post[`posts_id`]; ?>"><?php echo $post[`posts_user`]; ?><?php echo $total_comments; ?></a></p>

               <p><?php echo $post[`contents`]; ?></p>
        </div><!--seven--><hr/>

     </article><!--blog post-->


    <?php 
      } 
      ?>