Re: Ajax autosuggest/autocomplete from database - problem
Posted: Mon Aug 23, 2010 4:19 am
use explode() instead of split()
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
<?php
require_once('db.php');
include('classes/stem.php');
include('classes/cleaner.php');
$_POST['search'] = 'some valid keyword';
if( !empty ( $_POST['search'] ) ):
$string = $_POST['search'];
$main_url = 'http://www.roscripts.com/';
$stemmer = new Stemmer;
$stemmed_string = $stemmer->stem ( $string );
$clean_string = new jSearchString();
$stemmed_string = $clean_string->parseString ( $stemmed_string );
$new_string = '';
foreach ( array_unique ( explode ( " ",$stemmed_string ) ) as $array => $value )
{
if(strlen($value) >= 3)
{
$new_string .= ''.$value.' ';
}
}
$new_string = substr ( $new_string,0, ( strLen ( $new_string ) -1 ) );
print_r("bla: $total");
exit;
if ( strlen ( $new_string ) > 3 ):
$split_stemmed = explode ( " ",$new_string );
mysql_select_db($database);
$sql = "SELECT DISTINCT COUNT(*) as occurences, title, subtitle FROM articles WHERE (";
while ( list ( $key,$val ) = each ( $split_stemmed ) )
{
if( $val!='' && strlen ( $val ) > 0 )
{
$sql .= "((title LIKE '%".$val."%' OR subtitle LIKE '%".$val."%' OR content LIKE '%".$val."%')) OR";
}
}
$sql=substr ( $sql,0, ( strLen ( $sql )-3 ) );//this will eat the last OR
$sql .= ") GROUP BY title ORDER BY occurences DESC LIMIT 10";
$query = mysql_query($sql) or die ( mysql_error () );
$row_sql = mysql_fetch_assoc ( $query );
$total = mysql_num_rows ( $query );
if($total>0):
echo ' <div class="entry">'."\n";
echo ' <ul>'."\n";
while ( $row = mysql_fetch_assoc ( $query ) )
{
echo ' <li>'."\n";
echo ' <a href="'.$main_url.'articles/show/'.$row['id'].'">'.$row['title'].''."\n";
echo ' <em>'.$row['subtitle'].'</em>'."\n";
echo ' <span>Added on 2007-06-03 by roScripts</span></a>'."\n";
echo ' </li>'."\n";
}
echo ' </ul>'."\n";
echo ' </div>'."\n";
endif;
endif;
endif;
?>
Code: Select all
if($total>0):Code: Select all
bla: 0Code: Select all
<?php
require_once('db.php');
include('classes/stem.php');
include('classes/cleaner.php');
$_POST['search'] = 'some valid keyword';
if( !empty ( $_POST['search'] ) ):
$string = $_POST['search'];
$main_url = 'http://www.roscripts.com/';
$stemmer = new Stemmer;
$stemmed_string = $stemmer->stem ( $string );
$clean_string = new jSearchString();
$stemmed_string = $clean_string->parseString ( $stemmed_string );
$new_string = '';
foreach ( array_unique ( explode ( " ",$stemmed_string ) ) as $array => $value )
{
if(strlen($value) >= 3)
{
$new_string .= ''.$value.' ';
}
}
$new_string = substr ( $new_string,0, ( strLen ( $new_string ) -1 ) );
if ( strlen ( $new_string ) > 3 ):
$split_stemmed = explode ( " ",$new_string );
mysql_select_db($database);
$sql = "SELECT DISTINCT COUNT(*) as occurences, title, subtitle FROM articles WHERE (";
while ( list ( $key,$val ) = each ( $split_stemmed ) )
{
if( $val!='' && strlen ( $val ) > 0 )
{
$sql .= "((title LIKE '%".$val."%' OR subtitle LIKE '%".$val."%' OR content LIKE '%".$val."%')) OR";
}
}
$sql=substr ( $sql,0, ( strLen ( $sql )-3 ) );//this will eat the last OR
$sql .= ") GROUP BY title ORDER BY occurences DESC LIMIT 10";
$query = mysql_query($sql) or die ( mysql_error () );
$row_sql = mysql_fetch_assoc ( $query );
$total = mysql_num_rows ( $query );
print_r("bla: $total");
exit;
if($total>0):
echo ' <div class="entry">'."\n";
echo ' <ul>'."\n";
while ( $row = mysql_fetch_assoc ( $query ) )
{
echo ' <li>'."\n";
echo ' <a href="'.$main_url.'articles/show/'.$row['id'].'">'.$row['title'].''."\n";
echo ' <em>'.$row['subtitle'].'</em>'."\n";
echo ' <span>Added on 2007-06-03 by roScripts</span></a>'."\n";
echo ' </li>'."\n";
}
echo ' </ul>'."\n";
echo ' </div>'."\n";
endif;
endif;
endif;
?>
Code: Select all
$_POST['search'] = 'some valid keyword';Code: Select all
$_POST['search'] = 'yolanda';Code: Select all
<?php
require_once('db.php');
include('classes/stem.php');
include('classes/cleaner.php');
$_POST['search'] = 'yolanda ';
if( !empty ( $_POST['search'] ) ):
$string = $_POST['search'];
$main_url = 'http://www.roscripts.com/';
$stemmer = new Stemmer;
$stemmed_string = $stemmer->stem ( $string );
$clean_string = new jSearchString();
$stemmed_string = $clean_string->parseString ( $stemmed_string );
$new_string = '';
foreach ( array_unique ( explode ( " ",$stemmed_string ) ) as $array => $value )
{
if(strlen($value) >= 3)
{
$new_string .= ''.$value.' ';
}
}
$new_string = substr ( $new_string,0, ( strLen ( $new_string ) -1 ) );
if ( strlen ( $new_string ) > 3 ):
$split_stemmed = explode ( " ",$new_string );
mysql_select_db($database);
$sql = "SELECT DISTINCT COUNT(*) as occurences, title, subtitle FROM articles WHERE (";
while ( list ( $key,$val ) = each ( $split_stemmed ) )
{
if( $val!='' && strlen ( $val ) > 0 )
{
$sql .= "((title LIKE '%".$val."%' OR subtitle LIKE '%".$val."%' OR content LIKE '%".$val."%')) OR";
}
}
$sql=substr ( $sql,0, ( strLen ( $sql )-3 ) );//this will eat the last OR
$sql .= ") GROUP BY title ORDER BY occurences DESC LIMIT 10";
$query = mysql_query($sql) or die ( mysql_error () );
$row_sql = mysql_fetch_assoc ( $query );
$total = mysql_num_rows ( $query );
print_r("bla: $total");
if($total>0):
echo ' <div class="entry">'."\n";
echo ' <ul>'."\n";
while ( $row = mysql_fetch_assoc ( $query ) )
{
echo ' <li>'."\n";
echo ' <a href="'.$main_url.'articles/show/'.$row['id'].'">'.$row['title'].''."\n";
echo ' <em>'.$row['subtitle'].'</em>'."\n";
echo ' <span>Added on 2007-06-03 by roScripts</span></a>'."\n";
echo ' </li>'."\n";
}
echo ' </ul>'."\n";
echo ' </div>'."\n";
endif;
endif;
endif;
?>
Code: Select all
$query = mysql_query($sql) or die ( mysql_error () );
$row_sql = mysql_fetch_assoc ( $query );
$total = mysql_num_rows ( $query );
Code: Select all
$query = mysql_query($sql) or die ( mysql_error () );
$total = mysql_num_rows ( $query );
Code: Select all
<html>
<head>
<title>roScripts - Ajax auto-suggest</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="lib/prototype.js" type="text/javascript"></script>
<script src="src/scriptaculous.js" type="text/javascript"></script>
<script src="src/unittest.js" type="text/javascript"></script>
<link rel="stylesheet" href="css/style.css" type="text/css" />
</head>
<body>
<div id="container">
<form method="get" action="">
<label for="testinput">Search</label><br />
<input type="text" id="search" name="search" autocomplete="off" class="input" value="" /><br />
<div id="update" style="display:none;position:relative;"></div>
<input type="image" name="register" class="submit-btn" src="http://www.roscripts.com/images/btn.gif" alt="submit" title="submit" />
</form>
<script type="text/javascript" language="javascript" charset="utf-8">
new Ajax.Autocompleter('search','update','response.php', { tokens: ','} );
</script>
</div>
</body>
</html>
Code: Select all
<?php
require_once('db.php');
include('classes/stem.php');
include('classes/cleaner.php');
#$_POST['search'] = 'yolanda';
if( !empty ( $_POST['search'] ) ):
$string = $_POST['search'];
$main_url = 'http://www.roscripts.com/';
$stemmer = new Stemmer;
$stemmed_string = $stemmer->stem ( $string );
$clean_string = new jSearchString();
$stemmed_string = $clean_string->parseString ( $stemmed_string );
$new_string = '';
foreach ( array_unique ( explode ( " ",$stemmed_string ) ) as $array => $value )
{
if(strlen($value) >= 3)
{
$new_string .= ''.$value.' ';
}
}
$new_string = substr ( $new_string,0, ( strLen ( $new_string ) -1 ) );
if ( strlen ( $new_string ) > 3 ):
$split_stemmed = explode ( " ",$new_string );
mysql_select_db($database);
$sql = "SELECT DISTINCT COUNT(*) as occurences, title, subtitle FROM articles WHERE (";
while ( list ( $key,$val ) = each ( $split_stemmed ) )
{
if( $val!='' && strlen ( $val ) > 0 )
{
$sql .= "((title LIKE '%".$val."%' OR subtitle LIKE '%".$val."%' OR content LIKE '%".$val."%')) OR";
}
}
$sql=substr ( $sql,0, ( strLen ( $sql )-3 ) );//this will eat the last OR
$sql .= ") GROUP BY title ORDER BY occurences DESC LIMIT 10";
#$query = mysql_query($sql) or die ( mysql_error () );
#$row_sql = mysql_fetch_assoc ( $query );
#$total = mysql_num_rows ( $query );
$query = mysql_query($sql) or die ( mysql_error () );
$total = mysql_num_rows ( $query );
print_r("bla: $total");
if($total>0):
echo ' <div class="entry">'."\n";
echo ' <ul>'."\n";
while ( $row = mysql_fetch_assoc ( $query ) )
{
echo ' <li>'."\n";
echo ' <a href="'.$main_url.'articles/show/'.$row['id'].'">'.$row['title'].''."\n";
echo ' <em>'.$row['subtitle'].'</em>'."\n";
echo ' <span>Added on 2007-06-03 by roScripts</span></a>'."\n";
echo ' </li>'."\n";
}
echo ' </ul>'."\n";
echo ' </div>'."\n";
endif;
endif;
endif;
?>