2012-07-19 100 views
1

所以我有一些表中我想用来连接的行。我也有一个运行查询来计算评论的文件。我想从该文件中提取该号码。php连接以显示来自另一个文件的数据

我是用直线html做的,但是我的网站变得太大了,所以我试图用php和mysql来动态化它。

因此,与我的HTML它的硬编码的工作原理:

<?php include_once("comments/post_title/tpost_title.php") ?> 

所以,我想我可能只是串连它是相同的,但由于某些原因,我的网页浏览器(Chrome)认为它的评论。

PHP的:

$query = "SELECT post_title FROM sessions"; 
$result = mysql_query($query); 

while($row = mysql_fetch_assoc($result)) 

{ 
    echo "<a href=\"#" .strip_tags("{$row['post_title']}"). "\">read comment"; 
    echo "<?php include_once(\"comments/" .strip_tags("{$row['post_title']}")."/t". strip_tags("{$row['post_title']}"). ".php\") ?>"; 
    echo "</a>"; 
} 

我读过有关 “@file_get_contents”,但我的理解它是有限的。据我所知它必须设置为一个变量,然后我将不得不将它插入我的while循环。但我迷失在这个变量如何改变我的表中的下一行来拉下一个post_title。

我想另外一个选择是把查询放在这个文件中,这个文件将注释统计到这个循环中,但是我必须把一个变量放到查询中。 (这是“好”的编码?)举例说:

$query = "SELECT post_title FROM sessions WHERE session = 'variable'; 

在此先感谢帮助和洞察力。

回答

1

要在循环读取这个文件,你需要:

while($row = mysql_fetch_assoc($result)) 
{ 
    echo "<a href=\"#" .strip_tags("{$row['post_title']}"). "\">read comment"; 

    include_once("comments/" .strip_tags("{$row['post_title']}")."/t". strip_tags("{$row['post_title']}"). ".php"); 
    echo "</a>"; 
} 

如果你读了文件不是PHP,但直接的HTML,那么你就可以使用ReadFile的(),而包括()。语法是一样的。

可以肯定的,不过,这将是更好的像,

$file = "comments/" .strip_tags("{$row['post_title']}")."/t". strip_tags("{$row['post_title']}"). ".php"; 
    if (file_exists($file)) 
     readfile($file); 
    else 
     echo "ERROR"; 
相关问题