Page 1 of 1

search engine cuestion

Posted: Mon Jun 22, 2009 10:12 am
by shoyle
i'm trying to make a search engine and it works exept for one detail, when i write a keyword it gives me the results, sopose: u searched for= "google"
1 results= http://Www.google.com
but i dont know how to turn that result into a real link that u can actually clik on and will send you to the page..

Code: Select all

  
   1. <?php
   2.  
   3. // Get the search variable from URL
   4.  
   5. $var = @$_GET['q'] ;
   6. $trimmed = trim($var); //trim whitespace from the stored variable
   7.  
   8. // rows to return
   9. $limit=10;
  10.  
  11. // check for an empty string and display a message.
  12. if ($trimmed == "")
  13. {
  14. echo "<p>Please enter a search...</p>";
  15. exit;
  16. }
  17.  
  18. // check for a search parameter
  19. if (!isset($var))
  20. {
  21. echo "<p>We dont seem to have a search parameter!</p>";
  22. exit;
  23. }
  24.  
  25. //connect to your database ** EDIT REQUIRED HERE **
  26. mysql_connect("mysql host","user","pass"); //(host, username, password)
  27.  
  28. //specify database ** EDIT REQUIRED HERE **
  29. mysql_select_db("database") or die("Unable to select database"); //select which database we're using
  30.  
  31. // Build SQL Query
  32. $query = "select * from searchengine where keywords like \"%$trimmed%\"
  33. order by id"; // EDIT HERE and specify your table and field names for the SQL query
  34.  
  35. $numresults=mysql_query($query);
  36. $numrows=mysql_num_rows($numresults);
  37.  
  38. // If we have no results, offer a google search as an alternative
  39.  
  40. if ($numrows == 0)
  41. {
  42. echo "<h4>Results</h4>";
  43. echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";
  44. }
  45.  
  46. // next determine if s has been passed to script, if not use 0
  47. if (empty($s)) {
  48. $s=0;
  49. }
  50.  
  51. // get results
  52. $query .= " limit $s,$limit";
  53. $result = mysql_query($query) or die("Couldn't execute query");
  54.  
  55. // display what the person searched for
  56. echo "<p>You searched for: "" . $var . ""</p>";
  57.  
  58. // begin to show results set
  59. echo "Results";
  60. $count = 1 + $s ;
  61.  
  62. // now you can display the results returned (i think its here somewhere where ihave to make the change)
  63. while ($row= mysql_fetch_array($result)) {
  64. $title = $row["url"];
  65.  
  66. echo "$count.)&nbsp;$title" ;
  67. $count++ ;
  68. }
  69.  
  70. $currPage = (($s/$limit) + 1);
  71.  
  72. //break before paging
  73. echo "<br />";
  74.  
  75. // next we need to do the links to other results
  76. if ($s>=1) { // bypass PREV link if s is 0
  77. $prevs=($s-$limit);
  78. print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\"><<
  79. Prev 10</a>&nbsp&nbsp;";
  80. }
  81.  
  82. // calculate number of pages needing links
  83. $pages=intval($numrows/$limit);
  84.  
  85. // $pages now contains int of pages needed unless there is a remainder from division
  86.  
  87. if ($numrows%$limit) {
  88. // has remainder so add one page
  89. $pages++;
  90. }
  91.  
  92. // check to see if last page
  93. if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
  94.  
  95. // not last page so give NEXT link
  96. $news=$s+$limit;
  97.  
  98. echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
  99. }
 100.  
 101. $a = $s + ($limit) ;
 102. if ($a > $numrows) { $a = $numrows ; }
 103. $b = $s + 1 ;
 104. echo "<p>Showing results $b to $a of $numrows</p>";
 105.  
 106. ?>

Re: search engine cuestion

Posted: Mon Jun 22, 2009 10:15 am
by onion2k
You're writing a search engine but you don't know how to make HTML links? 8O

Re: search engine cuestion

Posted: Mon Jun 22, 2009 10:28 am
by mattpointblank
Change line 66 from:

Code: Select all

 
echo "$count.)&nbsp;$title" ;
 
To:

Code: Select all

 
echo "$count.)&nbsp; <a href=\"$title\">$title</a>" ;
 
