View Full Version : Search function using static pages

12-21-2011, 01:08 PM
I need to search a bunch of files (.php files) for whatever a user searches for. I can do this easily enough from a database but i cant find a way to do it on a file.

Can anyone help me figure out what i need to do?

12-21-2011, 01:17 PM
What are you searching for?
and can you show us one of the pages with an example of what you might search?

12-21-2011, 01:29 PM
If you look at this page as example

I need it so if someone searches for e.g. 'Zero Tolerance Approach' It would show a link to that page.

So far ive got this which seems to work but theres probably a better-easier way to do it


$Files = array('index.php' => 'index.php',
'cpcs.php' => 'cpcs.php',
'risk.php' => 'riskassessments.php',
'env.php' => 'enviromentalpolicy.php',
'hns.php' => 'healthandsafety.php');

$Links = array('index.php' => 'Homepage',
'cpcs.php' => 'CPCS',
'risk.php' => 'Risk Assessments',
'env.php' => 'Enviromental Policy',
'hns.php' => 'Health And Safety');

foreach ($Files as $f => $l)
$lines = implode('', file('view/'.$f));

//foreach ($lines as $line_num => $line)

echo "<a href='$l?hl=".$_GET['q']."'>".$Links[$f]."</a><br />";
//echo "not found.<br />";


12-21-2011, 05:09 PM
Without having your content in a database (like MySQL), I think you're on
the right track with what you did. A lot of looping, but that's the way it will be.
That's why MySQL is so nice.

12-21-2011, 07:21 PM
I've never done this before, but here's my stab at it. I didn't really understand why you had two arrays, and which ones were the actual files - so I've used the values in the files array as keys, and the title names as their values so that I can loop like shown - makes more sense to me.

$searchString = $_GET['q'];
$files = array('index.php' => 'Homepage',
'cpcs.php' => 'CPCS',
'riskassessments.php' => 'Risk Assessments',
'enviromentalpolicy.php' => 'Enviromental Policy',
'healthandsafety.php' => 'Health And Safety');

foreach ($files as $file => $title){
// Get contents of the file, and put in a string
$content = file_get_contents($file);
// Compare case-insensitively
$found = stristr($content,$searchString);
if($found !== FALSE){
// String was found
echo '<a href="'.$file.'">'.$title.'</a>';