SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    WebAmoeba mythix's Avatar
    Join Date
    Aug 2002
    Location
    here
    Posts
    578
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation arrays. remove null values?

    Hi,

    I'm developing a search engine for my site, but have come accross a problem. Currently a search string is supplied by the user, the string 'strg' is sent via the GET method. Once recieved the PHP uses the explode() function:

    $search = explode(" ", $HTTP_GET_VARS["strg"]);

    Then counts the words. From the new array and counted number of words it then creates the necessary SQL statement.

    However if the search string inlcuded a double space there will be values in the array equivalent to null, and as a result all of the entries from the database are given! Is there an easy way to remove these?

    Thanks
    Laws are like sausages. You have much more respect for them if you haven't actually seen how they're made.

    http://www.webamoeba.co.uk

  2. #2
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could try:

    PHP Code:
    $foo = array(' ');
    $final array_diff($search$foo); 
    or if that does not work:

    PHP Code:
    function mylength($var) {
       return 
    str_len($var) > 1;
    }

    $final array_filter($search'mylength'); 

  3. #3
    WebAmoeba mythix's Avatar
    Join Date
    Aug 2002
    Location
    here
    Posts
    578
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now I'm truly confused????

    I managed to find a faster way to do it:

    $search = array_filter( $search, "strlen" );

    At first I thought i had a problem because the code wasn't working, on closer inspection by using the array_values() function I was able to see that even once I had managed to remove the blank values that the search still wasn't working properly!!! and that it is still returning all of the records even after removing these values!

    here's a copy of the code:

    <form name="searchown" method="get" action="search.php">
    <input type="text" size=40 name="strg" class="smalltxt" maxlength="100">
    &nbsp;
    <input type="submit" align="absmiddle" height=22 class="smalltxt" value="Search" name="submit">
    <br>
    <span class="smalltxt">For best results do NOT include words such as and etc...</span>
    </form>

    <?php
    if ($HTTP_GET_VARS["strg"]) {

    echo "<p class=normtxt>You searched for: <span class=normtxtRed>" . $HTTP_GET_VARS["strg"] . "</span></p>";

    $db = mysql_connect("localhost", "root");
    @mysql_select_db("mythix",$db);

    $search = explode(" ", $HTTP_GET_VARS["strg"]);

    $count = 0;

    $search = array_filter( $search, "strlen" );

    print_r(array_values($search));

    $words = count($search);
    $sql = "SELECT * FROM tblArticles WHERE ";
    $sqlcount = "SELECT count(*) AS total FROM tblArticles WHERE ";
    $count = 0;

    while ($count < $words) {

    if ($count > 0) {
    $sql = $sql . " OR ";
    $sqlcount = $sqlcount . " OR ";
    }

    $sql = $sql . "description LIKE '%" . $search[$count] . "%' OR name LIKE '%" . $search[$count] . "%'";
    $sqlcount = $sqlcount . "description LIKE '%" . $search[$count] . "%' OR name LIKE '%" . $search[$count] . "%'";
    $count ++;

    }

    $result = @mysql_query($sql, $db);
    $resultcount = @mysql_query($sqlcount, $db);
    $total = @mysql_fetch_array($resultcount);

    if ($myrow2 = @mysql_fetch_array($result)) {

    do {

    printf("<p class=date><a href=general.php?loc=%s&id=%s>%s</a><br>\n", $myrow2["location"], $myrow2["id"], $myrow2["name"]);
    printf("%s<br>\n", $myrow2["date"]);
    printf("<span class=normtxt>%s</span></p>\n", $myrow2["description"]);

    } while ($myrow2 = mysql_fetch_array($result));


    } else {

    echo("<span class=normtxtRed>MYTHIX ERROR 2<br>sorry no records were found</span>");

    }

    printf("<p class=smalltxt>Your search returned %s", $total[total]);

    if ($total[total] == 1) {
    echo " result";
    } else {
    echo " results";
    }

    }
    ?>

    I haven't got round to compacting it. Does anyone know why it is still returning all of the values?

    I have published the code so you can look @ what its doing wrong: http:\\mythix.port5.com\search.php

    any help greatly appreciated! Note - i'm new to PHP so enlighten me before talking aout really complex stuff
    Laws are like sausages. You have much more respect for them if you haven't actually seen how they're made.

    http://www.webamoeba.co.uk

  4. #4
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this might be a better way to deal with multiple spaces -- use preg_split() instead of explode().

    PHP Code:
    $search preg_split('/ +/'$HTTP_GET_VARS['strg']); 
    then you don't need the array_filter() part.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  5. #5
    WebAmoeba mythix's Avatar
    Join Date
    Aug 2002
    Location
    here
    Posts
    578
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks

    You resolved my problem

    Laws are like sausages. You have much more respect for them if you haven't actually seen how they're made.

    http://www.webamoeba.co.uk


Bookmarks

Posting Permissions

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