Jump to content

Search Bar & Results with pagination


moreira

Recommended Posts

I need some help.

I've created a search bar (search.php) that submit the result (resultado.php).

Everthing is ok, except the pagination! Results on first page good but the links for the other pages not good (it seems that looses the input information) "Notice: Undefined index: pesquisa in resultado.php on line 6".

I tried to work with session but whitout sucess.

Can anybody help?

 

the search.php:

<table width="200" border="0" align="center" valign="top">
  <tr>
      <td><div align="center"><form id="form1" name="form1" method="get" action="search.php">
  <label for="textfield"></label>
  <input name="pesquisa" type="text" id="pesquisa" value="pesquisar por..." size="26" />
</form>
</td>

 

and the resultado.php:

<?php
include("classes/Pagination.php");
include("classes/config.php");

$dbc = @mysqli_connect(localhost, root, mifa, ocorredorcms);
$keyword = trim($_GET['pesquisa']) ;
$keyword = mysqli_real_escape_string($dbc, $keyword);

//PAGINATION SETTINGS
//the pagination requires the total amount of articles
$total_records = mysql_num_rows(mysql_query("SELECT * FROM cms_content where title like '%$keyword%' or body like '%$keyword%' or category like '%$keyword%'"));
  $page = 1;
  $size = 3;
  if (isset($_GET['page'])){
$page = (int) $_GET['page'];
}
//Pagination class
$pagination = new Pagination();
//here we use the variable $siteurl which is defined in the configuration.php, should you ever use mod_rewrite to rewrite your url's
//it would be a good idea to add the $siteurl variable to your urls, same for images or links to your styles and scripts
$pagination->setLink("search.php?page=%s");
$pagination->setPage($page);
$pagination->setSize($size);
$pagination->setTotalRecords($total_records);
////////////
$sql = mysql_query("SELECT * FROM cms_content where title like '%$keyword%' or body like '%$keyword%' or category like '%$keyword%' ". $pagination->getLimitSql());
$sql_num = mysql_num_rows($sql);
//if the result of the function mysqli_num_rows IS null then show a message
  //when we store something into a variable we use = , but when we want to check/compare for something we use == or when comparing strings ===
  if($sql_num == 0):
//so if there is nothing show the message

echo '<h2>Não foram encontrados resultados para: "'.$_GET['pesquisa'].'"</h2>';

else:
//while there are rows print them on the screen
		if(isset($_GET['artigo'])){
			$artigo_id = $_GET['artigo'];
			$query = mysql_query("SELECT * FROM cms_content WHERE id = {$artigo_id}");
			$artigo = mysql_fetch_array($query);
		} else {
			$query = mysql_query("SELECT * FROM cms_content where title like '%$keyword%' or body like '%$keyword%' or category like '%$keyword%' ORDER BY id DESC ". $pagination->getLimitSql());
			while($artigo = mysql_fetch_array($query)){
				echo "<div class=\"artigo-info\">";
				echo 	'<h2><a href="'.$artigo['category'].'.php?artigo='.$artigo['id']."\">" . $artigo['title'] . "</a></h2>";
				echo '<p class="style4">' . $artigo['description'] . '</p>';
				echo 	'<j10><a href="'.$artigo['category'].'.php?artigo='.$artigo['id']."\">" .  "LER O ARTIGO COMPLETO >" . "</a></j10>";	
				echo "</div>";
			}
		}
							$navigation = $pagination->create_links();
				echo '<div align="center">' . $navigation . '</div>';
endif;
?>

 

 

please help :(

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.