I agree with onion2k though...

Re: search engine cuestion

Posted: Mon Jun 22, 2009 1:03 pm
by shoyle
onion2k wrote:You're writing a search engine but you don't know how to make HTML links? 8O
Thanks for your helpless answer, no i dont know how to put it in php or how to make a link if i would i wouldent make this cuestion genious.., this is a copy-paste code from a php tutorial

Re: search engine cuestion

Posted: Mon Jun 22, 2009 1:11 pm
by shoyle
mattpointblank wrote:Change line 66 from:

Code: Select all

 
echo "$count.)&nbsp;$title" ;
 
To:

Code: Select all

 
echo "$count.)&nbsp; <a href=\"$title\">$title</a>" ;
 
I agree with onion2k though...
nope, still wont work: in my database i got it as: http://www.google.com
i tryed : http://www.google.com

but none of them work.. :banghead:

this is what i get:

You searched for: "google"
Results.) .) http://www.google.com

Showing results 1 to 1 of 1

Re: search engine cuestion

Posted: Mon Jun 22, 2009 1:14 pm
by Eric!
You asked the same question here and it has been answered:
viewtopic.php?f=1&t=101984

But you still need to SHOW us your HTML output. So if you are using Internet Explorer, run your script, then when you get the results, go to VIEW-> SOURCE, then copy and paste that here to this forum.

Re: search engine cuestion

Posted: Mon Jun 22, 2009 2:35 pm
by shoyle
yes i did, but what i had for answer didint work :S
is this qhat u're looking for?

Code: Select all

<p>You searched for: "inicio"</p>Results.)&nbsp; <a href=""></a>.)&nbsp;http://www.inmosuhr.com<br /><p>Showing results 1 to 1 of 1</p>

Re: search engine cuestion

Posted: Mon Jun 22, 2009 4:47 pm
by Eric!
It didn't work because we are trying to guess at your problem. We don't know what is in your variables. You don't need to make a new post just to ask the same question again.

Now that we can see your HTML format, we can see what is inside your variables. Your HTML format is wrong, it doesn't match your posted code, and some of your variable are empty.

line 67 (going by the outer set of line numbers) should be:

echo "$count.)&nbsp; <a href=\"$title\">$title</a>" ;

Line 79 and Line 99 -- what are they doing? Which line is getting echoed?

The variable you are trying to set to a link is empty. I think you might have a typo or something in the code you are actually running, versus the code you posted. Can you repost the code that you used to generate the HTML code?

Here is what is wrong with your output. You need a <br> after the results. Is the "inicio" what you are searching for? If so, that that variable is working. Your count variable is blank, one of your title URL variables is blank.

Your Code:
<p>You searched for: "inicio"</p>Results.)&nbsp; <a href=""></a>.)&nbsp;http://www.inmosuhr.com<br /><p>Showing results 1 to 1 of 1</p>

Your problems:
1.<p>You searched for: "[is this what you are searching for:"inicio"]"</p>Results[nothing shown for $count].)&nbsp; <a href="[this is where the http://www.inmosuhr.com link should appear, but your variable is blank]"></a>[
".)" what is this .)? somthing is missing here too]
&nbsp;http://www.inmosuhr.com<br /><p>Showing results 1 to 1 of 1</p>

Your HTML should look like (when searching for Godzilla):
<p>You searched for: "Godzilla"</p>Results.<br>1)&nbsp; <a href="http://www.inmosuhr.com">Click here to go to http://www.inmosuhr.com</a><br /><p>Showing results 1 to 1 of 1</p>

Re: search engine cuestion

Posted: Mon Jun 22, 2009 5:57 pm
by shoyle
Well, :D to be 100% honest i dont understan line 79 or 99.. (i barely understand the other hundred..) but anyways heres my code:

Code: Select all

 
<?php
 
  // Get the search variable from URL
 
  $var = @$_GET['q'] ;
  $trimmed = trim($var); //trim whitespace from the stored variable
 
// filas para el resultado
$limit=10; 
 
// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<p>Ingrese una palabra...</p>";
  exit;
  }
 
// check for a search parameter
if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }
 
//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("mysql","user","pass"); //(host, username, password)
 
