All pastes #454917 Raw Edit

Something

public text v1 · immutable
·published 2007-04-23 21:54 UTC
rendered paste body
<?php

header("Content-type: text/html; charset=utf-8");

?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
     "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Search [Newbold]</title>
<link href="/style.css" rel="stylesheet" type="text/css" media="all">
<link rel="alternate" type="application/atom+xml" title="Feed" href="http://newbold.name/feed.xml">
<link rel="home" title="Home" href="http://newbold.name/">
<script src="/mint/?js" type="text/javascript"></script>
</head>
<body>

<h1>Search</h1>

<form action="/search/" method="get">
<p><label for="search">Search</label> <input id="search" type="text" name="q"><input type="submit" value="Go"></p>
</form>

<?php

if(isset($_REQUEST['q']) && ($_REQUEST['q'] !== '')) {
	
	$q = strip_tags($_REQUEST['q']);
	
	$connection = mysql_connect('content.newbold.name', 'content', '') or die('Could not connect: ' . mysql_error());
	mysql_select_db('content') or die('Could not select database');
	
	$query = 'SELECT * FROM content WHERE page like "%'.$_REQUEST['q'].'%" OR title like "%'.$_REQUEST['q'].'%" OR content like "%'.$_REQUEST['q'].'%"';
	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
	
	$count = mysql_num_rows($result);
	if($count == 0) $rs = ' did not match any items.';
	if($count == 1) $rs = ' matched one item.';
	if($count >  1)$rs = ' matched '.$count.' items.';
	
	if($count >= 1) {
		echo '<p>Your search for <strong>'.$q.'</strong>'.$rs.'</p>';
		while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
			
			// reset
			$matches = array();
			
			$match1 = explode(" ", $line['content']);
			$match2 = explode(" ", strtolower($line['content']));
			
			$ql = strtolower($q);
			
			foreach($match2 as $key => $value) {
				if(strstr($value, $ql) !== false) {
					$matches[] = $key;
				}
			}
			
			$start = $matches[0] - 5;
			$end = $matches[0] + 5;
			
			$context = array_slice($match1, $start, $end);
			$context2 = implode(" ", $context);
			$context2 = str_ireplace($q, '<strong>'.$q.'</strong>', $context2);
			
			// $context = array_search(strtolower($q), $match2);
			
			if($context2 !== '') {
				$context3 = '...'.$context2.'...<br>';
			}
			else {
				$context3 = null;
			}
			
			echo '<p><a href="http://newbold.name/'.$line['page'].'">'.$line['title'].'</a><br>';
			echo $context3;
			echo '<span style="color: #666;">newbold.name/'.$line['page'].'/</span></p>';
		}
	
	}
	else {
		echo '<p>Your search for <strong>'.$q.'</strong>'.$rs.'</p>';
	}
	
	if(isset($connection)) mysql_close($connection);
	
}
else {
	echo '<p>You can search the content on this site using the form above.</p>';
}

?>

<p><a href="/" accesskey="1" title="Go home">Home</a></p>

</body>
</html>