2013-09-27 46 views
-2

我在顯示我的帖子時出現問題。Prolem with for loop功能

運行:

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

錯誤顯示: 「解析錯誤:語法錯誤,意想不到的T_STRING的index.php上線22」

下面是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> 
+0

你有沒有語法錯誤? – aldanux

+0

不能先看看你的代碼,然後問這樣的問題? –

回答

1
foreach($posts as $post){ 

    <article> 

問題就在這裏。

你不能像這樣在PHP內部使用HTML。

這樣做修復:

foreach($posts as $post) { ?> 

<article> 

,並在必要時重新打開PHP標籤。

另一種方法是使用你的HTML報價:

foreach($posts as $post) { 

    echo '<article>'; 
+0

是的,你只需要在這裏添加一個關閉php標籤 –

+0

這不會僅僅因爲在'

'後面還有一個缺少的PHP開始標籤而解決它。 – insertusernamehere

+0

@insertusernamehere他學會了如何處理這個問題。我相信他可以自己解決剩下的問題。 :) – Aristona