Jump to content

if condition is ???


hailmunan

Recommended Posts

hello guys..

 

i dont know where to start so i`ll just paste my code here ok?

 

if( $_POST['keyword'] )
{
   /* Connect to the database: */
   mysql_pconnect("localhost","admin","password")
       or die("ERROR: Could not connect to database!");
   mysql_select_db("fyp");

   /* Get timestamp before executing the query: */
   $start_time = getmicrotime();

   /* Set $keyword and $results, and use addslashes() to
    *  minimize the risk of executing unwanted SQL commands: */
   $keyword = addslashes( $_POST['keyword'] );
   $results = addslashes( $_POST['results'] );

   /* Execute the query that performs the actual search in the DB: */
   $result = mysql_query(" SELECT p.page_url AS url,
                           COUNT(*) AS occurrences 
                           FROM page p, word w, occurrence o
                           WHERE p.page_id = o.page_id AND
                           w.word_id = o.word_id AND
                           w.word_word = \"$keyword\"
                           GROUP BY p.page_id
                           ORDER BY occurrences DESC
                           LIMIT $results " );

   /* Get timestamp when the query is finished: */
   $end_time = getmicrotime();

if ($result == true)

{


   /* Present the search-results: */
   print "<h2>Search results for '".$_POST['keyword']."':</h2>\n";
   for( $i = 1; $row = mysql_fetch_array($result); $i++ )
   {
      print "$i. <a href='".$row['url']."'>".$row['url']."</a>\n";
      print "(occurrences: ".$row['occurrences'].")<br><br>\n";
   }

}

else
{

print "<br> no keyword found ";

}

 

brief explanation..

actually the code is taken somewhere from the net.. its a search engine function

 

 

i edited the bold section a little bit.

 

the i added the if function..

it should work this way..

 

if the search function found something in the database.. it should presents the result

  if the search function did not found anything in the database.. (else) it should printed the " no keyword found"

 

but.. at the (else).. it appeared the

/* Present the search-results: */

without any results...

 

so how do i twist the if ($result == true) to something that is workable?

 

meaning..

if the the query did not found anything, it should appear the "no keyword found"

 

thanks for the help folks..

 

Link to comment
Share on other sites

wow, it works like a charm..

thanks mate.. so i did it like this..

 

 

if (mysql_num_rows($result) == 0)

{

  print "<br> no keyword found ";
   

}

else
{

/* Present the search-results: */
   print "<h2>Search results for '".$_POST['keyword']."':</h2>\n";
   for( $i = 1; $row = mysql_fetch_array($result); $i++ )
   {
      print "$i. <a href='".$row['url']."'>".$row['url']."</a>\n";
      print "(occurrences: ".$row['occurrences'].")<br><br>\n";
   }

}

 

 

 

 

why cant i do like this

if (mysql_num_rows($result) == 1)

and change the result in if end else..?

 

it wont work..

 

the result is "no keyword found" for both results..

 

thanks..

Link to comment
Share on other sites

understood..

 

i am now taking this to a new level..

i want to predefine few keywords and display it

how do i do that?

i have the idea of using array

 

but it doesnt seems working for  me..

 

heres my code

 

//if( $_POST['keyword'] )
//{
   /* Connect to the database: */
   mysql_pconnect("localhost","admin","password")
       or die("ERROR: Could not connect to database!");
   mysql_select_db("fyp");

   /* Get timestamp before executing the query: */
   $start_time = getmicrotime();

   /* Set $keyword and $results, and use addslashes() to
    *  minimize the risk of executing unwanted SQL commands: */
//  $keyword = addslashes( $_POST['keyword'] );
  
$keyword = array ("malaysia", "ssd");
    

   $results = addslashes( $_POST['results'] );

   /* Execute the query that performs the actual search in the DB: */
   $result = mysql_query(" SELECT p.page_url AS url,
                           COUNT(*) AS occurrences 
                           FROM page p, word w, occurrence o
                           WHERE p.page_id = o.page_id AND
                           w.word_id = o.word_id AND
                           w.word_word = \"$keyword\"
                           GROUP BY p.page_id
                           ORDER BY occurrences DESC " );

   /* Get timestamp when the query is finished: */
   $end_time = getmicrotime();

if (mysql_num_rows($result) == 0)

{
print "<h2>Search results for '".$keyword[0]."' :</h2>\n";
  print "<br> Keyword not found <br>";
}

else
{

/* Present the search-results: */
   print "<h2>Search results for ".$keyword." :</h2>\n";
   for( $i = 1; $row = mysql_fetch_array($result); $i++ )
   {
      print "$i. <a href='".$row['url']."'>".$row['url']."</a>\n";
      print "(occurrences: ".$row['occurrences'].")<br><br>\n";
   }

}

   /* Present how long it took the execute the query: */
   print "<br> query executed in ".(substr($end_time-$start_time,0,5))." seconds.";
//}
//else
//{
//   /* If no keyword is defined, present the search page instead: */
//   print "<form method='post'> Keyword: 
//          <input type='text' size='20' name='keyword'>\n";
//   print "Results: <select name='results'><option value='5'>5</option>\n";
//   print "<option value='10'>10</option><option value='15'>15</option>\n";
//   print "<option value='20'>20</option></select>\n";
//
//   print "<input type='submit' value='Search'></form>\n";
//}

print "</body></html>\n";

 

 

instead.. the result turn out another way.. [refer attachment]

it displays as the keyword is "Array"

 

where did i went wrong on this?

 

and how do i display few &keyword after the result?

 

example:

 

$keyword[0]  - found

$keyword[1]  - not found

$keyword[2]  - not found

$keyword[3]  - found..

 

 

help is much appreciated..

 

thanks

 

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.