Results 1 to 10 of 10

Thread: PHP/MySQL search engine.. [resolved]

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    PHP/MySQL search engine.. [resolved]

    Ok, I've made this search engine, and it works fine how it is, but I just want to add something to it. I want to add something to it so that it will count how many times it found the search query in every result it found. The search engine basically just looks in my MySQL database for the query, and then returns the results for you to be able to look over or whatever you want. I figured I wanted to be able to search through stuff and see how many times a word or whatever was found within the results, so now I'm trying to do that. Anyway, here's the raw code of the script I'm using:

    Note: basically, the things I'm looking inside of for the results are $j['j_subject'] and $j['j_post'].

    Another note: I realize my programming skills are probably pretty weak.. but it works. Feel free to clean it up or something, if you feel so obliged.

    PHP Code:
    <center>
      <table width="375" cellpadding="0" cellspacing="0" class="users">
        <tr width="100%">
          <td width="50" class="users-top" align="center">ID</td>
          <td width="200" class="users-top">Entry Title</td>
          <td width="75" class="users-top">Date</td>
          <td width="25" class="users-top">FND</td>
        </tr>
    <?
      $c = "SELECT COUNT(*) FROM $mysql[journal] WHERE j_subject LIKE '%" . $_GET['query'] . "%' OR j_post LIKE '%" . $_GET['query'] . "%'";
      $c = mysql_query($c);
      $c = mysql_fetch_array($c);

      $jq = "SELECT j_id, j_subject, j_post, j_date FROM $mysql[journal] WHERE j_subject LIKE '%" . $_GET['query'] . "%' OR j_post LIKE '%" . $_GET['query'] . "%'";
      $jq = mysql_query($jq);
      while($j = mysql_fetch_array($jq)){

        $found = 0;
        /* ADD HOW TO FIND VARIABLES IN HERE */

        echo "    <tr width=\"100%\">\n";
        echo "      <td width=\"50\" class=\"users-data\" align=\"center\">$j[j_id]</td>\n";
        echo "      <td width=\"200\" class=\"users-data\"><a href=\"?act=journal_comm&id=$j[j_id]\">$j[j_subject]</a></td>\n";
        echo "      <td width=\"75\" class=\"users-data\">$j[j_date]</td>\n";
        echo "      <td width=\"25\" class=\"users-data\">$found</td>\n";
        echo "    </tr>\n";
      }
      echo "    <tr width=\"100%\">\n";
      echo "      <td colspan=\"3\" class=\"users-bottom\">$c[0] entries found</td>\n";
      echo "    </tr>\n";
    ?>
      </table>
    </center>
    For all I know, it's something really simple that I could do and it's just not comming to me. There's alot on my mind so I'm trying to take my mind off of it with some programming but I'm just have some troubles.. Any help is appreciated.

    edit: removed some code that had page-specific formatting and some other stuff that used my own functions that might've confused anyone.. also removed some useless html formatting for easier reading
    Last edited by kows; Sep 17th, 2003 at 11:15 PM.
    Like Archer? Check out some Sterling Archer quotes.

  2. #2
    Lively Member
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    96
    I guess you want to use $found = 0; to count the times a word is found.

    And what do you wnat to find. the amount of words or the amount of articles. (which can contain the word many times)

    if articles, place the found thing Before the while and add on to it every loop $found++
    then you know the amount of articles..
    http://www.raketje.com coming..............soon................

  3. #3
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Are you wanting to count how many times $_GET['query'] shows up within the post/subject?

    If so, you can use preg_match():

    Code:
    $found = preg_match('/' . addslashes($_GET['query']) . '/i', $_GET['query']);
    Or something like that.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    yeah, sorry for being unclear, my head must've been full of stuff last night. I thought I mentioned what I was looking for, so basically I'm looking for $_GET['query'] in $j['j_subject'] and $j['j_post'], and I've already done that fine, but I want to count how many times it was found in it..

    I'll try that preg_match() in a minute, and edit my post to see if it worked.

    edit: ok, well I tried that preg_match, and it seems that it only returns as true or false, or 1 or 0. I did this within my while() loop to figure that out:

    PHP Code:
    $found_subj preg_match('/' addslashes($_GET['query']) . '/i'$j['j_subject']);
    $found_post preg_match('/' addslashes($_GET['query']) . '/i'$j['j_post']);
    $found $found_subj $found_post
    anyone else have any suggestions, or a way to do it with preg_match()?
    Last edited by kows; Sep 17th, 2003 at 10:34 AM.
    Like Archer? Check out some Sterling Archer quotes.

  5. #5
    Lively Member
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    96
    I did this within my while() loop to figure that out:

    this means it's being reset every time....
    again.. $found=0 before the while
    and the code in the while

    $found = $found + $found_subj + $found_post;
    http://www.raketje.com coming..............soon................

  6. #6
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Whoops. preg_match() will return just 0 or 1 because it stops searching after it finds a match. I forgot about that.

    What you want is preg_match_all(). That should return the number of matches. Just keep in mind what bekkel said and make sure it isn't being reset every time you loop.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    I've never really understood how preg* functions worked, and reading the PHP.net explanation doesn't give me much to run on on how it all works.. I used the base preg_match() I had before and added the the next parameter as $out, but then what? How would I count that up? count($out)? I'm too confused right now.
    Like Archer? Check out some Sterling Archer quotes.

  8. #8
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Code:
    while($j = mysql_fetch_array($jq)){ 
    
        $found += preg_match_all('/' . addslashes($_GET['query']) . '/i', $j['j_subject']);
        $found += preg_match_all('/' . addslashes($_GET['query']) . '/i', $j['j_post']);
    
        //the rest of your original code here
    }
    Is all you need to do.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    No, it's not all I have to do, because that's what I did the first time and it didn't work. That's why I looked it up on PHP.net and found out I needed another parameter, the output I guess, and that's where $out came from. I added the extra parameter, $out, and it didn't work after that either.

    The error I got was:

    Code:
    Warning: Wrong parameter count for preg_match_all() in c:\webroot\david\newsite\search_do.php on line 57
    Line 53 through 59:
    PHP Code:
    53    while($j mysql_fetch_array($jq)){
    54      
    55      
    56      $found 
    0;
    57      $found += preg_match_all('/' addslashes($_GET['query']) . '/i'$j['j_subject']);
    58      $found += preg_match_all('/' addslashes($_GET['query']) . '/i'$j['j_post']);
    59 
    Oh, by the way, bekkel, I'm not trying to count the number of articles I have, so I'm not resetting any variables while defining $found within my while() loop. $found has to be calculated for every result found, so that's why it's there in the first place. $found is for finding the number of matches of $_GET['query'] within the subject of the post or the actual content of the post.

    edit: When retrying the above I had with the extra parameter defined simply as $out, it worked fine. I guess I was doing something wrong the first time around, but anyway, thanks guy with no home!
    Last edited by kows; Sep 17th, 2003 at 11:16 PM.
    Like Archer? Check out some Sterling Archer quotes.

  10. #10
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Yeah, I checked the manual and I guess you do need a third parameter, even if you don't use it, which doesn't make sense to me.

    With preg_match(), however, the third parameter is optional.

    Glad you got it working.
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width