Firstly, can I just say. I know this is really badly written but its to the best of my ability.

Right, on index.php it makes a query which should go to search.php. Search.php handles everything. It makes a request to ajax.php which makes the request from the database.

The problem Im having is refining the results to be 'LIKE' %search%. Could someone help me out please. All the source code is below :)

(note, this is all within vBulletin. So index.php will look strange, but its all fine ;))

index.php

$stylevar[htmldoctype]
<html dir="$stylevar[textdirection]" lang="$stylevar[languagecode]">
<head>
$headinclude
<title>Search Engine - $vboptions[bbtitle]</title>


<script type="text/javascript">

function getHTTPObject() {
	var http = false;
	//Use IE's ActiveX items to load the file.
	if(typeof ActiveXObject != 'undefined') {
		try {http = new ActiveXObject("Msxml2.XMLHTTP");}
		catch (e) {
			try {http = new ActiveXObject("Microsoft.XMLHTTP");}
			catch (E) {http = false;}
		}
	//If ActiveX is not available, use the XMLHttpRequest of Firefox/Mozilla etc. to load the document.
	} else if (XMLHttpRequest) {
		try {http = new XMLHttpRequest();}
		catch (e) {http = false;}
	}
	return http;
}
var http = getHTTPObject();

var url = "search-engine/searchnew.php";


function handler() {//Call a function when the state changes.
	if(http.readyState == 4 && http.status == 200) {
		document.getElementById('txt').innerHTML = http.responseText;
	}
}

function postMethod(a,b) {
	var params = "q=" + a + "&links=" + b;
	http.open("POST", url, true);
	
	//Send the proper header infomation along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");

	http.onreadystatechange = handler;
	http.send(params);
}


</script>

</head>
<body>

$header
$navbar

<center>
<form action="search-engine/search.php" method="POST" name="frm">
  <p> <input type="text" name="q" autocomplete="off">
  </p>
    <p>
   <input name="Submit" type="button"  onclick="postMethod(document.frm.q.value)" value="Submit" />
  </p>
</form>
<div id="txt"></div>


</center>

$footer
</body>
</html>

search.php

<?php
include('includes/config.php');
    error_reporting( E_ALL );
        
        $sql = 'SELECT COUNT(*) from list;';
        $res = mysql_query($sql);
        if ( is_resource( $res ) ) {
                $total = mysql_result( $res, 0 );
        
        }
      
?>
<html>
<head>
    <script type="text/javascript" src="http://jquery.com/src/jquery-latest.js"></script>
    <script type="text/javascript" src="pagination.js"></script>
    <script type="text/javascript">
    function pageselectCallback(page_id, jq){
        var first = (page_id*10)+1, second = (page_id*10)+5;
        $('#Searchresult').text("Showing search results " + first + '-' + second);
        $.ajax({
        type:'GET',
        url:'ajax.php',
        data:'offset=' + first + '&limit=5',
        success:function(msg) {
            $('#ajaxContent').html(msg);
        }
        });
    }
    $(document).ready(function(){
        $("#Pagination").pagination( <?php echo $total;?>, { 
        num_edge_entries: 2,
        num_display_entries: 8,
        callback: pageselectCallback 
        });

        pageselectCallback(0);
    });
    </script>
    
</head>
<body>
      <div id="Pagination">
     
     </div><br clear="all"/>
    <div id="Searchresult">
    </div>
    <div id="ajaxContent"></div>
</body>
</html>

ajax.php

<?php
    $offset = $_GET['offset'];
    $limit = $_GET['limit'];
    $search=$_POST["q"];
    include('includes/config.php');
        $result = mysql_query('SELECT * from list WHERE title LIKE '%$search%' LIMIT ' . $offset . ',' . $limit);
    if ( is_resource( $result ) ) {
    
        while ( $r = mysql_fetch_assoc( $result ) ) {
        
        $title=$r["title"];
   $url=$r["url"];
   $description=$r["description"];
   $lastcrawl=$r["lastcrawl"];
      
     
   echo '
   	<table class="tborder" cellpadding="$stylevar[cellpadding]" cellspacing="$stylevar[cellspacing]" border="0" width="600" align="center">
<tr>
	<td class="tcat"><a href="'.rawurldecode($url).'">'.$title.'</a></td>
</tr>
<tr>
	<td class="alt1">'.$description.'</td>
</tr>
<tr>
	<td class="alt1">Date Crawled: '.$lastcrawl.'</td>
</tr>
</table>';
       }
        
                
    }
    
?>

The following line doesn't appear to follow the correct syntax:

$result = mysql_query('SELECT * from list WHERE title LIKE '%$search%' LIMIT ' . $offset . ',' . $limit);

Try replacing it with the following:

$result = mysql_query("SELECT * from `list` WHERE `title` LIKE '%$search%' LIMIT '$offset','$limit'") or die(mysql_error());

I am unsure weather the above replacement will work but the proper error reporting at the end should give more info.

This article has been dead for over six months. Start a new discussion instead.