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
https://forums.phpfreaks.com/topic/124759-if-condition-is/
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
https://forums.phpfreaks.com/topic/124759-if-condition-is/#findComment-644491
Share on other sites

I don't understand, you CAN do mysql_num_rows($result) == 1, but it will only evaluate to true when, as you expect, the number of results is 1.

 

Perhaps you're confusing what mysql_num_rows() does, it returns an int, not bool, the int is the number of results from the query.

Link to comment
https://forums.phpfreaks.com/topic/124759-if-condition-is/#findComment-644526
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
https://forums.phpfreaks.com/topic/124759-if-condition-is/#findComment-644809
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.