Page 1 of 2
Ajax autosuggest/autocomplete from database - problem
Posted: Sun Aug 22, 2010 4:22 pm
by defroster
Hello,
I am trying to implement some code I found and downloaded here:
http://www.roscripts.com/Ajax_autosugge ... e-154.html
When I try and run this I get no search results back when searching? I have entered data in the database and all connections seem good. Several people in the comments say they have problems, but no solution.
Any one that can find the fault here?
- THANKS
Code: Select all
<?php
require_once('db.php');
include('classes/stem.php');
include('classes/cleaner.php');
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 ( split ( " ",$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 = split ( " ",$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;
?>
The code can be found in attached zip file.
Re: Ajax autosuggest/autocomplete from database - problem
Posted: Mon Aug 23, 2010 1:22 am
by Gargoyle
put
Code: Select all
$total = mysql_num_rows ( $query );
print_r($total);
exit;
and see what it says.
Re: Ajax autosuggest/autocomplete from database - problem
Posted: Mon Aug 23, 2010 1:28 am
by defroster
I just added it to the end like this
Code: Select all
<?php
require_once('db.php');
include('classes/stem.php');
include('classes/cleaner.php');
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 ( split ( " ",$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 = split ( " ",$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;
$total = mysql_num_rows ( $query );
print_r($total);
exit;
?>
And what I get is:
Code: Select all
Warning: mysql_num_rows() expects parameter 1 to be resource, null given in /Applications/XAMPP/xamppfiles/htdocs/search/response.php on line 68
Where line 68 is the added '$total = mysql_num_rows ( $query );'
Re: Ajax autosuggest/autocomplete from database - problem
Posted: Mon Aug 23, 2010 1:35 am
by Gargoyle
put it before where it says
Re: Ajax autosuggest/autocomplete from database - problem
Posted: Mon Aug 23, 2010 1:47 am
by defroster
Ok, thanks. I put this now. But the return is nothing. Just completely blank page. If I hit view source there is absolutely nothing
Code: Select all
<?php
require_once('db.php');
include('classes/stem.php');
include('classes/cleaner.php');
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 ( split ( " ",$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 = split ( " ",$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($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;
?>
Re: Ajax autosuggest/autocomplete from database - problem
Posted: Mon Aug 23, 2010 1:50 am
by Gargoyle
then there's most likely a problem with yoru sql query.
try:
if it just says "bla", the sql query is not returning data, so nothing is sent back to the user because of if($total>0)
Re: Ajax autosuggest/autocomplete from database - problem
Posted: Mon Aug 23, 2010 1:59 am
by defroster
Thanks. Now I did this:
Code: Select all
<?php
require_once('db.php');
include('classes/stem.php');
include('classes/cleaner.php');
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 ( split ( " ",$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 = split ( " ",$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;
?>
But then I get this error:
Code: Select all
Parse error: syntax error, unexpected T_EXIT in /Applications/XAMPP/xamppfiles/htdocs/search/response.php on line 50
Re: Ajax autosuggest/autocomplete from database - problem
Posted: Mon Aug 23, 2010 2:12 am
by Gargoyle
you're missing the semicolon.
Re: Ajax autosuggest/autocomplete from database - problem
Posted: Mon Aug 23, 2010 2:40 am
by defroster
Thanks, Silly me forgot.
The bad news there is still no output, not even "bla"

Re: Ajax autosuggest/autocomplete from database - problem
Posted: Mon Aug 23, 2010 2:42 am
by Gargoyle
put it before
Code: Select all
if( !empty ( $_POST['search'] ) ):
if that works, put it before
Code: Select all
if ( strlen ( $new_string ) > 3 ):
Re: Ajax autosuggest/autocomplete from database - problem
Posted: Mon Aug 23, 2010 2:52 am
by defroster
I really appreciate your help! Thanks
When I put the code above the:
Code: Select all
if( !empty ( $_POST['search'] ) ):
I got a "bla"
But when I do this:
Code: Select all
<?php
require_once('db.php');
include('classes/stem.php');
include('classes/cleaner.php');
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 ( split ( " ",$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 = split ( " ",$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;
?>
I get nothing again.. ;(
Re: Ajax autosuggest/autocomplete from database - problem
Posted: Mon Aug 23, 2010 3:02 am
by Gargoyle
$_POST['search'] is empty so your script will do nothing.
put
Code: Select all
$_POST['search'] = 'some valid keyword';
and see if it works
Re: Ajax autosuggest/autocomplete from database - problem
Posted: Mon Aug 23, 2010 3:09 am
by defroster
Thanks Gargoyle,
Now I do this:
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 ( split ( " ",$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 = split ( " ",$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;
?>
But I get a new error :
Code: Select all
Deprecated: Function split() is deprecated in /Applications/XAMPP/xamppfiles/htdocs/search/response.php on line 21
bla:
Any ideas what I should use instead of the split?
Re: Ajax autosuggest/autocomplete from database - problem
Posted: Mon Aug 23, 2010 3:35 am
by timWebUK
Re: Ajax autosuggest/autocomplete from database - problem
Posted: Mon Aug 23, 2010 3:49 am
by defroster
Thanks, but that created some problems:
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 ( preg_split ( " ",$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 = preg_split ( " ",$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;
?>
generates:
Code: Select all
Warning: preg_split() [function.preg-split]: Empty regular expression in /Applications/XAMPP/xamppfiles/htdocs/search/response.php on line 21
Warning: array_unique() expects parameter 1 to be array, boolean given in /Applications/XAMPP/xamppfiles/htdocs/search/response.php on line 21
Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/search/response.php on line 21
bla: