PDA

View Full Version : Paging mordred's search script results (no Mysql)


Alex Piotto
08-27-2002, 04:30 PM
Hi everybody... and hallo mordred, if you are reading this :)

Below goes the search script that mordred helps me to build, with some little modifications (thanks, mordred!).
The script works fine.
Now I am trying to add a function to split the results of the search through different pages.
I was able to count the results, calculate the total number of pages etc, and until there is still ok...

but, I cannot find (yet) a way to split the results and create the links, without Mysql.

the db I am using is like this

id;field1;field2;field3;field4;field5;

Here is the code, that works fine but don't split the results... only calculates what should be....
The link part belong to another script... I just copied there to be oriented, once I can split the results!
The code:
___________________________________________________
<?
Header('Cache-Control: no-cache');
Header('Pragma: no-cache');

//if ($file = ($wichdb)){
if ($file){$file = ($wichdb);}
else {$file = "Catalogo-1.txt";}

if ($file){
echo "<html><body style='font-family:verdana;font size:12px;'>";
echo "<form method='post' action='$PHP_SELF'>";
echo "<input type='hidden' name='submitted' value='done'>";
echo "<input type='hidden' name='filename' value='$file'>";
echo "<b>Procurar por:</b> <input type='text' name='keyword' size='58'>";
echo "<input type='submit' value='Buscar' name='submit'></form>";
}
$searchPattern = ''; // contains the searchPattern
function search_csv($file, $searchString){
if (empty($searchString)) {return false;} // check if the search string contains nothing
global $searchPattern; $results = array(); //Declare global variables
$keywords = preg_split('/[\s]+/', trim($searchString)); // prepare search pattern
$keywords = array_values(array_unique($keywords)); // remove duplicate entries
for ($k = 0; $k < count($keywords); $k++) {
$keywords[$k] = '\b' . preg_quote($keywords[$k], '/') . '\b';} // escape preg specific characters and find only complete keywords
$searchPattern = '/(' . implode('|', $keywords) . ')/i'; // build the search case-insensitive pattern
$fp = fopen($file, "r"); // go through the file
while ( ($str = fread($fp, 1)) != "\n" ) {} // advance the file pointer to the next line
while ($fields = fgetcsv ($fp, 4096, ";")) { // iterate through each line and
if (preg_match ($searchPattern, $fields[1] . ' ' . $fields[2]. ' ' . $fields[3]. ' ' . $fields[4]. ' ' . $fields[5])) {
$results[] = $fields; // search all the fields
}}
fclose ($fp);
return $results;
}

// call the function if form has been submitted
if (isset($HTTP_POST_VARS['submitted']) && $HTTP_POST_VARS['submitted'] = 'done') {
$file = ($filename);
$results = search_csv($file, $HTTP_POST_VARS['keyword']);
echo "<div style='font-family:verdana; font-size:12px; color:#000000;'>\n";
if (!$results || count($results) == 0) { echo "<font color='blue'><b>Não encontrei nada...</b></font>\n";}
else { echo "<font color='blue'><b>Resultados da Busca:</b></font><br /><br />\n";

// Do some calculations
$numrows = count($results);
if (!($limit)){$limit = 10;}
if (!($page)){$page = 0;}
$pages = intval($numrows/$limit);
if ($numrows%$limit) { $pages++;}
$current = ($page/$limit) + 1;
if (($pages < 1) || ($pages == 0)) { $total = 1;}
else {$total = $pages;}
$first = $page + 1;
if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {$last = $page + $limit;}
else{$last = $numrows;}

// Write down how many items in the results, how many pages etc.
echo "<html><head><style>A:hover {color: #FF0000; font-size: 13 px; font-family: Verdana;}</style></head>";
echo "<body link='#0000FF' vlink='#0000FF' alink='#0000FF'><table style='font-family:verdana; font-size:13px;' width='100%' border='0' cellspacing='0'><tr>";
echo "<td style='border-top: 1 solid #0000FF; border-bottom: 1 solid #0000FF' width='50%' align='left'>Items <b>$first</b> - <b>$last</b> de <b>$numrows</b><br></td>";
echo "<td style='border-top: 1 solid #0000FF; border-bottom: 1 solid #0000FF' width='50%' align='right'>Página <b>$current</b> de <b>$total</b><br></td></tr></table><br>";

// PROBLEMS STARTS HERE... ################################################################

// Write down (all) search results: I need to split the results in pages!!!!!!!!

$limit = "10";
$allresults = count($results);

for ($i = 0; $i < $allresults; $i++) {
echo "<b>Bairro: </b>" . preg_replace($searchPattern, "<span style='color:red;'>\\1</span>", $results[$i][1]) . "<br />";
echo "<b>Genero: </b>" . preg_replace($searchPattern, "<span style='color:red;'>\\1</span>", $results[$i][2]) . "<br />";
echo "<b>Tipo: </b>" . preg_replace($searchPattern, "<span style='color:red;'>\\1</span>", $results[$i][3]) . "<br />";
echo "<b>USD/Mês: </b>" . preg_replace($searchPattern, "<span style='color:red;'>\\1</span>", $results[$i][4]) . "<br />";
echo "<b>Descrição: </b>" . preg_replace($searchPattern, "<span style='color:red;'>\\1</span>", $results[$i][5]) . "<br /><hr>\n";
}
}
echo "</div>";
}

// Write down the links for the other pages
if ($results){
echo "<table width='100%' align='center' border='0' style='font-family:verdana; font-size:13px;'><tr><td align='right'>Páginas: ";
for ($i=1; $i <= $pages; $i++){
$ppage = $limit*($i - 1);
if ($ppage == $page){ echo("<font color='red'><b>$i</b></font>\n");}
else{ echo("<a href=\"$PHP_SELF?newviewdata=$file&value=$ppage&page=$ppage\" onfocus='blur()'>$i</a> \n");}}
echo "</td></tr></table></body></html>";
}
?>
</body>
</html>
_________________________________________________

Any suggestion will be welcome....

Thank

Alex :confused: :confused: