View Single Post
Old 11-25-2005, 06:57 AM   #1
ciaran@clissman
Green Mole
 
Join Date: May 2004
Posts: 10
Thumbs up Extracting search results and using them in your own web pages

Hi all,
after a fair amount of hacking about, I worked out how to get the results of a phpdig search out of phpdig and into php and html code of my own design. This is not elegant and the programming is untidy. But perhaps a more skilled practitioner can use this code as a starting point for better things.
The version it works with is 1.8.7

I actually use the code by including it in an iframe and passing the sole parameter, query_text as part of the src= url.

I hope someone finds this useful

Ciaran
Code:
<?php
/*
--------------------------------------------------------------------------------
PhpDig Version 1.6.x
This program is provided under the GNU/GPL license.
See the LICENSE file for more information.
All contributors are listed in the CREDITS file provided with this package.
PhpDig Website : http://www.phpdig.net/
--------------------------------------------------------------------------------

THIS SCRIPT BASED ON SEARCH.PHP, BUT WITH CHANGES WHICH ALLOW THE EXTRACTION OF THE SEARCH RESULTS INTO AN ARRAY AND THEIR SUBSEQUENT PRESENTATION IN HTML OF YOUR CHOICE.


*/
$relative_script_path = '.';

if (is_file("$relative_script_path/includes/config.php")) {
    include "$relative_script_path/includes/config.php";
}
else {
    die("Cannot find config.php file.\n");
}

if (is_file("$relative_script_path/libs/search_function.php")) {
    include "$relative_script_path/libs/search_function.php";
}
else {
   die("Cannot find search_function.php file.\n");
}

// extract vars
extract(phpdigHttpVars(
     array('query_string'=>'string',
           'refine'=>'integer',
           'refine_url'=>'string',
           'site'=>'integer',
           'limite'=>'integer',
           'option'=>'string',
           'lim_start'=>'integer',
           'browse'=>'integer',
           'path'=>'string'
           )
     ));


$template = "array";

$a = phpdigSearch($id_connect, $query_string, $option, $refine,
              $refine_url, $lim_start, $limite, $browse, $site, $path,
              $relative_script_path, $template, $adlog_flag, $rssdf, $template_demo);


/*

THE FOLLOWING LINES ARE USEFUL FOR DEBUG

echo "<hr>";
  print_r(array_keys($a));
echo "<hr>";

print_r(array_values($a));

echo "<hr>";
*/

$out = array_values($a);

$c = count($out);


$heading = $out[4]; //extracts the header, which you can now hack any way you like
$footer = $out[5]; //extracts the footer, which again you can hack. Since the footer contains multiple links to search.php, you need to change these to csearch.php
$footer = preg_replace('/search.php/i','csearch.php',$footer);
$footer = preg_replace('/<span[^>]*>/i','<span style="font-weight:bold; font-family:verdana; font-size:9pt; color:#536E99">',$footer); //this changes the formatting of the active page of the results that you're looking at.

//$footer = preg_replace('/<\/span>/i','</b>',$footer);

$head = $out[4]; //these next few lines replace the standard header with results x to y of z. No doubt there is a better, more phpdig, way to do this !


$needle = strpos ($heading,'total');

$heading = substr ($heading,0,$needle);

$heading = str_replace(',',' of ',$heading);

if (strpos($head,'total')!=FALSE)
	{
		$a = $out[19]; //itself an array starting at 1 - the results are in the 19th element of the returned array, for the first screen.


		if ($lim_start) $a = $out[20];//the results are in the 20th element of the returned array, for the second and subsequent screens.

		$out = array_values($a); //now $out has each search result in an array of its own. To get the names of the indices  - its an associative array- enable the debug lines above.

		$hasresults=1; //a hack flag to stop the printing out of results when there are none. This is far from elegant....
	}
else
	{
		$heading = "No hits returned";
	}

?>
<html>
	<head>
			<style>
				a{text-decoration:none; font-family:verdana; font-size:9pt; color:#536E99}
				footer.a {text-decoration:none; font-family:verdana; font-size:5pt; color:#536E99}
				span.bolder{font-weight:bold}
			</style>
	</head>
	<body style="background-image: url(../images/grade.gif); background-repeat: repeat-x; ">
		<table width=100% border=0>

<?php
		print("<font style=\"font-family:verdana; font-size:7pt; color: black\"> $heading
		</font>
		<br>"); //print out the header, which you formatted above

$i = 0;
while (($i < count($out)) and ($hasresults==1)) //this loop outputs the links.
	{
		$a = $out[$i]; //, the array with the results in it
		print("
				<tr>
					<td style=\"padding-right:0.5em;\">
						$a[page_link]
					</td>
				</tr>
			");
		  $i++;
	}
print("</table>

	<div id=\"footer\">
");
echo $footer; //print out the footer, which you formatted above.
echo "</div>";
?>

	</body>

</html>
ciaran@clissman is offline   Reply With Quote