LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   php parse error (https://www.linuxquestions.org/questions/linux-newbie-8/php-parse-error-830277/)

zev42 09-04-2010 03:06 AM

php parse error
 
I too hit a brick wall with a parse error, but it was over a search engine php, so a ton of code to un ravel, being in php by only 4 days, i figured i'd take it to an expert heres the code:

Code:

<?php

$submit = $_POST["submit"];

$keywords = $_POST["keywords"];

if(isset($submit) || isset($keywords))

{

doSearch($keywords);

}

else

{

getKeywords();

}

function getKeywords()

{

?>

<html>

<head>

<title> Enter Search Keywords </title>

</head>

<body bgcolor="#000000">

<form name="frmKW" action="srh.php" method="post">


<input type="text" name="keywords" maxlength="100" border="#ff6600">
<input type="submit" name="submit" value="Search" bgcolor="#222222" border="#ff6600">

</form>

</body>

</html>

<?php

}

{

}

function doSearch($search_keywords)

{
$arrWords = explode(" ", $search_keywords);

if(sizeof($arrWords) == 0 || $search_keywords == "")

{

echo "search for blank engaged<br>";

echo "<a href='searchdocs.php'>Go Back</a>";

}

else

{

// Connect to the database

$dServer = "localhost";

$dDb = "content";

$dUser = "admin";

$dPass = "password";

$s = @mysql_connect($dServer, $dUser, $dPass)

or die("Couldn't connect to database server");

@mysql_select_db($dDb, $s)

or die("Couldn't connect to database");


for($i = 0; $i < sizeof($arrWords); $i++)

{

$query = "select articleIds from searchWords where word = '{$arrWords[$i]}'";

$result = mysql_query($query);

if(mysql_num_rows($result) > 0)

{

// Get the id's of the articles

$row = mysql_fetch_array($result);

$arrIds = explode(",", $row[0]);

$arrWhere = implode(" OR articleId = ", $arrIds);

$aQuery = "select articleId, title, left(content, 100) as summary from articles where articleId = " . $arrWhere;

$aResult = mysql_query($aQuery);

$count = 0;

$articles = array();

if(mysql_num_rows($aResult) > 0)

{

while($aRow = mysql_fetch_array($aResult))

{

$articles[$count] = array (

"articleId" => $aRow["articleId"],

"title" => $aRow["title"],

"summary" => $aRow["summary"]

);

$count++;

}

}

if(isset($articles))

{

$articles = array_unique($articles);



echo "<h1>" . sizeof($articles);

echo (sizeof($articles) == 1 ? " article" : " articles");

echo " found:</h1>";



foreach($articles as $a => $value)

{

?>

<a href="article.php?articleId=<?php echo $articles[$a]["articleId"]; ?>">

<b><u><?php echo $articles[$a]["title"]; ?></u></b>

</a>

<br><?php echo $articles[$a]["summary"] . "..."; ?>

<br>

<a href="article.php?articleId=<?php echo $articles[$a]; ?>">

http://www.mysite.com/article.php?articleId=<?php echo $articles[$a]["articleId"]; ?>

</a>

<br><br>

<?php

}

}

else

{

echo "No results found for '$search_keywords'<br>";

echo "<a href='searchdocs.php'>Go Back</a>";

}



}

}

}

?>


Wim Sturkenboom 09-04-2010 03:59 AM

Please post your code between [code] and [/code] tags. It makes it far easier to read as it will maintain indentations.

Below my attempt to get YOUR indentations right; I might have made a mistake.

Code:

<?php

$submit = $_POST["submit"];
$keywords = $_POST["keywords"];
if(isset($submit) || isset($keywords))
{
    doSearch($keywords);
}
else
{
    getKeywords();
}

function getKeywords()
{
?>
    <html>
    <head>
    <title> Enter Search Keywords </title>
    </head>
    <body bgcolor="#000000">
    <form name="frmKW" action="srh.php" method="post">
    <input type="text" name="keywords" maxlength="100" border="#ff6600">
    <input type="submit" name="submit" value="Search" bgcolor="#222222" border="#ff6600">
    </form>
    </body>
    </html>
<?php
}

{

}

function doSearch($search_keywords)
{
    $arrWords = explode(" ", $search_keywords);
    if(sizeof($arrWords) == 0 || $search_keywords == "")
    {
        echo "search for blank engaged<br>";
        echo "<a href='searchdocs.php'>Go Back</a>";
    }
    else
    {
        // Connect to the database
        $dServer = "localhost";
        $dDb = "content";
        $dUser = "admin";
        $dPass = "password";
        $s = @mysql_connect($dServer, $dUser, $dPass) or die("Couldn't connect to database server");
        @mysql_select_db($dDb, $s) or die("Couldn't connect to database");

        for($i = 0; $i < sizeof($arrWords); $i++)
        {
            $query = "select articleIds from searchWords where word = '{$arrWords[$i]}'";
            $result = mysql_query($query);
            if(mysql_num_rows($result) > 0)
            {
                // Get the id's of the articles
                $row = mysql_fetch_array($result);
                $arrIds = explode(",", $row[0]);
                $arrWhere = implode(" OR articleId = ", $arrIds);
                $aQuery = "select articleId, title, left(content, 100) as summary from articles where articleId = " . $arrWhere;
                $aResult = mysql_query($aQuery);
                $count = 0;
                $articles = array();
                if(mysql_num_rows($aResult) > 0)
                {
                    while($aRow = mysql_fetch_array($aResult))
                    {
                        $articles[$count] = array (
                        "articleId" => $aRow["articleId"],
                        "title" => $aRow["title"],
                        "summary" => $aRow["summary"]
                        );
                        $count++;
                    }
                }

                if(isset($articles))
                {
                    $articles = array_unique($articles);
                    echo "<h1>" . sizeof($articles);
                    echo (sizeof($articles) == 1 ? " article" : " articles");
                    echo " found:</h1>";
                    foreach($articles as $a => $value)
                    {
?>
                        <a href="article.php?articleId=<?php echo $articles[$a]["articleId"]; ?>">
                        <b><u><?php echo $articles[$a]["title"]; ?></u></b>
                        </a>
                        <br><?php echo $articles[$a]["summary"] . "..."; ?>
                        <br>
                        <a href="article.php?articleId=<?php echo $articles[$a]; ?>">
                        http://www.mysite.com/article.php?articleId=<?php echo $articles[$a]["articleId"]; ?>
                        </a>
                        <br><br>
<?php
                    }
                }
                else
                {
                    echo "No results found for '$search_keywords'<br>";
                    echo "<a href='searchdocs.php'>Go Back</a>";
                }

            }
        }
    }

?>

From what I see now, you're missing a curly

zev42 09-04-2010 04:33 AM

wow.. thanks much!

hrmm,
did you add it?

the semantics are wonderous! but im still receiving a error on the final line.. could it also be because most of my pages are .htm's and the article it requests is article.php?

aside, im sorry if i in any way imlied this is MY code or that i constructed it, obvious enough see :D it was a public doc tutorial, and i'd just about given up on searchbars lol

again, much thanks for the assitence, i grok your talent!!

Wim Sturkenboom 09-04-2010 04:44 AM

No, I did not add it; should be before the last '?>' to close the function doSearch

No, it's not because of php versus htm as you get a parse error indicating that PHP processes it

No, I did not say it was your code although it is the code that you posted and therefore the reference to YOUR ;) Else it gets to difficult to explain :)

If you use an editor with syntax highlighting, you might see it.

PS
There is a dedicated programming section at LQ

PPS
If your problem is solved, please mark it as such using the thread tools just above the first post.

zev42 09-04-2010 05:08 AM

THANKS MUCH!!

lol, the document reads perfectly, now i have a couldnt connect to database server, dont stress, i can google and check the programming link, thaks again sir!


All times are GMT -5. The time now is 04:45 AM.