3

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?

6
  • <!-- and --> are HTML comment markers. You are commenting out the resulting HTML. Commented Sep 18, 2013 at 19:59
  • Sorry, i should have mentioned, i commented out the td elements because i thought it was causing an error when trying to access the $match array. I am wondering why none of the echos or any of the php inside the HTML isn't working. It just all gets commented out. Commented Sep 18, 2013 at 20:03
  • Do you have errors in your PHP logs? Commented Sep 18, 2013 at 20:04
  • Yes, the only thing coming out is the 'keyword' index not being defined upon initial loading of the page. After i submit a text value that is never seen again. Commented Sep 18, 2013 at 20:06
  • And did you try using <?php instead of <? as suggested below? Commented Sep 18, 2013 at 20:07

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

This was the problem. The code in the short tags is now working. Once this flag was set to "On".
@Leveticus Think about not using short opening tags... If you now move this code to another system it'll most likely break again unless you again update the configuration.
I changed the tags to not use short openings. I noticed your comment below that was directed to bzupnick and made the change. Thanks for the tip. I would upvote you if I had enough rep to do it.
1

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

5 Comments

<?= is just fine to start PHP. It is short syntax for <?php echo
@bzupnick No, its turned off in a lot of environments, my understanding is that it's best practice to always use <?php.
@JustinWood It's not really fine. Use of short tags is not recommended, seeing as your code could blow up on certain platforms that don't have it enabled in their php.ini.
@Marcel I'm not disagreeing with that. I am just stating that wouldn't be the reason his code would be 'commented' out. That would be better suited for a comment, not an answer.
@JustinWood This may not answer everything asked, I was attempting to answer why the subsequent PHP code blocks weren't parsing correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.