I have made a sql query to search a database on various fields that should be between a range of values or between various multiple choices
[code=php]
<?php require_once(‘Connections/dbconn.php’); ?>
<?php
$vareyecolor_rsVis = “%”;
if (isset($_POST[‘eye_color’])) {
$vareyecolor_rsVis = (get_magic_quotes_gpc()) ? $_POST[‘eye_color’] : addslashes($_POST[‘eye_color’]);
}
$varbrlo_rsVis = “%”;
if (isset($_POST[‘brlo’])) {
$varbrlo_rsVis = (get_magic_quotes_gpc()) ? $_POST[‘brlo’] : addslashes($_POST[‘brlo’]);
}
$varbrhi_rsVis = “%”;
if (isset($_POST[‘brhi’])) {
$varbrhi_rsVis = (get_magic_quotes_gpc()) ? $_POST[‘brhi’] : addslashes($_POST[‘brhi’]);
}
$varwstlo_rsVis = “%”;
if (isset($_POST[‘wstlo’])) {
$varwstlo_rsVis = (get_magic_quotes_gpc()) ? $_POST[‘wstlo’] : addslashes($_POST[‘wstlo’]);
}
$varwsthi_rsVis = “%”;
if (isset($_POST[‘wsthi’])) {
$varwsthi_rsVis = (get_magic_quotes_gpc()) ? $_POST[‘wsthi’] : addslashes($_POST[‘wsthi’]);
}
$varhiplo_rsVis = “%”;
if (isset($_POST[‘hiplo’])) {
$varhiplo_rsVis = (get_magic_quotes_gpc()) ? $_POST[‘hiplo’] : addslashes($_POST[‘hiplo’]);
}
$varhiphi_rsVis = “%”;
if (isset($_POST[‘hiphi’])) {
$varhiphi_rsVis = (get_magic_quotes_gpc()) ? $_POST[‘hiphi’] : addslashes($_POST[‘hiphi’]);
}
$varhtlo_rsVis = “%”;
if (isset($_POST[‘htlo’])) {
$varhtlo_rsVis = (get_magic_quotes_gpc()) ? $_POST[‘htlo’] : addslashes($_POST[‘htlo’]);
}
$varhthi_rsVis = “%”;
if (isset($_POST[‘hthi’])) {
$varhthi_rsVis = (get_magic_quotes_gpc()) ? $_POST[‘hthi’] : addslashes($_POST[‘hthi’]);
}
$varhcolor_rsVis = “%”;
if (isset($_POST[‘hair_color’])) {
$varhcolor_rsVis = (get_magic_quotes_gpc()) ? $_POST[‘hair_color’] : addslashes($_POST[‘hair_color’]);
}
$varwarea_rsVis = “%”;
if (isset($_POST[‘w_area’])) {
$varwarea_rsVis = (get_magic_quotes_gpc()) ? $_POST[‘w_area’] : addslashes($_POST[‘w_area’]);
}
if(empty($offset))
$offset=0;
define(ROWS,25);
mysql_select_db($database_dbconn, $dbconn);
$query_rsVis = sprintf(“SELECT users.id, users.color_eyes, users.foto1, users.foto2, users.foto3, users.alias_name, users.meas_bres, users.meas_waist, users.meas_hip, users.height, users.cv1_date, users.cv1_title, users.cv1_contents FROM users WHERE users.color_eyes=’%s’ AND users.meas_bres>’%s’ AND users.meas_bres<‘%s’ AND users.meas_waist>’%s’ AND users.meas_waist<‘%s’ AND users.meas_hip>’%s’ AND users.meas_hip<‘%s’ AND users.height>’%s’ AND users.height<‘%s’ AND users.hair_color=’%s’ AND users.w_area=’%s'”, $vareyecolor_rsVis,$varbrlo_rsVis,$varbrhi_rsVis,$varwstlo_rsVis,$varwsthi_rsVis,$varhiplo_rsVis,$varhiphi_rsVis,$varhtlo_rsVis,$varhthi_rsVis,$varhcolor_rsVis,$varwarea_rsVis);
$rsVis = mysql_query($query_rsVis, $dbconn) or die(mysql_error());
$totalRows_rsVis = mysql_num_rows($rsVis);
Here the query only works if both the range of values is selected.
How do I modify this query if a person wants to give the option”All records” or does not insert any value in the range of values(that means all records ,of say meas_waist,no range to be specified)
Somebody pls help!!
Thanks
Ashokvas
[code=php]
<?
$a = $_POST['result_1'];
if($a == ""){
$a = "%";
};
mysql_query("SELECT * FROM my_tbl WHERE feild1 LIKE '$a';");
[/code]
[code=php]<?
$myquery = "SELECT * FROM users WHERE 1";
if (isset($_POST['eye_color'])) {
$vareyecolor_rsVis = (get_magic_quotes_gpc()) ? $_POST['eye_color'] : addslashes($_POST['eye_color']);
}
if(isset($vareyecolor_rsVis) || $vareyecolor_rsVis == ""){
$myquery .= " AND color_eyes='$vareyecolor_rsVis'";
};[/code]
[i]Originally posted by ashokvas [/i]
Thanks for your help which is as followsPHP:
--------------------------------------------------------------------------------
<?
$myquery = "SELECT * FROM users WHERE 1";
if (isset($_POST['eye_color'])) {
$vareyecolor_rsVis = (get_magic_quotes_gpc()) ? $_POST['eye_color'] : addslashes($_POST['eye_color']);
}
if(isset($vareyecolor_rsVis) || $vareyecolor_rsVis == ""){
$myquery .= " AND color_eyes='$vareyecolor_rsVis'";};
--------------------------------------------------------------------------------
I do not understand the following:
1)"SELECT * FROM users WHERE 1";
what does WHERE 1 mean?
[/quote]
2)If the user does not fill any value for 'eye_color' this will give empty value to the variable($vareyecolor_rsVis == "")
in this case the search result will be blank as there are no records where 'eye_color' is blank.
What I want is that when this variable is not set,then the search result should include all the records for all the values of 'eye_color'that have been entered in the database.
In efect it means that the " AND color_eyes='$vareyecolor_rsVis'"
should not be part of the query at all.
Hence the query should include this AND color_eyes='$vareyecolor_rsVis'part if the 'eye_color' is not set to "".
[/quote]
[code=php]
<?php require_once('Connections/dbconn.php'); ?>
<?php
$query_rsVis = "SELECT users.id, users.color_eyes, users.foto1, users.foto2, users.foto3, users.alias_name, users.meas_bres, users.meas_waist, users.meas_hip, users.height, users.cv1_date, users.cv1_title, users.cv1_contents FROM users ";
if (isset($_POST['eye_color'])) {
$vareyecolor_rsVis = (get_magic_quotes_gpc()) ? $_POST['eye_color'] : addslashes($_POST['eye_color']);
}
if(isset($vareyecolor_rsVis) && $vareyecolor_rsVis !== ""){
$query_rsVis .= " WHERE color_eyes='$vareyecolor_rsVis'";
}
if (isset($_POST['brlo'])) {
$varbrlo_rsVis = (get_magic_quotes_gpc()) ? $_POST['brlo'] : addslashes($_POST['brlo']);
}
if(isset($varbrlo_rsVis) && $varbrlo_rsVis !== ""){
$query_rsVis .= " AND users.meas_bres>='$varbrlo_rsVis'";
}
if (isset($_POST['brhi'])) {
$varbrhi_rsVis = (get_magic_quotes_gpc()) ? $_POST['brhi'] : addslashes($_POST['brhi']);
}
if(isset($varbrhi_rsVis) && $varbrhi_rsVis !== ""){
$query_rsVis .= " AND users.meas_bres<='$varbrhi_rsVis'";
}
if (isset($_POST['wstlo'])) {
$varwstlo_rsVis = (get_magic_quotes_gpc()) ? $_POST['wstlo'] : addslashes($_POST['wstlo']);
}
if(isset($varwstlo_rsVis) && $varwstlo_rsVis !== ""){
$query_rsVis .= " AND users.meas_waist>='$varwstlo_rsVis'";
}
if (isset($_POST['wsthi'])) {
$varwsthi_rsVis = (get_magic_quotes_gpc()) ? $_POST['wsthi'] : addslashes($_POST['wsthi']);
}
if(isset($varwsthi_rsVis) && $varwsthi_rsVis !== ""){
$query_rsVis .= " AND users.meas_waist<='$varwsthi_rsVis'";
}
if (isset($_POST['hiplo'])) {
$varhiplo_rsVis = (get_magic_quotes_gpc()) ? $_POST['hiplo'] : addslashes($_POST['hiplo']);
}
if(isset($varhiplo_rsVis) && $varhiplo_rsVis !== ""){
$query_rsVis .= " AND users.meas_hip>='$varhiplo_rsVis'";
}
if (isset($_POST['hiphi'])) {
$varhiphi_rsVis = (get_magic_quotes_gpc()) ? $_POST['hiphi'] : addslashes($_POST['hiphi']);
}
if(isset($varhiphi_rsVis) && $varhiphi_rsVis !== ""){
$query_rsVis .= " AND users.meas_hip<='$varhiphi_rsVis'";
}
if (isset($_POST['htlo'])) {
$varhtlo_rsVis = (get_magic_quotes_gpc()) ? $_POST['htlo'] : addslashes($_POST['htlo']);
}
if(isset($varhtlo_rsVis) && $varhtlo_rsVis !== ""){
$query_rsVis .= " AND users.height>='$varhtlo_rsVis'";
}
if (isset($_POST['hthi'])) {
$varhthi_rsVis = (get_magic_quotes_gpc()) ? $_POST['hthi'] : addslashes($_POST['hthi']);
}
if(isset($varhthi_rsVis) && $varhthi_rsVis !== ""){
$query_rsVis .= " AND users.height<='$varhthi_rsVis'";
}
if (isset($_POST['hair_color'])) {
$varhcolor_rsVis = (get_magic_quotes_gpc()) ? $_POST['hair_color'] : addslashes($_POST['hair_color']);
}
if(isset($varhcolor_rsVis) && $varhcolor_rsVis !== ""){
$query_rsVis .= " AND users.hair_color='$varhcolor_rsVis'";
}
if (isset($_POST['w_area'])) {
$varwarea_rsVis = (get_magic_quotes_gpc()) ? $_POST['w_area'] : addslashes($_POST['w_area']);
}
if(isset($varwarea_rsVis) && $varwarea_rsVis !== ""){
$query_rsVis .= " AND users.w_area='$varwarea_rsVis'";
}
if(empty($offset))
$offset=0;
define(ROWS,25);
mysql_select_db($database_dbconn, $dbconn);
$rsVis = mysql_query($query_rsVis, $dbconn) or die(mysql_error());
$totalRows_rsVis = mysql_num_rows($rsVis);
?><?php
if ($totalRows_rsVis != 0)
{
$prevOffset=$offset-ROWS;
$nextOffset=$offset+ROWS;
if(!mysql_data_seek($rsVis,$offset))
echo "Error seeking data";?>
[/code]
[code=php]
<?php
for($rowCounter=0;
(($rowCounter<ROWS)&&
($row_rsVis = mysql_fetch_assoc($rsVis)));
$rowCounter++){ ?>
[/code]
<tr>
<td height="23" colspan="4"><img src="Graphics/SimpleLine.jpg" width="625" height="5" /></td>
</tr>
<tr><td height="23" colspan="4"><div align="center">
<table width="635" border="0" cellspacing="0" cellpadding="5">
<tr>
[code=php]
<?php }
echo($offset+1)."-".
($rowCounter+$offset)." of";
echo" $totalRows_rsVis records found matching".
" your criterian<br>";
if($offset>0)
echo "nt<a href="search_Visitor_Result.php"?offset=".rawurlencode($previousOffset).">Previous</a>";
else
echo "<br>Previous";
if(($row_rsVis !=false)&&($totalRows_rsVis >$nextOffset))
echo"nt<a href="search_Visitor_Result.php"?offset=".rawurlencode($nextOffset).">Next</a>";
else
echo " Next";
}
else
{
header("Location:search_Visitor_Result2.php");
exit;
}
echo"<br><br><a href="SearchVisit.htm">Back to Search</a><br>";
?> [/code]
[code=php]
$query_rsVis = "SELECT users.id, users.color_eyes, users.foto1, users.foto2, users.foto3, users.alias_name, users.meas_bres, users.meas_waist, users.meas_hip, users.height, users.cv1_date, users.cv1_title, users.cv1_contents FROM users WHERE 1";
if (isset($_POST['eye_color'])) {
$vareyecolor_rsVis = (get_magic_quotes_gpc()) ? $_POST['eye_color'] : addslashes($_POST['eye_color']);
}
if(isset($vareyecolor_rsVis) && $vareyecolor_rsVis !== ""){
$query_rsVis .= " AND color_eyes='$vareyecolor_rsVis'";
}.....................
[/code]
[code=php]for($rowCounter=0;
(($rowCounter<ROWS)&&
($row_rsVis = mysql_fetch_assoc($rsVis)));
$rowCounter++){
if($row_rsVis['foto1'] != ""){
//normal code(with HTML and stuff) here.
};};[/code]
[code=php]
$request =array();
if(isset($_POST['eye_color'])){
$request = $_POST;
}else{
$request = $_GET;
};
// replace your reading $_POST for reading $request EG:
if (isset($request['eye_color'])) {
$vareyecolor_rsVis = (get_magic_quotes_gpc()) ? $_request['eye_color'] : addslashes($request['eye_color']);
};
// then provide your links to go like:
echo("<a href="?eye_color=".$request['eye_color']."&nextThing="$request['nextThing'].".....&page=$i">$i</a>");[/code]
I DO NOT UNDERSTAND WHY THIS IS REQUIRED?WHY ONLY if(isset($_POST['eye_color'])) then....[/quote]
DOES THIS HAVE TOBE FOR ALL THE VARIABLES IN MY QUERY??[/quote]
WHAT ABOUT USING THIS FOR LINKS ON NEXT/PREVIOUS -WILL THIS WORK ON BOTH OR DO HAVE THEY HAVE TO BE DIFFERENT?THERE WILL BE A PREVIOUS OFFSET AND A NEXT OFFSET AS PER ME.[/quote]
[code=php]
if(!isset($request['page'])){
$request['page'] = 1;
};
$request['page']--;
if($request['page'] != 0){
echo("<a href="?eye_color=".$request['eye_color']."&...&page=".($request['page'] - 1)."">previoust</a>");
};
if($request['page'] != $NumberOfPages){
echo("<a href="?eye_color=".$request['eye_color']."&...&page=".($request['page'] + 1)."">next</a>");
};
?>
[/code]
[i]Originally posted by scragar [/i]
[B][code=php]
$request =array();
if(isset($_POST['eye_color'])){
$request = $_POST;
}else{
$request = $_GET;
};
// replace your reading $_POST for reading $request EG:
if (isset($request['eye_color'])) {
$vareyecolor_rsVis = (get_magic_quotes_gpc()) ? $_request['eye_color'] : addslashes($request['eye_color']);
};
// then provide your links to go like:
echo("<a href="?eye_color=".$request['eye_color']."&nextThing="$request['nextThing'].".....&page=$i">$i</a>");[/code]
Then just read in the number so you know where in the redords to skip to(although this isn't the best description you should be able to understand roughly what I mean right?) [/B][/QUOTE]
[code=php]if(!isset($request['page'])){ //if they have not provided a page no.
$request['page'] = 1; // set it to one.
}; //end if
$request['page']--; // deduct one from it's value so it starts at zero and not 1.
[/code]
[code=php]
<?php require_once('Connections/dbconn.php'); ?>
<?php
$query_rsVis = "SELECT users.id, users.color_eyes, users.foto1, users.foto2, users.foto3, users.alias_name, users.meas_bres, users.meas_waist, users.meas_hip, users.height, users.cv1_date, users.cv1_title, users.cv1_contents FROM users WHERE 1";
$browseString="";
if (isset($_GET['eye_color'])) {
$vareyecolor_rsVis = (get_magic_quotes_gpc()) ? $_GET['eye_color'] : addslashes($_GET['eye_color']);
}
if(isset($vareyecolor_rsVis) && $vareyecolor_rsVis !== ""){
$query_rsVis .= " AND color_eyes='$vareyecolor_rsVis'";
$browseString.="eye_color=".rawurlencode($vareyecolor_rsVis)."&";
}
if (isset($_GET['brlo'])) {
$varbrlo_rsVis = (get_magic_quotes_gpc()) ? $_GET['brlo'] : addslashes($_GET['brlo']);
}
if(isset($varbrlo_rsVis) && $varbrlo_rsVis !== ""){
$query_rsVis .= " AND users.meas_bres>='$varbrlo_rsVis'";
$browseString.="brlo=".rawurlencode($varbrlo_rsVis)."&";
}
if (isset($_GET['brhi'])) {
$varbrhi_rsVis = (get_magic_quotes_gpc()) ? $_GET['brhi'] : addslashes($_GET['brhi']);
}
if(isset($varbrhi_rsVis) && $varbrhi_rsVis !== ""){
$query_rsVis .= " AND users.meas_bres<='$varbrhi_rsVis'";
$browseString.="brhi=".rawurlencode($varbrhi_rsVis)."&";
}
if (isset($_GET['wstlo'])) {
$varwstlo_rsVis = (get_magic_quotes_gpc()) ? $_GET['wstlo'] : addslashes($_GET['wstlo']);
}
if(isset($varwstlo_rsVis) && $varwstlo_rsVis !== ""){
$query_rsVis .= " AND users.meas_waist>='$varwstlo_rsVis'";
$browseString.="wstlo=".rawurlencode($varwstlo_rsVis)."&";
}
if (isset($_GET['wsthi'])) {
$varwsthi_rsVis = (get_magic_quotes_gpc()) ? $_GET['wsthi'] : addslashes($_GET['wsthi']);
}
if(isset($varwsthi_rsVis) && $varwsthi_rsVis !== ""){
$query_rsVis .= " AND users.meas_waist<='$varwsthi_rsVis'";
$browseString.="wsthi=".rawurlencode($varwsthi_rsVis)."&";
}
if (isset($_GET['hiplo'])) {
$varhiplo_rsVis = (get_magic_quotes_gpc()) ? $_GET['hiplo'] : addslashes($_GET['hiplo']);
}
if(isset($varhiplo_rsVis) && $varhiplo_rsVis !== ""){
$query_rsVis .= " AND users.meas_hip>='$varhiplo_rsVis'";
$browseString.="hiplo=".rawurlencode($varhiplo_rsVis)."&";
}
if (isset($_GET['hiphi'])) {
$varhiphi_rsVis = (get_magic_quotes_gpc()) ? $_GET['hiphi'] : addslashes($_GET['hiphi']);
}
if(isset($varhiphi_rsVis) && $varhiphi_rsVis !== ""){
$query_rsVis .= " AND users.meas_hip<='$varhiphi_rsVis'";
$browseString.="hiphi=".rawurlencode($varhiphi_rsVis)."&";
}
if (isset($_GET['htlo'])) {
$varhtlo_rsVis = (get_magic_quotes_gpc()) ? $_GET['htlo'] : addslashes($_GET['htlo']);
}
if(isset($varhtlo_rsVis) && $varhtlo_rsVis !== ""){
$query_rsVis .= " AND users.height >='$varhtlo_rsVis'";
$browseString.="htlo=".rawurlencode($varhtlo_rsVis)."&";
}
if (isset($_GET['hthi'])) {
$varhthi_rsVis = (get_magic_quotes_gpc()) ? $_GET['hthi'] : addslashes($_GET['hthi']);
}
if(isset($varhthi_rsVis) && $varhthi_rsVis !== ""){
$query_rsVis .= " AND users.height <='$varhthi_rsVis'";
$browseString.="hthi=".rawurlencode($varhthi_rsVis)."&";
}
if (isset($_GET['hair_color'])) {
$varhcolor_rsVis = (get_magic_quotes_gpc()) ? $_GET['hair_color'] : addslashes($_GET['hair_color']);
}
if(isset($varhcolor_rsVis) && $varhcolor_rsVis !== ""){
$query_rsVis .= " AND users.hair_color='$varhcolor_rsVis'";
$browseString.="hair_color=".rawurlencode($varhcolor_rsVis)."&";
}
if (isset($_GET['w_area'])) {
$varwarea_rsVis = (get_magic_quotes_gpc()) ? $_GET['w_area'] : addslashes($_GET['w_area']);
}
if(isset($varwarea_rsVis) && $varwarea_rsVis !== ""){
$query_rsVis .= " AND users.w_area='$varwarea_rsVis'";
$browseString.="w_area=".rawurlencode($varwarea_rsVis);
}
if(empty($offset))
$offset=0;
define(ROWS,4);
mysql_select_db($database_dbconn, $dbconn);
$scriptName="search_Visitor_Result.php";
$rsVis = mysql_query($query_rsVis, $dbconn) or die(mysql_error());
$totalRows_rsVis = mysql_num_rows($rsVis);
?><?php
if ($totalRows_rsVis != 0)
{
$previousOffset=$offset-ROWS;
$nextOffset=$offset+ROWS;
if(!mysql_data_seek($rsVis,$offset))
echo "Error seeking data";?>
[/code]
[code=php]
<?php
for($rowCounter=0;
(($rowCounter<ROWS)&&
($row_rsVis = mysql_fetch_assoc($rsVis)));
$rowCounter++){ ?>[/code]
[code=php]
<?php }
echo($offset+1)."-".
($rowCounter+$offset)." of";
echo" $totalRows_rsVis records found matching".
" your criterian<br>";
if($offset>0)
echo "nt<a href="".$scriptName."?offset=".rawurlencode($previousOffset)."&".$browseString."">Previous</a>";
else
echo "<br>Previous";
if(($row_rsVis !=false)&&($totalRows_rsVis >$nextOffset))
echo"nt<a href="".$scriptName."?offset=".rawurlencode($nextOffset)."&".$browseString."">Next</a>";
else
echo " Next";
}
else
{
header("Location:search_Visitor_Result2.php");
exit;
}
echo"<br><br><a href="SearchVisit.htm">Back to Search</a><br>";
?>[/code]
0.1.9 — BETA 5.19