Hey, I have question for this script, which should do live ajax search from sql database. I’m not getting this to work for me. can you guys give a hand. I’m trying to use this script to make an online dictionary.
image of my my Sql database table.
files: index.html,
[php]
search.php,
[php]<?php
// file for database connection
include(‘inc/db.inc.php’);
// configuration file
include(‘inc/config.inc.php’);
if(isset($_GET[‘p’])) {
$page_number = $_GET[‘p’];
$arraySearch = $_GET[‘terms’];
$show_count = $_GET[‘count’];
settype($page_number, ‘integer’);
}
$nospaces = substr($_GET[‘terms’],0,4);
$offset = ($page_number - 1) * $records_number;
// check for an empty string and display a message.
if ($_GET[‘terms’] == “”) {
echo ‘
// minim 3 characters condition
} else if(strlen($_GET[‘terms’]) < $limitchar) {
echo ‘
// no spaces in first 4 letters
} else if(preg_replace(’/[a-zA-Z0-9]/’, ‘’, $nospaces)) {
echo ‘
} else {
// explode search words into an array
$arraySearch = explode(" “, $_GET[‘terms’]);
// table fields to search
$arrayFields = array(0 => $first_field, 1 => $second_field);
$countSearch = count($arraySearch);
$a = 0;
$b = 0;
$query = “SELECT * FROM $table_name WHERE (”;
$countFields = count($arrayFields);
while ($a < $countFields)
{
while ($b < $countSearch)
{
$query = $query.”$arrayFields[$a] LIKE ‘%$arraySearch[$b]%’";
$b++;
if ($b < $countSearch)
{
$query = $query." AND “;
}
}
$b = 0;
$a++;
if ($a < $countFields)
{
$query = $query.”) OR (";
}
}
$query = $query.") LIMIT $offset, $records_number;";
$search = mysql_query($query);
// get number of search results
$arrayFields = array(0 => $first_field, 1 => $second_field);
$countSearch = count($arraySearch);
$a = 0;
$b = 0;
$query = “SELECT * FROM $table_name WHERE (”;
$countFields = count($arrayFields);
while ($a < $countFields)
{
while ($b < $countSearch)
{
$query = $query."$arrayFields[$a] LIKE ‘%$arraySearch[$b]%’";
$b++;
if ($b < $countSearch)
{
$query = $query." AND “;
}
}
$b = 0;
$a++;
if ($a < $countFields)
{
$query = $query.”) OR (";
}
}
$query = $query.")";
$count_results = mysql_query($query);
$numrows = mysql_num_rows($count_results);
// no results
if($numrows == 0) {
echo ‘
// show results
} else {
echo ’
'. $_GET['terms'] .' - '. $numrows .' results found
while($row = mysql_fetch_assoc($search)) {
$urltitle = str_replace(" “,”_", $row[‘title’]);
echo ’
played
$nav = ‘’;
for($page = 1; $page <= $maxPage; $page++) {
if ($page == $page_number) {
$nav .= “$page”;
}
else
{
$nav .= “<a href=“javascript:htmlData(‘search.php’,'terms=”.$_GET[‘terms’].”&p=$page’)">$page";
}
}
if ($page_number > 1) {
$page = $page_number - 1;
$prev = “<a href=“javascript:htmlData(‘search.php’,'terms=”.$_GET[‘terms’].”&p=$page’)">«";
$first = “<a href=“javascript:htmlData(‘search.php’,'terms=”.$_GET[‘terms’].”&p=1’)">First";
}
else {
$prev = ‘’;
$first = ‘’;
}
if ($page_number < $maxPage) {
$page = $page_number + 1;
$next = “<a href=“javascript:htmlData(‘search.php’,'terms=”.$_GET[‘terms’].”&p=$page’)">»";
$last = “<a href=“javascript:htmlData(‘search.php’,'terms=”.$_GET[‘terms’].”&p=$maxPage’)">Last";
}
else {
$next = ‘’;
$last = ‘’;
}
echo $data;
echo "<div id=“results_bottom”>
$first $prev $nav $next $last
inc/config.inc.php
[php]
<?php $table_name = "tbl_customer"; //which table from database $first_field = "CustomerName"; //which field from table $second_field = "Address"; //which field from table $limitchar = 4; //minimum of characters $records_number = 10; // Number of records to show per page (different from 0) $page_number = 1;// default start page ?>[/php]
and inc/db.inc.php
[php]
[/php]