Leveticus
Leveticus

Reputation: 33

PHP code being commented out

I'm using CentOS 6.4 with apache installed. I have a single php file called cmsSearch.php. At the top of the file I have PHP that executes fine (queries a Sphinx Search index). But, any php I try to run in the HTML that is below (trying to run a foreach and populate a table with the results of the Sphinx Search) just gets commented out when I view the page in the console (Chrome). Here is the whole cmsSearch.php file:

<?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>

Not sure why the php executes at the top fine (i can echo out and the var dump works), but then any php put in the HTML just shows as comments and doesn't do anything. Anyone have an idea?

Upvotes: 3

Views: 4269

Answers (2)

Jeremy Harris
Jeremy Harris

Reputation: 24549

Your PHP contained inside the HTML is using short tags which can be turned off in your php.ini file. Take a look at this directive and make sure it is set to true if you want to use them:

short_open_tag true

http://www.php.net/manual/en/ini.core.php#ini.short-open-tag

Upvotes: 4

Jasper
Jasper

Reputation: 75993

Try using <?php to start your PHP blocks, not <?... It's the difference between the blocks of code.

Upvotes: 1

Related Questions