Basically the following code queries google for search pages on my website. I am trying to create the 'google' style of result pages at the bottom of the page.
I can get the code to work, but its starts at page 0 when I want it to start at page 1.
My head hurts from thinking too much. Could someone point out where I am going wrong please.
Code: Select all
<?php
// include the soap class
include("nusoap.php");
// create a instance of the SOAP client object
$soapclient = new soapclient("http://api.google.com/search/beta2");
// uncomment the next line to see debug messages
// $soapclient->debug_flag = 1;
//Create the search form
if (!$_REQUEST['query'])
{
echo " ";
echo "<table width="85%" border="0" cellspacing="0" cellpadding="0" align="center">";
echo "<tr>";
echo "<form action="".$_SERVER['PHP_SELF']."" method="post">";
echo "<td width="17%">";
echo "<input type="text" name="query" value="".stripslashes($_REQUEST['query'])."">";
echo "</td>";
echo "<td width="9%">";
echo "<input type="image" border="0" name="Submit" value="Submit" src="/images/Template/SearchBut.gif">";
echo "</td>";
echo "</form>";
echo "<td width="74%"> </td>";
echo "</tr>";
echo "</table>";
echo "<p>";
}
ELSE
{
//Specify search of defined site only
$SearchQ = "site:localhost ".$_REQUEST['query'];
// Number of results per page
$rows_per_page = 10;
// check if $pageid is set
if (!isset($_GET['pageid']))
{
$pageid = 0;
}else{
$pageid = $_GET['pageid'];
}
// Determine record to begin displaying from
$start = $pageid * $rows_per_page;
// set up an array containing input parameters to be passed to the remote procedure
$params = array(
'key' => 'MyLicenseKey', // Google license key
'q' => $SearchQ, // search term
'start' => $start, // start from result n
'maxResults' => $rows_per_page, // show a total of n results
'filter' => true, // remove similar results
'restrict' => '', // restrict by topic
'safeSearch' => true, // remove adult links
'lr' => 'lang_en|lang_fr', // restrict by language
'ie' => '', // input encoding
'oe' => '' // output encoding
);
// invoke the method on the server
$result = $soapclient->call("doGoogleSearch", $params, "urn:GoogleSearch", "urn:GoogleSearch");
// print the results of the search
// if error, show error
if ($result['faultstring'])
{
echo "<h2>Error</h2>";
echo "".$result['faultstring']."";
}
else
{
//Display form again with search box containing previous search
echo " ";
echo "<table width="85%" border="0" cellspacing="0" cellpadding="0" align="center">";
echo "<tr>";
echo "<form action="".$_SERVER['PHP_SELF']."" method="post">";
echo "<td width="17%">";
echo "<input type="text" name="query" value="".stripslashes($_REQUEST['query'])."">";
echo "</td>";
echo "<td width="9%">";
echo "<input type="image" border="0" name="Submit" value="Submit" src="/images/Template/SearchBut.gif">";
echo "</td>";
echo "</form>";
echo "<td width="74%" align="right" Class="BodyText">Your search for <b>".stripslashes($_REQUEST['query'])."</b> produced ".$result['estimatedTotalResultsCount']." results.</td>";
echo "</tr>";
echo "</table>";
echo "<p>";
//Show list of matches with links
if (is_array($result['resultElements']))
{
foreach ($result['resultElements'] as $r)
{
echo "<table width="85%" border="0" cellspacing="0" cellpadding="0" align="center">";
echo "<tr>";
echo "<li><a href=" . $r['URL'] . ">" . $r['title'] . "</a>";
echo "<br>";
echo $r['snippet'] . "(" . $r['cachedSize'] .
")";
echo "<p>";
}
}
echo "</ul>";
echo "</tr>";
echo "</table>";
//Following code creates and displays result page links
echo "<table width="90%" border="0" cellspacing="0" cellpadding="0" align="center">";
echo "<tr>";
echo "<td>";
echo "<div align="center">";
// Count of all returned records
$total_records = $result['estimatedTotalResultsCount'];
// Determine number of pages we will need
$pages = ceil($total_records / $rows_per_page);
$rows = $result['estimatedTotalResultsCount'];
// Create Previous link if pageid is greater than 0
if ($pageid > 0) {
$url = "test.php?pageid=" . ($pageid - 1) . "&query=" . $query;
echo "<a href="$url">Previous</a>\n";
}
// Create page number links
for ($i = 0; $i < $pages; $i++) {
$url = "test.php?pageid=" . $i . "&query=" . $query;
if ($i == $pageid)
{
echo " <font class="CurrentPage">$i</font> ";
} else {
echo " <a href="$url">$i</a> ";
}
}
// Create Next link if we still have pages to display
if ((($pageid+1)*$rows_per_page)< $total_records)
{
if ($pageid < $pages)
{
$url = "test.php?pageid=" . ($pageid + 1) . "&query=" . $query;
echo "<a href="$url">Next</a>\n";
}
}
echo "</div>";
echo "</td>";
echo "</tr>";
echo "</table>";
}
}
?>Code: Select all
tags.[/color]