Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    New Coder
    Join Date
    Aug 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    multple keyword search and multiple search inputs

    I need help. I will say that I am not a php coder but I am forced to develop things on my own. I have a MySQL database of basketball drills with the following fields: category, title, description, number of players, keywords, and an image.

    I want to search in probably 3 different ways:

    1. Search by keyword. I have php code that successfully returns results when I put in a single keyword.

    $search = mysql_real_escape_string(trim($_POST['searchterms']));
    $query=mysql_query("SELECT * FROM Drills WHERE KEYWORDS LIKE '%$search%'");

    but I can't return things if I type in more than keyword. should keywords be separated by commands in the database? I first did then took the commas out. So I need help if people type in more than one keyword.

    2. I will also want people to be able to search by a category dropdown. Again, successful with that..

    3. Will want people to be able to choose category + keyword + the number of players required (possible dropdown). So basically multiple search inputs to come up with drills for example that might involve ball-handling, passing and require 6 players....

    I have done a lot of googling and pieced together code that does things in isolation but really need help with the overall effort. I can email or interact with people who can help me put together this effort.

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    you could check if a space existed in the $_POST['searchterms'] if so explode by space
    and contruct like $search[1] OR like $search[2] etc
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #3
    New Coder
    Join Date
    Aug 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    response

    are you saying identify specific keywords to search for. right now I currently populate a variable with whatever is entered in the search field. DO you mean literally $search[1].

    I am learning the explode and implode concepts. so that is hazy .

  • #4
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    try something like:

    PHP Code:
    $search mysql_real_escape_string(trim($_POST['searchterms']));
        if(
    stristr($search' ') === FALSE) {
            
    $keywords[1] = $search;
          }else{
            
    $keywords explode(' ',$search);
        }
        
    $searchterm '';
        foreach (
    $keywords as $keyword){
            
    $searchterm .= " OR `KEYWORDS` LIKE '%$keyword%' ";
        }
        echo 
    '<form method="post"><input name="searchterms" type="text"><input type="submit"></form>';
        
        
    $query=mysql_query("SELECT * FROM `drills` WHERE `KEYWORDS` LIKE '%$keywords[1]%' $searchterm");
        
        while (
    $row mysql_fetch_array($query)) {
        echo 
    $row['KEYWORDS'].'<br>';
        } 
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #5
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    p.s if you need to search multiple columns you need to use concat:

    say if your columns are

    id | keywords | players | technique | venue

    and you want to search say the middle 3 change the above script to:

    PHP Code:
    $search mysql_real_escape_string(trim($_POST['searchterms']));
        if(
    stristr($search' ') === FALSE) {
            
    $keywords[1] = $search;
          }else{
            
    $keywords explode(' ',$search);
        }
        
    $searchterm '';
        foreach (
    $keywords as $keyword){
            
    $searchterm .= " OR concat(`KEYWORDS`, `players`, `technique`) LIKE '%$keyword%' ";
        }
        echo 
    '<form method="post"><input name="searchterms" type="text"><input type="submit"></form>';
        
        
    $query=mysql_query("SELECT * FROM `drills` WHERE concat(`KEYWORDS`, `players`, `technique`) LIKE '%$keywords[1]%' $searchterm");
        
        while (
    $row mysql_fetch_array($query)) {
        echo 
    $row['KEYWORDS'].'<br>';
        } 
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #6
    New Coder
    Join Date
    Aug 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    still working on it

    thank you for the first set of code. I have not looked at the concat yet. I did get an error trying to adapt the first set of code you offered. It may be in the if section. The page can not displayed "due to programming error"

  • #7
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    post the code you have and i'll have a look at it
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #8
    New Coder
    Join Date
    Aug 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    of course this was trying to merge the code you offered me with what I had so it is a mix..probably something due to that. .the form is on a different html page.


    PHP Code:
    $search mysql_real_escape_string(trim($_POST['searchterms']));
        if(
    stristr($search' ') === FALSE) {
            
    $keywords[1] = $search;
          }else{
            
    $keywords explode(' ',$search);
        }
        
    $searchterm '';
        foreach (
    $keywords as $keyword){
            
    $searchterm .= " OR `KEYWORDS` LIKE '%$keyword%' ";
        }
          
    $query=mysql_query("SELECT * FROM Drills WHERE KEYWORDS LIKE '%$keywords[1]%' $searchterm");
    }
    echo 
    "<p class='title'>Practice Drill Library Results<br><b class='body'>(Click on the Drill Image for a Larger View)</b><br>";

    WHILE(
    $rows mysql_fetch_array($query)):

    $TITLE $rows['TITLE']; 
    $DESCRIPTION $rows['DESCRIPTION'];
    $NOTES $rows['NOTES'];
    $IMAGE $rows['IMAGE']; 

    echo 
    "<table border=1>";
    echo 
    "<tr class=body valign=top>";
    echo 
    "<td width=15%>" .  $TITLE "</td>";
    echo 
    "<td width=45%>" .  $DESCRIPTION "</td>";
    echo 
    "<td width=20%>" .  $NOTES "</td>";
    echo 
    "<td width=20%>" .  "<a href='$IMAGE'><img src='$IMAGE' width='250' height='284' target='_blank'></a>" "</td>";
    echo 
    "</tr>";
    echo 
    "</table>";

    endwhile; 
    Last edited by Inigoesdr; 08-23-2013 at 04:20 AM.

  • #9
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    PHP Code:
        $search mysql_real_escape_string(trim($_POST['searchterms']));
        if(
    stristr($search' ') === FALSE) {
            
    $keywords[1] = $search;
          }else{
            
    $keywords explode(' ',$search);
        }
        
    $searchterm '';
        foreach (
    $keywords as $keyword){
            
    $searchterm .= " OR `KEYWORDS` LIKE '%$keyword%' ";
        }
          
    $query=mysql_query("SELECT * FROM Drills WHERE KEYWORDS LIKE '%$keywords[1]%' $searchterm");

    echo 
    "<p class='title'>Practice Drill Library Results<br><b class='body'>(Click on the Drill Image for a Larger View)</b><br>";

    while (
    $row mysql_fetch_array($query)) {

    $TITLE $rows['TITLE']; 
    $DESCRIPTION $rows['DESCRIPTION'];
    $NOTES $rows['NOTES'];
    $IMAGE $rows['IMAGE']; 

    echo 
    "<table border=1>
    <tr class=body valign=top>
    <td width=15%>$TITLE</td>
    <td width=45%>$DESCRIPTION</td>
    <td width=20%>$NOTES</td>
    <td width=20%><a href='$IMAGE'><img src='$IMAGE' width='250' height='284' target='_blank'></a></td>
    </tr>
    </table>"
    ;


    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #10
    New Coder
    Join Date
    Aug 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    think it worked

    I think your corrections worked. I am getting results that seem to occur with more than one keyword. I am not putting commas between keywords right now, just spaces. it did not work if I happened (as a user) to put in commas in the input field. I will have to rest the results more to make sure I am pulling the correct rows given multiple keywords.

    Thank you.

    Now I need to work on the concat and multiple ways of searching.

    1. someone may just use a category dropdown and pull all the drills in a certain category. Not all the categories are similar to keywords.

    2. the filtering type functionality I mentioned in which I might choose a category or keyword + number of players required for a drill.

  • #11
    New Coder
    Join Date
    Aug 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    to be clear

    to be clear, when I was referring to the 3 different search items in the concat section, each item would have its own distinct values. from your code sample, it looked like you may have thought I was searching each column for the keyword(s). What I need to do is be able to filter. for example, if I choose all ball-handling drills in which I needed at least 6 players, I would get a different result than if I said I needed unlimited or 2 or 4 or of course a different category (passing). Of if I did it by keyword + number of players.

    Some one may just do a category search, just a keyword entry or do one of those + number of players.

  • #12
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    if they need to contain ALL the KEYWORD's change the OR to AND in the search line
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #13
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    to change to search with commas and spaces try instead of:

    PHP Code:
      if(stristr($search' ') === FALSE) {
            
    $keywords[1] = $search;
          }else{
            
    $keywords explode(' ',$search);
        } 
    PHP Code:
      if(stristr($search',') === FALSE) {
              if(
    stristr($search' ') === FALSE) {
            
    $keywords[1] = $search;
          }else{
            
    $keywords explode(' ',$search);
        }
          }else{
            
    $keywords explode(',',$search);
        } 
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #14
    New Coder
    Join Date
    Aug 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thank you

    I made this tweak with the comma and spaces and good to go. now to try and put the final effort into the different filter.

  • #15
    New Coder
    Join Date
    Aug 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    combining search filters and queries

    I have the multiple keyword search with this html and php code:

    html page
    <form action="search.php" method="POST">
    <input type="text" name="searchterms" placeholder="Search..." size="50"><br><br>
    <input type="submit" value="Search">
    </form>

    php code:
    $search = mysql_real_escape_string(trim($_POST['searchterms']));
    if(stristr($search, ',') === FALSE) {
    if(stristr($search, ' ') === FALSE) {
    $keywords[1] = $search;
    }else{
    $keywords = explode(' ',$search);
    }
    }else{
    $keywords = explode(',',$search);
    }
    $searchterm = '';
    foreach ($keywords as $keyword){
    $searchterm .= " OR `KEYWORDS` LIKE '%$keyword%' ";
    }

    $query=mysql_query("SELECT * FROM Drills WHERE KEYWORDS LIKE '%$keywords[1]%' $searchterm");

    echo "<p class='title'>Practice Drill Library Results<br><b class='body'>(Click on the Drill Image for a Larger View)</b><br>";

    while ($rows = mysql_fetch_array($query)) {

    $TITLE = $rows['TITLE'];
    $DESCRIPTION = $rows['DESCRIPTION'];
    $NOTES = $rows['NOTES'];
    $IMAGE = $rows['IMAGE'];

    echo "<table border=1>
    <tr class=body valign=top>
    <td width=15%>$TITLE</td>
    <td width=45%>$DESCRIPTION</td>
    <td width=20%>$NOTES</td>
    <td width=20%><a href='$IMAGE'><img src='$IMAGE' width='250' height='284'></a></td>
    </tr>
    </table>";
    }


    I also have the potential to have a dropdown where I can select categories. I created a second page just to work on this separately, hence the search2.php vs. my original search.php page from above

    <form action="search2.php" method="POST">
    <select name="categorysearch" id="categorysearch">
    <option value="BALL HANDLING">BALL HANDLING</option>
    <option value="PASSING">PASSING</option>
    <option value="FOOTWORK">FOOTWORK</option>
    <option value="TRANSITION">TRANSITION</option>
    <input type="submit" value="Search">
    </select>
    </form>

    with php code:
    $search = mysql_real_escape_string(trim($_POST['categorysearch']));
    $query=mysql_query("SELECT * FROM Drills WHERE CATEGORY LIKE '$search'");


    echo "<p class='title'>Practice Drill Library Results<br><b class='body'>(Click on the Drill Image for a Larger View)</b><br>";

    WHILE($rows = mysql_fetch_array($query)):

    $TITLE = $rows['TITLE'];
    $DESCRIPTION = $rows['DESCRIPTION'];
    $NOTES = $rows['NOTES'];
    $IMAGE = $rows['IMAGE'];

    echo "<table border=1>";
    echo "<tr class=body valign=top>";
    echo "<td width=15%>" . $TITLE . "</td>";
    echo "<td width=45%>" . $DESCRIPTION . "</td>";
    echo "<td width=20%>" . $NOTES . "</td>";
    echo "<td width=20%>" . "<a href='$IMAGE'><img src='$IMAGE' width='250' height='284' target='_blank'></a>" . "</td>";
    echo "</tr>";
    echo "</table>";

    endwhile;


    How can I do a couple of things:
    (1) combine them into one form and query where a user may either search by keyword OR use the dropdown?

    or

    (2) If I have a second dropdown, for example, that has the number of people required ... then I would have the ability of a user to have a keyword text search but also combine that with the possibility of filtering by category and\or number of people.

    My lack of knowledge is integrating the code and of course the multiple filter code.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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