//specify database ** EDIT REQUIRED HERE **
mysql_select_db("database") or die("Unable to select database"); //select which database we're using
 
// Build SQL Query  
$query = "select * from searchengine where keywords like \"%$trimmed%\"  
  order by id"; // EDIT HERE and specify your table and field names for the SQL query
 
 $numresults=mysql_query($query);
 $numrows=mysql_num_rows($numresults);
 
// If we have no results, offer a google search as an alternative
 
if ($numrows == 0)
  {
  echo "<h4>Results</h4>";
  echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";
 
// google
 echo "<p><a href=\"http://www.google.com/search?q=" 
  . $trimmed . "\" target=\"_blank\" title=\"Look up 
  " . $trimmed . " on Google\">Click here</a> to try the 
  search on google</p>";
  }
 
// next determine if s has been passed to script, if not use 0
  if (empty($s)) {
  $s=0;
  }
 
// get results
  $query .= " limit $s,$limit";
  $result = mysql_query($query) or die("Couldn't execute query");
 
// display what the person searched for
echo "<p>You searched for: "" . $var . ""</p>";
 
// begin to show results set
echo "Results";
echo "$count.)&nbsp; <a href=\"$title\">$title</a>" ;
 
// now you can display the results returned
  while ($row= mysql_fetch_array($result)) {
  $title = $row["url"];
 
  echo "$count.)&nbsp;$title" ;
  $count++ ;
  
  }
 
$currPage = (($s/$limit) + 1);
 
//break before paging
  echo "<br />";
 
  // next we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\"><< 
  Prev 10</a>&nbsp&nbsp;";
  }
 
// calculate number of pages needing links
  $pages=intval($numrows/$limit);
 
// $pages now contains int of pages needed unless there is a remainder from division
 
  if ($numrows%$limit) {
  // has remainder so add one page
  $pages++;
  }
 
// check to see if last page
  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
 
  // not last page so give NEXT link
  $news=$s+$limit;
 
  echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
  }
 
$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $numrows</p>";
  
?>
 
and im trying to figure out what u said but.. i pasted this form a tutorial and dont know a thing about php :D

Re: search engine cuestion

Posted: Mon Jun 22, 2009 6:37 pm
by Eric!
I don't think I can teach you everything you need to know for this level of an example through message posts. I suggest you try much simpiler examples until you can tackle this one.

I will however correct your code so that some of your results will show up as you want. However there are still many bugs left for you to sort out.

Replace lines 65-76 with this

Code: Select all

// begin to show results set
echo "Results<br>";
 
// now you can display the results returned
  $count=1;
  while ($row= mysql_fetch_array($result)) {
  $title = $row["url"];
  echo "$count.)<a href=\"$title\">Click here to go to $title</a><br>" ;
  $count++ ;
  
  }
 

Re: search engine cuestion

Posted: Mon Jun 22, 2009 7:01 pm
by shoyle
Eric! wrote:I don't think I can teach you everything you need to know for this level of an example through message posts. I suggest you try much simpiler examples until you can tackle this one.

I will however correct your code so that some of your results will show up as you want. However there are still many bugs left for you to sort out.

Replace lines 65-76 with this

Code: Select all

// begin to show results set
echo "Results<br>";
 
// now you can display the results returned
  $count=1;
  while ($row= mysql_fetch_array($result)) {
  $title = $row["url"];
  echo "$count.)<a href=\"$title\">Click here to go to $title</a><br>" ;
  $count++ ;
  
  }
 

Genious!! it worked perfectly!! thanks a lot :D its true i know very little but im trying to learn :D i've done some websites but still got most to learn teh site i had on my html code http://www.inmosuhr.com was done all by me but as u can see its got very small coding..

thanks again dude

Re: search engine cuestion

Posted: Mon Jun 22, 2009 7:24 pm
by shoyle
i still get problem with line 116 which is empty :s i dont get it :S

Re: search engine cuestion

Posted: Mon Jun 22, 2009 7:40 pm
by shoyle
yup cant understand why error on line 116:

Parse error: syntax error, unexpected $end in /home/pumsho/pumsho.com/search.php on line 116

if anyone has a good search engine code i will super thank u :S