我正在使用安装了Apache的CentOS 6.4。我有一个名为cmsSearch.php的单个php文件。在文件顶部,我有可以正常执行(查询Sphinx搜索索引)的PHP代码。但是,在下面的HTML中尝试运行的任何PHP(尝试运行foreach并使用Sphinx Search结果填充表格)只会在控制台(Chrome)中被注释掉。以下是整个cmsSearch.php文件:
<?php
require("sphinxapi.php");
if($_POST['keyword'])
{
$keyword = $_POST['keyword'];
$client = new SphinxClient();
$client->SetMatchMode(SPH_MATCH_ANY);
$result = $client->Query($keyword, "staffDirectoryMember");
if(!$result)
{
print "ERROR: " . $client->GetLastError();
}
else
{
var_dump($result["matches"]);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Sphinx Test</title>
<style>
.container
{
border: solid 1px black;
height: 350px;
width: 700px;
margin: auto;
padding: 5px;
}
.output
{
margin-top:20px;
border: solid 1px red;
height: 200px;
}
</style>
</head>
<body>
<?echo "TEST"; ?>
<div class="container">
<div>
<form method="post" action="cmsSearch.php">
<input type="text" name="keyword">
<input type="submit" value="Search">
</form>
<div class="output">
<? echo "test2"; ?>
<table>
<thead>
<tr>
<th>ID</th>
<th>Weight</th>
<th>ClientId</th>
<th>DomainId</th>
<th>ContentTypeId</th>
</tr>
</thead>
<tbody>
<?
echo "Above for loop";
foreach($result["matches"] as $match)
{
echo "Print from for loop:";
var_dump($match);
?>
<!-- <tr>
<td><?=$match[id]?></td>
<td><?=$match[weight]?></td>
<td><?=$match[attrs][clientid]?></td>
<td><?=$match[attrs][domainid]?></td>
<td><?=$match[attrs][contenttypeid]?></td>
</tr> -->
<?}
echo "After for loop";
?>
</tbody>
</table>
</div>
</div>
</div>
我不确定为什么PHP在顶部正常执行(我可以输出并且var dump也可以工作),但是放在HTML中的任何PHP都只显示为注释,而不起作用。有人有想法吗?
<!--
和-->
是 HTML 的注释标记。您正在注释掉生成的 HTML。 - Justin Wood<?php
代替<?
了吗? - bfavaretto