Jump to content
  • Revista PROGRAMAR: Já está disponível a edição #60 da revista programar. Faz já o download aqui!

diokhan

search com checkbox

Recommended Posts

diokhan

boas,

eu agora estou a fazer um search e posso escolher algumas opções com checkbox, como por exemplo, em que campos da tabela procurar, tenho assim o código neste momento:

<?php
echo'
<form method="GET" action="restrito.php">
<fieldset>
	<legend><b>Search Enzyme</b></legend>
        <input type="hidden" name="opcao" value="searchEnzyme">
       	<b>Search for:</b><br>
        <input type="text" id="consulta" name="consulta" maxlength="100" size="75" /><br><br>
        <b>Search in:</b><br>
        <input type="checkbox" name="acceptedName" value="accepted_name">Accepted Name<br>
        <input type="checkbox" name="otherName" value="other_names">Other Name<br>
        <input type="checkbox" name="systematicName" value="sys_name">Systematic Name<br>
        <input type="checkbox" name="heading" value="heading">Heading Class<br>
        <input type="checkbox" name="EC" value="ec_num">EC<br>';
       	
	$alias = array(
            'accepted_name' => 'Accepted Name',
		'other_names' => 'Other Name',
		'sys_name' => 'Systematic Name',
		'ec_num' => 'EC'
	);

	$lst_enzyme = $alias[$_GET['lst_enzyme']];

	echo'<br><br><center><input type="submit" name="submit" value="Search" /></center>
	<br>';
	if(empty($_GET['consulta'])){

	}elseif (isset($_GET['consulta'])) 
		{
			echo 'Searching for:<strong> '.$consulta.'</strong>';
		}
echo' 
</fieldset>
</form>';
include ('enzymeSearch.php');
if (empty($_GET['consulta'])) {

}else{
echo "
<table class='sortable' align='center'>
<tr>
	<th>
		Accepted Name
	</th>
	<th>
		Other Name
	</th>
	<th>
		Systematic Name
	</th>
	<th>";
	if (!isset($_GET['heading'])){

	}else{
		echo "Heading Class";
	}
	echo"
	</th>
	<th>
		EC
	</th>
</tr>";

$res = $conexao->query($paginacao->sql());
while($r = $res->fetch(PDO::FETCH_OBJ)) {
	echo"
	<tr>
		<td>";
			print $r->accepted_name;
		echo"</td>
		<td>";
			print $r->other_names;
		echo"</td>
		<td>";
			print $r->sys_name;
		echo"</td>
		<td>";
		if (!isset($_GET['heading'])){

		}else{
			print $r->heading;
		}
		echo"</td>
		<td>";
			print $r->ec_num;
		echo"</td>
	</tr>";
}
}
echo "</table>"
?>

e no enzymeSearch.php:

<?php
require_once ('./admin/page.php');	
require_once ('./admin/constantes.php');

$szTemp = "";
$count = 0;
if (isset($_GET['submit'])) {
if (isset($_GET['acceptedName'])) {
	$acceptedName = $_GET['acceptedName'];
	$szTemp = " WHERE C.ACCEPTED_NAME LIKE '%".$acceptedName."%' ";
}

if (isset($_GET['otherName'])){
	$otherName = $_GET['otherName'];
	$count=$count+1;

	if (!empty($szTemp)){
		$szTemp .= "AND ";
	}else{
		$szTemp .= " WHERE ";
	}
	$szTemp .= "C.OTHER_NAMES LIKE '%".$otherName."%' ";
}

if (isset($_GET['systematicName'])){
	$systematicName = $_GET['systematicName'];
	$count=$count+1;

	if (!empty($szTemp)){
		$szTemp .= "AND ";
	}else{
		$szTemp .= " WHERE ";
	}
	$szTemp .= "C.SYS_NAME LIKE '%".$systematicName."%' ";
}

if (isset($_GET['heading'])){
	$heading = $_GET['heading'];
	/*$count=$count+1;

	if (!empty($szTemp)){
		$szTemp .= "AND ";
	}else{
		$szTemp .= " WHERE ";
	}
	$szTemp .= "E.NAME_GO_TERMS LIKE '%".$nameGOTerms."%' ";*/
}

if (isset($_GET['EC'])){
	$EC = $_GET['EC'];
	$count=$count+1;

	if (!empty($szTemp)){
		$szTemp .= "AND ";
	}else{
		$szTemp .= " WHERE ";
	}
	$szTemp .= "C.EC_NUM LIKE '%".$EC."%' ";
}
}
$conexao = new Conexao();
$conexao = $conexao->conexao;

$paginacao = new Paginacao_PDO();

if (!isset($_GET['consulta'])) {

}elseif (empty($consulta)){
$auth_error = '<div id="notification_error">Search empty! Please enter a search word!</div>';
   	echo $auth_error;
}elseif (strlen($consulta) < 3){
$auth_error = '<div id="notification_error">Search must be at least 3 characters!</div>';
   	echo $auth_error;
}else{
/*if (isset($heading)){
	$paginacao->sql = "SELECT * FROM ENTRY AS E WHERE heading
						LIKE '%".$_GET['consulta']."%' ORDER BY ID ASC";
}else{*/
	$paginacao->sql = "SELECT * FROM CLASS AS C" . $szTemp . " ORDER BY ID ASC";
//}
}
?>

de momento não funciona, não faz a query, alguem pode ajudar-me a ver o erro ou ajudar-me a melhorar o meu código?

PS: dividi o código em duas partes para não ficar muito confuso, assim separei o código por partes importantes


...Join the dark side...and get a free cookie...

Share this post


Link to post
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

×

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.