Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Fala pessoal, tudo beleza?
Estou fazendo um sisteminha de busca onde você apenas digita a palavra chave, e ele busca em várias tabelas(Escolhidas préviamente).
A base do sistema é o Anywhere in Db, acontece que o anywhere in db busca em toda database (como diz o nome né ^_^ ) e o que eu quero é buscar em tabelas e colunas especificas.
Alguem pode me ajudar nessa "empreitada"? Sem dúvida vai ser útil para futuros usuários que precisarem de algo parecido!
Como eu sei que código grande faz a galera desanimar até de olhar vou postar onde é feita a seleção das colunas na db, tudo que eu preciso é listar as colunas de acordo com qual tabela foi encontrado resultados. Ex.: Se foi tabela clientes, só exibir 3 colunas dessa tabela, Nome, Tel,Email, capisce?
Esse fragmento de código seleciona as colunas:
$sql = 'desc '.$tables[$i]['Tables_in_'.$dbname];
$res = mysql_query($sql);
$collum = fetch_array($res);
Apenas quero colocar um condição do tipo:
if ($tables[$i]['Tables_in_'.$dbname] == "proceeding") { Aqui selecionaria a coluna, mas como!? }
Vamo lá, o que eu tenho até agora:
Ps: O connect da database não esta aí no código.
<div class="bloc">
<div class="title">
title
</div>
<div class="content">
<form method="post">
<input type="text" name="search_text" />
</form>
<?php
function fetch_array($res){
$data = array();
while ($row = mysql_fetch_assoc($res))
{
$data[] = $row;
}
return $data;
} //@endof function fetch_array
function table_arrange($array){
$table_data = ''; // @abstract returning table
$max =0; // @abstract max lenth of a row
$max_i =0; // @abstract number of the row which is maximum max lenth of a row
$search_text = $_POST["search_text"];
for($i=0;$i<sizeof($array);$i++){
//@abstract table row
$table_data .= '<tr class='.(($i&1)?'"odd_row"':'"even_row"') .' >';
//
$j=0;
foreach($array[$i] as $key => $data){
//@abstract a class around the search text
$data = preg_replace("|($search_text)|Ui" , "<pre class=\"search_text\"><b>$1</b></pre>" , htmlspecialchars($data));
$table_data .= '<td>'. $data .' </td>';
$j++;
}
if($max<$j){
$max = $j;
$max_i = $i;
}
$table_data .= '</tr>'."\n";
}
$table_data .= '</table></div>';
unset($data);$data_a = $array[$max_i];
$table_head = '<tr>';
foreach($data_a as $key => $value){
$table_head .= '<td class="keys">'. $key.'</td>';
}
$table_head .= '</tr>'."\n";echo '<div class="table_bor">
<table cellspacing="0" cellpadding="3" border="0" class="data_table">'.$table_head.$table_data;
}if(!empty($_POST['search_text']))
// @abstract for each Search Text we seach in the database $search_text = mysql_real_escape_string($_POST['search_text']);
$result_in_tables = 0;
// @abstract table count in the database
$dbname = 'saeservicos';
$tables = array( //Tabelas onde desejo fazer a busca.
array('Tables_in_'.$dbname => 'clients'),
array('Tables_in_'.$dbname => 'proceeding'),
);
//endof table count
for($i=0;$i<sizeof($tables);$i++)
// @abstract for each table of the db seaching text
{
//@abstract querry bliding of each table
$sql = 'select count(*) from '.$tables[$i]['Tables_in_'.$dbname];
$res = mysql_query($sql);
if(mysql_num_rows($res)>0)
//@abstract Buliding search Querry, search
{
//@abstract taking the table data type information
$sql = 'desc '.$tables[$i]['Tables_in_'.$dbname];
$res = mysql_query($sql);
$collum = fetch_array($res);
//$collum = array(array('nome'));
$search_sql = 'select * from '.$tables[$i]['Tables_in_'.$dbname].' where ';
$no_varchar_field = 0;
for($j=0;$j<sizeof($collum);$j++)
// @abstract only finding each row information
{
## we are searching all the fields in this table
//if(substr($collum[$j]['Type'],0,7)=='varchar'|| substr($collum[$j]['Type'],0,7)=='text')
// @abstractonly type selection part of query buliding
// @todo seach all field in the data base put a 1 in if(1)
// @example if(1)
//{
//echo $collum[$j]->Field .'<br />';
if($no_varchar_field!=0){$search_sql .= ' or ' ;}
$search_sql .= '`'.$collum[$j]['Field'] .'` like \'%'.$search_text.'%\' ';
$no_varchar_field++;
//} // endof type selection part of query bulidingtype selection part
}//@endof for |buliding search query
if($no_varchar_field>0)
// @abstract only main searching part showing the data
{
$res = mysql_query($search_sql);
$search_result = fetch_array($res);
if(sizeof($search_result))
// @abstract found search data showing it!
{
$result_in_tables++;
if ($tables[$i]['Tables_in_'.$dbname] == "proceeding") //só para testes
{
echo "Tabela Proceeding";
}
if ($tables[$i]['Tables_in_'.$dbname] == "clients") //só para testes
{
echo "Tabela Clients";
}
echo
'<span class="number_result"> Total Results for <i>"'.$search_text .'"</i>: '.mysql_affected_rows().'</span>
<br/>';
table_arrange($search_result);
echo '<br/><br/>';
}// @endof showing found search
}//@endof main searching
}//@endof querry building and searching
}
if(!$result_in_tables)
// @abstract if result is not found
{
echo '<div class="notif error">
<strong>Erro :</strong> Sua busca não retornou resultados, tente usar outras palavras-chave.
<a class="close" href="#"></a>
</div>';
}
mysql_close();
}
// @abstract common footter
?>
</div>
</div>
Agradeço a ajuda de todos, qualquer opinião é bem-vinda!
att,Dênis Fernandes
Carregando comentários...