Jump to content

Search function count() only ever register as 1


tradet

Recommended Posts

I tried to create a search function for my page but I'm having trouble organizing them with the most relevant first.

 

I followed this guide but can't make it work.

 

<?php
session_start();
virtual('/Connections/bumhome.php');

if(isset($_GET['search']) && strlen($_GET['search']) > 0 ) {
$string = $_GET['search'];

virtual('/scripts/porterstemmer.php');
virtual('/scripts/cleaner.php');

$stemmer = new PorterStemmer;
$stemmed_string = $stemmer->stem(strtolower($string));

$clean_string = new cleaner();
$stemmed_string = $clean_string->parseString($stemmed_string);

$split = split(" ",$stemmed_string);
foreach ($split as $array => $value) {
	$new_string .= ''.$value.' ';
}
$new_string = substr( $new_string, 0, (strLen($new_string)-1) );

$split_stemmed = split(" ",$new_string);


$about_sql = "SELECT DISTINCT COUNT(*) As occurrences, id, pass, title, subtitle, date_entered, entry FROM about WHERE (";

$blog_sql = "SELECT DISTINCT COUNT(*) As occurrences, id, title, date_entered, entry FROM blog WHERE (";
             
while( list($key,$val) = each($split_stemmed) ){
        	if( $val <> " " and strlen($val) > 0 ){
		$val = '%'.$val.'%';
		$val = mysql_real_escape_string(trim($val));

		$about_sql .= "(pass LIKE '$val' OR title LIKE '$val' OR subtitle LIKE '$val' OR date_entered LIKE '$val' OR entry LIKE '$val') OR";

    			$blog_sql .= "(title LIKE '$val' OR date_entered LIKE '$val' OR entry LIKE '$val') OR";
    		}
}
$about_sql = substr( $about_sql, 0, (strlen($about_sql)-3) );
$about_sql .= ") GROUP BY id ORDER BY occurrences DESC";

$blog_sql = substr( $blog_sql, 0, (strlen($blog_sql)-3) );
$blog_sql .= ") GROUP BY id ORDER BY occurrences DESC";

mysql_select_db($database_bumhome, $bumhome);
$q_about= mysql_query($about_sql, $bumhome) or die(mysql_error());
$about_total = mysql_num_rows($q_about);

$q_blog = mysql_query($blog_sql, $bumhome) or die(mysql_error());
$blog_total = mysql_num_rows($q_blog);

$total = about_total + $blog_total;
}

?>

 

It searches and all but COUNT() - occurrences, or $row_q_blog['occurrences']/$row_q_about['occurrences'] never passes 1 making me unable to sort it.

 

Here are the actual queries when I search "open source":

$about_sql = SELECT COUNT(*) As occurrences, id, pass, title, subtitle, date_entered, entry FROM about WHERE ((pass LIKE '%open%' OR title LIKE '%open%' OR subtitle LIKE '%open%' OR date_entered LIKE '%open%' OR entry LIKE '%open%') OR(pass LIKE '%source%' OR title LIKE '%source%' OR subtitle LIKE '%source%' OR date_entered LIKE '%source%' OR entry LIKE '%source%')) GROUP BY id ORDER BY occurrences DESC

 

and

 

$about_sql = SELECT COUNT(*) As occurrences, id, title, date_entered, entry FROM blog WHERE ((title LIKE '%open%' OR date_entered LIKE '%open%' OR entry LIKE '%open%') OR(title LIKE '%source%' OR date_entered LIKE '%source%' OR entry LIKE '%source%')) GROUP BY id ORDER BY occurrences DESC

 

Even if I have several "open" and "source" in my title and entry it doesn't find more than one.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.