All pastes #790514 Raw Edit

Something

public text v1 · immutable
·published 2007-11-20 13:16 UTC
rendered paste body
<?php
trim( $search_string );
$keywords = explode( ' ', $search_string );

if ( count($keywords) == 0 )
{
	return 'stripped';
}

switch ( $matching )
{
default:
case 'or':
	$matching = '||';
	break;
case 'all':
	$matching = '&&';
	break;
}

// Now for the fun stuff, building the MySQL query.
$stmt = '
SELECT 
	`df`.`id`, 
	`full_name`, 
	`filesize`, 
	`hits`, 
	`system`, 
	`path`, 
	ROUND(SUM(`dr`.`rating`) / COUNT(`dr`.`id`), 2) AS `rating` 
FROM 
	`downloads_files` AS `df` 
LEFT JOIN 
	`downloads_ratings` AS `dr` ON `dr`.`file_id` = `df`.`id`
WHERE';

if ( strtolower($system) != 'all' )
{
	$stmt .= '
		`system` = "' . $system . '" AND ';
}

if ( strtolower($region) != 'all' )
{
	$stmt .= ' 
	`df`.`language` = "(' . $region . ')" AND';
}

if ( strtolower($genre) != 'all' )
{
	$stmt .= ' 
	`df`.`genre` = "' . $genre . '" AND';
}

$stmt .= '(';
$i = 1;
foreach ( $keywords as $keyword )
{
	$stmt .= '
		`full_name` LIKE "%' . $keyword . '%"';

	// Must any/all keywords match?
	if ( $i != count($keywords) )
	{
		$stmt .= ' ' . $matching . ' ';
		$i++;
	}
}

$stmt .= ') 
GROUP BY 
	`df`.`id`
ORDER BY 
	`df`.`system`, `df`.`filename` ASC LIMIT 50';

$result = $this->sql->fetch_assoc_array( $stmt );
?>