2

Hi Guys I have this search from that takes a search term matches it with a table.field and in php it searches all matching data. I want to add autocomplete to it, can anyone PLEASE assist?

Here is the HTML FORM

<form action="'.$_SERVER['REQUEST_URI'].'" method="post">   
    <input type="text" id="searchThis" name="searchThis"  placeholder="search" value="" size="14" />
        <select name="searchItems" id="searchItems"> 
        <optgroup value="Vehicles" label="Vehicles">Vehicles 
            <option value="vehicles.Make">Make</option>
            <option value="vehicles.model">Model</option>
            <option value="vehicles.RegNumber">Registration Number</option>
            <option value="vehicles.licenseExpireDate">License Expire Date</option>
        </optgroup>
        <optgroup value="Owners" label="Owners">Clients
            <option value="owners.OwnerName" label="" >Name</option>
            <option value="owners.mobile">Mobile Number</option>
        </optgroup>
        </select> 

    <input type="submit" id="doSearch" name="Search" value="Search" />
    </form>
    <ul id="result">
    </ul>

There is the JS

<script src="js/jquery-1.8.0.min.js" type="text/javascript"></script>
<script type="text/javascript">
 var $j = jQuery.noConflict(); 

(function($j){

 $j(document).ready(function (){

    $j("#searchThis").keyup(function() 
    { 
    var searchThis = $j('#searchThis').val();
    var searchItems = $j('#searchItems').val();
    var dataString = {'searchThis': searchThis,'searchItems':searchItems};
    if(searchThis!='')
    {
        $j.ajax({
        type: "POST",
        url: "doAutocomplete_search.php",
        data: dataString, 
        dataType: "html",
        cache: false,
        success: function(data)
        {
        $j("#result").html(data).show();
        }
        });
    }return false;    
    });

    $j("#result").live("click",function(e){ 
        var clicked = $j(e.target);
        var name = clicked.find('.name').html();
        var decoded = $j("<div/>").html(name).text();
        $j('#searchThis').val(decoded);
    });

    $j(document).live("click", function(e) { 
        var clicked = $j(e.target);
        if (! clicked.hasClass("search")){
        $j("#result").fadeOut(); 
        }
    });

    $j('#searchid').click(function(){
        $j("#result").fadeIn();
    });    

});

})($j);

And the PHP

function implement($cxn,$searchThis, $field) {
$show = "";

//Item to be searched
$srchThis = strip_tags(trim($searchThis));
//[0]= table , [1]=field    to search

$srchStack = explode('.',$field);
    $gtData = "SELECT * FROM ".$srchStack[0]." WHERE ".$srchStack[1]." like '%|{$srchThis}|%'";
         //or die(mysqli_error($cxn))
        if($selectc = mysqli_query($cxn,"SELECT * FROM {$srchStack[0]} WHERE {$srchStack[1]} LIKE '{$srchThis}%' OR {$srchStack[1]} LIKE '%{$srchThis}%'")) { 
            $srchData = array();
        $rows = mysqli_fetch_row($selectc);
        echo $rows;
            //if() {, MYSQL_ASSOC
                //echo var_dump($srchData);
                $show .= '
                <table style="border:2px solid #0000">';
                //foreach($srchData as $fields=>$data) {
                for($s=0; $s < $rows && $srchData = mysqli_fetch_assoc($selectc);$s++) {
                    if($srchStack[0] == 'vehicles' && $fields == 'RegNumber') {
                        $dataItem = $data;
                        $editTbl = 'Vehicles';
        $link = 'href="index.php?list=vehicles&&tbl='.$srchStack[0].'&&item='.$dataItem.'"';
                    } elseif($srchStack[0] == 'vehicles' && $fields == 'Make') {
                        $dataItem = $data;
                        $editTbl = 'vehicles';
        $link = 'href="index.php?list=vehicles&&tbl='.$srchStack[0].'&&item='.$dataItem.'"';
                    }

                    $show .= '<tr><td><a '.$link.'>'.$data.'</a></td></tr>
                    ';  
                }
                $show .= '</table>';

                return $show;
        } else {
            $show .= "There are no entries in the database...<br>".mysqli_error($cxn);

            return $show;
        } 
    //}

}

echo implement($cxn, $_POST['searchThis'], $_POST['searchItems']);


$cxn->close();
3
  • What i want to be able to do is get a return that i can select and them press the submit button to put out the full details... Commented Oct 23, 2014 at 9:23
  • There is way too much code here. Please remove any code that is irrelevant to the question. Commented Oct 23, 2014 at 9:41
  • Hi @SverriM.Olsen code has been edited... Commented Oct 23, 2014 at 9:45

1 Answer 1

1

Hi Guys so i had to do some refactoring, realized it was more the PHP MySQL code.

Here is the refactored PHP code

//Item to be searched
$srchThis = strip_tags(trim($searchThis));
//[0]= table , [1]=field    to search

$srchStack = explode('.',$field);
    $gtData = "SELECT * FROM ".$srchStack[0]." WHERE ".$srchStack[1]." like '%|{$srchThis}|%'";
         //or die(mysqli_error($cxn))
        if($selectc = mysqli_query($cxn,"SELECT * FROM {$srchStack[0]} WHERE {$srchStack[1]} LIKE '{$srchThis}%' OR {$srchStack[1]} LIKE '%{$srchThis}%'")) { 
            //$srchData = array();

            $rows = mysqli_num_rows(@$selectc) or die(mysqli_error($cxn).'<br>No Rows returned...');
            if($rows > 0) {//, MYSQL_ASSOC

                //$link = ''; $l_c = 0;
                $show .= '<table style="border:2px solid #0000">';
                for($c = NULL;$c != $rows && $srchData = mysqli_fetch_assoc($selectc); $c++) {                  

                foreach($srchData as $fields=>$data) {
                    if($fields == $srchStack[1]) {
                        $dataItem = $data;
                        $editTbl = $srchStack[0];
        $show .= '<tr><td><a href="index.php?list='.$srchStack[0].'&&tbl='.$srchStack[0].'&&'.$srchStack[1].'='.$dataItem.'">'.$dataItem.'</a></td></tr>';//$a_json_row($dataItem);

                //$show .= $link[$c];$link[$c]
                    }

                }
                }

                $show .= '</table>';
                return $show;
        } else {
            $show .= "There are no entries in the database...<br>".mysqli_error($cxn);

            return $show;
        } 

Of-course the JS code still needs some more work but this greatly improved the results... Hope this help someone...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.