PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
defroster
Forum Commoner
Posts: 49 Joined: Wed Mar 24, 2010 12:05 pm
Post
by defroster » Sun Aug 22, 2010 4:22 pm
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.
Gargoyle
Forum Contributor
Posts: 130 Joined: Wed Jul 14, 2010 12:25 am
Post
by Gargoyle » Mon Aug 23, 2010 1:22 am
put
Code: Select all
$total = mysql_num_rows ( $query );
print_r($total);
exit;
and see what it says.
defroster
Forum Commoner
Posts: 49 Joined: Wed Mar 24, 2010 12:05 pm
Post
by defroster » Mon Aug 23, 2010 1:28 am
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 );'
Gargoyle
Forum Contributor
Posts: 130 Joined: Wed Jul 14, 2010 12:25 am
Post
by Gargoyle » Mon Aug 23, 2010 1:35 am
put it before where it says
defroster
Forum Commoner
Posts: 49 Joined: Wed Mar 24, 2010 12:05 pm
Post
by defroster » Mon Aug 23, 2010 1:47 am
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;
?>
Gargoyle
Forum Contributor
Posts: 130 Joined: Wed Jul 14, 2010 12:25 am
Post
by Gargoyle » Mon Aug 23, 2010 1:50 am
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)
defroster
Forum Commoner
Posts: 49 Joined: Wed Mar 24, 2010 12:05 pm
Post
by defroster » Mon Aug 23, 2010 1:59 am
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
Gargoyle
Forum Contributor
Posts: 130 Joined: Wed Jul 14, 2010 12:25 am
Post
by Gargoyle » Mon Aug 23, 2010 2:12 am
you're missing the semicolon.
defroster
Forum Commoner
Posts: 49 Joined: Wed Mar 24, 2010 12:05 pm
Post
by defroster » Mon Aug 23, 2010 2:40 am
Thanks, Silly me forgot.
The bad news there is still no output, not even "bla"
Gargoyle
Forum Contributor
Posts: 130 Joined: Wed Jul 14, 2010 12:25 am
Post
by Gargoyle » Mon Aug 23, 2010 2:42 am
put it before
Code: Select all
if( !empty ( $_POST['search'] ) ):
if that works, put it before
Code: Select all
if ( strlen ( $new_string ) > 3 ):
defroster
Forum Commoner
Posts: 49 Joined: Wed Mar 24, 2010 12:05 pm
Post
by defroster » Mon Aug 23, 2010 2:52 am
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.. ;(
Gargoyle
Forum Contributor
Posts: 130 Joined: Wed Jul 14, 2010 12:25 am
Post
by Gargoyle » Mon Aug 23, 2010 3:02 am
$_POST['search'] is empty so your script will do nothing.
put
Code: Select all
$_POST['search'] = 'some valid keyword';
and see if it works
defroster
Forum Commoner
Posts: 49 Joined: Wed Mar 24, 2010 12:05 pm
Post
by defroster » Mon Aug 23, 2010 3:09 am
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?
timWebUK
Forum Contributor
Posts: 239 Joined: Thu Oct 29, 2009 6:48 am
Location: UK
Post
by timWebUK » Mon Aug 23, 2010 3:35 am
defroster
Forum Commoner
Posts: 49 Joined: Wed Mar 24, 2010 12:05 pm
Post
by defroster » Mon Aug 23, 2010 3:49 am
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: