Jump to content

[Resolvido] Sistema de pesquisa


PF2G
 Share

Recommended Posts

Boas

Eu estou a trabalhar num website onde tou a trabalhar com o sistema de pesquisa, tenho uma textbox que pesquisa no google (pesquisa.php) e quero acrescentar outra textbox onde ele pesquisa na base de dados (pesq_prod.php):

A minha duvida é acrescentar o pesq_prod.php e não sei se ta correto:

pesquisa.php

<?php require_once('Connections/futura.php'); ?>

<?php 


# Your site URL
$site = 'www.plasticos-futura.com';

# Make sure a search-query was entered
if(isset($_GET['q'])) {
# Are we on the first page?
$start = isset($_GET['start']) ? $_GET['start'] : 0;

# The URL to the Google search API
$url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=' .urlencode($_GET['q']) .'%20site:' .$site .'&rsz=large&start=' .$start . '&hl=pt-PT';

# Initiate CURL
/*$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://' .$site);
$body = curl_exec($ch);
curl_close($ch);*/

//set pear path TER ETENÇÃO QUE ASSIM SO DA LOCALMENTE! E DA OUTRA MANEIRA MAIS ABAIXO O INIT_SET SO DA ONLINE
/*ini_set(
	"include_path", ('PEAR' .
		";" .
		ini_get("include_path")
		)
);*/

ini_set(
	"include_path", (
		$_SERVER['DOCUMENT_ROOT'] . '/PEAR' .
		":" .
		ini_get("include_path")
		)
);



require_once ('es/PEAR/JSON.php');
require_once ('es/PEAR/HTTP/Request.php');

$req =& new HTTP_Request($url);
$req->sendRequest();

$body = $req->getResponseBody();

# Decode the JSON returned (you need the JSON-class if json_decode is undefined)
$body = json_decode($body);

# Build array
$i	= 0;
$search_results = array();


foreach($body->responseData as $rc) {
	$n_resultados = $rc->estimatedResultCount;
	$i++;
}
# Loop through all results

foreach($body->responseData->results as $r) {
	$search_results['results'][$i]['title'] = $r->title;
	$search_results['results'][$i]['url'] = $r->url;
	$search_results['results'][$i]['content'] = $r->content;
	$i++;
}

# Loop through all the pages of results
if(isset($body->responseData->cursor->pages)) {
	foreach($body->responseData->cursor->pages as $p) {
		$search_results['pages'][] = $p->start;
	}
}

if (empty($_POST['q']))
{
	echo'preencha o campo Q';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="styles/style.css" rel="stylesheet" />
<title>Plásticos Futura - Pesquisa</title>
<link rel="shortcut icon" type="image/x-icon" href="imagens/plasticos.ico" />
<script type="text/javascript" src="js/java.js"></script>
</head>

<body>
<table width="1200" border="0" align="center" cellpadding="0" cellspacing="0" style="background-image:url(imagens/fundo5.jpg);background-repeat:repeat-y;">
<tr>
<td>
<table width="1200" border="0" align="center" cellpadding="0" cellspacing="0" style="background-image:url(imagens/fundo3.jpg); background-repeat:no-repeat;" height="713" >
  <tr>
    <td height="60" valign="bottom"><?php include("topo.php"); ?></td>
  </tr>
  <tr>
    <td height="613" valign="top"><table width="1200" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td valign="top"><table width="260" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td align="right"><a href="index.php" title="Voltar à homepage"><img src="imagens/plasticos-futura.png" border="0" /></a></td>
          </tr>
          <tr>
            <td height="40"></td>
          </tr>
          <tr>
            <td><table width="250" border="0" align="right" cellpadding="0" cellspacing="0">
              <tr>
                <td width="30"></td>
                <td height="26" class="menu" onclick="window.location='index.php'"><a href="index.php" title="Home - Plásticos Futura" class="menu_text">Home</a></td>
              </tr>
              <tr>
                <td height="2"></td>
                <td height="2"></td>
              </tr>
              <tr>
                <td width="30"></td>
                <td height="26" class="menu" onclick="window.location='empresa.php'"><a href="empresa.php" class="menu_text" title="Empresa - Plásticos Futura">Empresa</a></td>
              </tr>
              <tr>
                <td height="2"></td>
                <td height="2"></td>
              </tr>
              <tr>
                <td width="30"></td>
                <td height="26" class="menu" onclick="window.location='produtos.php'"><a href="produtos.php" class="menu_text" title="Produtos - Plásticos Futura">Produtos</a></td>
              </tr>
              <tr>
                <td height="2"></td>
                <td height="2"></td>
              </tr>
              <tr>
                <td width="30"></td>
                <td height="26" class="menu" onclick="window.location='noticias.php'"><a href="noticias.php" class="menu_text"  title="Notícias - Plásticos Futura">Notícias</a></td>
              </tr>
               <tr>
                <td height="2"></td>
                <td height="2"></td>
              </tr>
		  <tr>
                  <td width="30"></td>
                  <td height="26" class="menu" onclick="window.location='oportunidades-de-negocio.php'"><a href="oportunidades-de-negocio.php" class="menu_text"  title="Oportunidades de Negócio - Plásticos Futura">Oportun. Negócio</a></td>
                </tr>
                <tr>
                  <td height="2"></td>
                  <td height="2"></td>
                </tr>
		  <tr>
                  <td width="30"></td>
                  <td height="26" class="menu" onclick="window.location='recrutamento.php'"><a href="recrutamento.php" class="menu_text"  title="Recrutamento - Plásticos Futura">Recrutamento</a></td>
                </tr>
                <tr>
                  <td height="2"></td>
                  <td height="2"></td>
                </tr>
              <tr>
                <td width="30"></td>
                <td height="26" class="menu" onclick="window.location='contactos.php'"><a href="contactos.php" class="menu_text"  title="Contactos - Plásticos Futura">Contactos</a></td>
              </tr>
            </table></td>
          </tr>
        </table></td>
        <td width="638" valign="top" class="texto_pretobig2"><div class="bomfundo"><strong>RESULTADOS DA PESQUISA::</strong><br />
            
              <br />
              <div align="justify">Resultados de pesquisa com a palavra "<b><?php echo @$_GET['q']; ?></b>":<br />
<br /> <?php 

				foreach($search_results['results'] as $sr) { ?>
				<a href="<?php echo urldecode($sr['url']); ?>" class="texto_laranja_normal"><b><?php echo $sr['title']; ?></b></a><br />
<?php echo $sr['content']; ?><br />
<br />
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - <br />
<br />
<?php }  } if(!isset($search_results['pages'])) {
	?>
		<strong>Não foram encontrados resultados!</strong>
	<?php
}
                  ?> <br />
<br />
<?php if(isset($search_results['pages'])) { ?>
            	Encontrado(s) <?php echo $n_resultados . " registo(s)."; ?> Página 
			<?php $i = 0; foreach($search_results['pages'] as $p) { $i++; ?>
                    
                        <?php if((!isset($_GET['start']) && $p == 0) || (@$_GET['start'] == $p)) { ?>
                            <a class="texto_preto"><?php echo $i; ?></a>
                        <?php } else { ?>
                            <a class="texto_preto" href="?q=<?php echo @$_GET['q']; ?>&start=<?php echo $p; ?>&paginas=mais/pesquisa"><?php echo $i; ?></a>
                        <?php } ?>
                    
                <?php } ?>
            
				<?php } ?></div>
        </div></td>
        <td width="178"></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="40"><?php include("rodape.php"); ?></td>
  </tr>
</table>
</td>
  </tr>
  </table>
</body>
</html>

pesq_prod.php

<?PHP
$prod = $_POST ['pq'];
$sql_prod = "SELECT * FROM produtos WHERE nome LIKE '%$prod%'";

$res=mysql_query($query) or die(mysql_error());
$count=mysql_num_rows($res);

if (empty($prod))
{
	echo 'TEXTBOX VAZIA';
}

if($count==0)
{
	$warning = "<font color='red'>A sua pesquisa nao encontrou resultados</font>";
}
else
{
	$warning = "<font color='green'>A sua pesquisa deu ".$count." resultados</font><br><p>";
}
?>                                                                

fiz os echo para testar...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. 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.