Display results by building up the search criteria using checkboxes

Hmm, okay, so it must be executing the mysql_query($sql) on each loop.

Try this:

$sql.="select * from tbl_hotels ";  
$sql.= "" .$sqlregion . "";  
$sql.= " order by Id_Hot";  
$result = mysql_query($sql);
while($q=mysql_fetch_assoc($result)){  
?>  
<p><?=$q['Nom_Hot']?></p> 
<? } ?>

Ye thats it, thanks cpradio.

keep on trucking!

I am trying to give some feedback of choice back to the user in the form of a sort of breadcrumb and get the name I need from the checkboxes, but the name that shows on the site is what I want not the id that I am passing into the array, so I thought about using a hidden type field, and it sort of done the job but instead pf picking out the ones that have been selected, instead it echo’d out all the names of the regions.

http://devchecksafetyfirst.csf.dcmanaged.com/?regionsName[]=Djerba&regions[]=155&regionsName[]=Hammamet&regions[]=288&regionsName[]=Mahdia&regionsName[]=Monastir&regionsName[]=Nabeul&regionsName[]=Sfax&regionsName[]=Sousse&regionsName[]=Tabarka&regionsName[]=Tozeur-Nefta&regionsName[]=Tunis-Cotes+de+Carthage&regionsName[]=Zarzis

Si I tried this below:


<input type="checkbox" name="regions[]" value="<?=$q['Id_Rsrt']?>" onClick="javascript:checkRefresh()" <?=((in_array($q['Id_Rsrt'], $_REQUEST['regions'])) ? "checked=\\"checked\\"" : "")?> class="inline" /> <?=$q['Nom_Rsrt']?><br/>
<input type="hidden" name="regionsName[]" value="<?=$q['Nom_Rsrt']?>" <?=((in_array($q['Nom_Rsrt'], $_REQUEST['regionsName'])) ? "checked=\\"checked\\"" : "")?> class="inline" />

But like i said i got all of the names out, what do you think is the best way to get the selected names out =“<?=$q[‘Nom_Rsrt’]?> as well as the id’s =”<?=$q[‘Id_Rsrt’]?>

I’m not following… You know what regions were requested they are in $_REQUEST[‘regions’], just create a list of them and get the names from the table for your breadcrumb…

oh right ye, ye maybe i was trying to hard there.

What I did was created another array.


if (is_array($_GET['regionsName'])) {
	 $regionsNameArray = array();
     foreach($_GET['regionsName'] as $regionsName) {
	 $regionsNameArray[] = '\\''.$regionsName.'\\'';
     }
	 $regionNameData = implode(',', $regionsNameArray);
	 //$sqlregion =  'WHERE IdRsrt_Hot IN ('. $regionNameData .' )';	
	 echo $regionNameData;
	 //echo $sqlregion;
}

And then tried to capture the name of the region rather than its ID through a hidden input.

But ye I could use the array to search and display those names that way isnt it.

Ye got it now, sorry to bother you on that one cpradio.

I tried to be too clever then, and it was in a way the most diificult way.


$sqlregionName =  'WHERE Id_Rsrt IN ('. $regionData .' )';	


<div style="position:relative; width:100%; height:30px; text-align:left; padding-left:20px; line-height:28px;">
Breadcrumb: <? $q=mysql_query("select Id_Rsrt, Nom_Rsrt from tbl_resorts " . $sqlregionName . "");
while($n=mysql_fetch_assoc($q)){ ?>
<?=$n['Nom_Rsrt']?>,
<? } ?>
</div>

Just the job

At first glance, do you see anything wrong with the code of below:


<?php
$r=mysql_query("select Id_Rsrt, Nom_Rsrt, IdCntry_Rsrt, Id_show from tbl_resorts where (IdCntry_Rsrt='7') and (Id_show='1') order by Nom_Rsrt ASC");
while($q=mysql_fetch_assoc($r)){ ?>
<input type="checkbox" name="regions[]" value="<?=$q['Id_Rsrt']?>" onClick="javascript:checkRefresh()" <?=((in_array($q['Id_Rsrt'], $_REQUEST['regions'])) ? "checked=\\"checked\\" " : "")?> class="inline" /> <?=$q['Nom_Rsrt']?><br/>

<?
$queryNumber = mysql_query("select count(Id_Hot) as total from tbl_hotels where ('IdRsrt_Hot=". $q['Id_Rsrt'] . "') and (Act_Hot='1') ");
$resultNumber = mysql_fetch_array($queryNumber);
echo $resultNumber['total'];
?>

<? } ?>

I’m not getting any errors, just not getting the result I need.

I basically want to display how many hotels are in each regions depending on the values, its this part im trying to get right


<?php
$queryNumber = mysql_query("select count(Id_Hot) as total from tbl_hotels where ('IdRsrt_Hot=". $q['Id_Rsrt'] . "') and (Act_Hot='1') ");
$resultNumber = mysql_fetch_array($queryNumber);
echo $resultNumber['total'];?>

All Im getting back is Zero’s

Yes, look at your query:

"select count(Id_Hot) as total from tbl_hotels where ('IdRsrt_Hot=". $q['Id_Rsrt'] . "') and (Act_Hot='1') "

Now parse it:

"select count(Id_Hot) as total from tbl_hotels where ('IdRsrt_Hot=VALUE_OF_ID_RST') and (Act_Hot='1') "

See the problem? That isn’t valid syntax in any imaginable way.

I think you meant to have:

"select count(Id_Hot) as total from tbl_hotels where (IdRsrt_Hot='". $q['Id_Rsrt'] . "') and (Act_Hot='1') "

Which when parsed, looks like:

"select count(Id_Hot) as total from tbl_hotels where (IdRsrt_Hot='VALUE_OF_ID_RST') and (Act_Hot='1') "

Yep I can see it, and yes yours works fine.

Thanks again.

I seem to be running into a few these now, where there no error, the results are displaying, but the results arent correct for what I need.

I have had to change the code that outputs the regions from one table so that it also checks another table to see if all of the hotels associated with it are equal to 1.


<?
$r=mysql_query("select DISTINCT Id_Rsrt, Nom_Rsrt, IdCntry_Rsrt, Id_show, Act_Hot from tbl_resorts, tbl_hotels where (tbl_resorts.IdCntry_Rsrt='1') and (tbl_resorts.Id_show='1') and (tbl_hotels.Act_Hot='1') order by Nom_Rsrt ASC");
?>

Its outputting it correctly, but not doing the final search of (tbl_hotels.Act_Hot=‘1’), which will mean that no region is displayed with [0] next to it.

http://devchecksafetyfirst.csf.dcmanaged.com/

Is there a problem with the code that I havent picked up on.

Cheers

Ah I seem to have it sorted.

I added another bit in to make sure they match up, and it worked, but in my head it didnt seem the natural thing to do, so it suprised me when it worked.


<?
$r=mysql_query("select DISTINCT Id_Rsrt, Nom_Rsrt, IdCntry_Rsrt, Id_show, Act_Hot, IdRsrt_Hot from tbl_resorts, tbl_hotels where (tbl_resorts.IdCntry_Rsrt='1') and (tbl_resorts.Id_show='1') and (tbl_hotels.Act_Hot='1') and (tbl_resorts.Id_Rsrt=tbl_hotels.IdRsrt_Hot) order by Nom_Rsrt
?>

This bit


<?
and (tbl_resorts.Id_Rsrt=tbl_hotels.IdRsrt_Hot)
?>

This is a guess, but its probably because you didn’t join the tables, you purely asked for information from both tables (try the below).

select 
  DISTINCT Id_Rsrt, 
    Nom_Rsrt,
    IdCntry_Rsrt, 
    Id_show, 
    Act_Hot, 
    IdRsrt_Hot 
from tbl_resorts
  LEFT JOIN tbl_hotels ON (tbl_resorts.Id_Rsrt=tbl_hotels.IdRsrt_Hot)
where (tbl_resorts.IdCntry_Rsrt='1') 
  and (tbl_resorts.Id_show='1') 
  and (tbl_hotels.Act_Hot='1') 
order by Nom_Rsrt 

Yes that worked, and its a new way that I havent come across before…

Thanks.

MMMM, major problem now.

All was good until I added the countries in to allow the user to flick through the countries, and its fine, as I can get the ID from the country to make sure the regions are showing correctly for the country.

But then its when I select a region which are pushed into an array and I cant add the country back into the url to keep the user on the same country until they select another.

http://devchecksafetyfirst.csf.dcmanaged.com/index.php?Country=65

When I select a country i can easily pass the id through, but I cant work out when i select a region how to pass the country id through with it so I can pick it up by using GET and assigning it to a variable.

Have I gone and snookered myself?


<?
session_start();
error_reporting(E_ALL);
ini_set('display_errors','Off');
include("config.php");

$selectCountry=@$_GET['Country'];

// Regions array
if (is_array($_GET['regions'])) {
	 $regionsArray = array();
     foreach($_GET['regions'] as $regions) {	
	 $regionsArray[] = '\\''.$regions.'\\'';
     }
	
	 $regionData = implode(',', $regionsArray);	
	 $sqlregion =  'AND IdRsrt_Hot IN ('. $regionData .' )';
	 $sqlregionName =  'WHERE Id_Rsrt IN ('. $regionData .' )';
	
	 //echo $sqlregionName;
	 //echo $regionData;
	 //echo $sqlregion;
}

// Star rating array
if (is_array($_GET['star'])) {
	 $starArray = array();
     foreach($_GET['star'] as $star) {
	 $starArray[] = '\\''.$star.'\\'';
     }
	 $starData = implode(',', $starArray);
	 $sqlstar =  'AND IdCat_Hot IN ('. $starData .' )';	
	 //echo $regionData;
	 //echo $sqlstar;
}
?>
<!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>
<title>CSF Result Page</title>
<link href="css/style.css" rel="stylesheet" type= "text/css" />
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
</head>
<body>
<div id="wrapper">
<div id="header">
<p style="position:relative; padding:10px;">All countries below:</p>
<? $d=mysql_query("select * from tbl_countries order by Nom_Cntry ASC");
while($f=mysql_fetch_assoc($d)){ ?>
'<a href="index.php?Country=<?=$f['Id_Cntry']?>"><?=$f['Nom_Cntry']?></a>',
<? } ?>
</div>

<div id="result_Center_Block">

<!-- Start of Breadcrumb
<div style="position:relative; width:100%; height:30px; text-align:left; padding-left:12px; padding-top:5px; padding-bottom:20px;">
<p><strong>Showing results for:</strong>
<? if ($sqlstar=="") { ?>

<? } else { ?>
<?=$starData?> star hotels in
<? } ?>
<? if ($sqlregion=="") { ?>
Tunisia
<? } else { ?>
<? $q=mysql_query("select Id_Rsrt, Nom_Rsrt from tbl_resorts " . $sqlregionName . "");
while($n=mysql_fetch_assoc($q)){ ?>
'<?=$n['Nom_Rsrt']?>',
<? } ?>
TUNISIA
<? } ?>
</p>
</div>End of breadcrumb -->

<div id="result_Left_Nav_Bar">
<form name="form1" id="myForm">
<!-- Regions -->
<p><strong>Regions:</strong></p>
<?php
$r=mysql_query("SELECT DISTINCT Id_Rsrt, Nom_Rsrt, IdCntry_Rsrt, Id_show, Act_Hot, IdRsrt_Hot FROM tbl_resorts LEFT JOIN tbl_hotels ON (tbl_resorts.Id_Rsrt=tbl_hotels.IdRsrt_Hot) WHERE (tbl_resorts.IdCntry_Rsrt='".$selectCountry."') AND (tbl_resorts.Id_show='1') AND (tbl_hotels.Act_Hot='1') ORDER BY Nom_Rsrt");
while($q=mysql_fetch_assoc($r)){ ?>
<input type="checkbox" name="regions[]" value="<?=$q['Id_Rsrt']?>" onClick="javascript:checkRefresh()" <?=((in_array($q['Id_Rsrt'], $_REQUEST['regions'])) ? "checked=\\"checked\\" " : "")?> class="inline" /><?=$q['Nom_Rsrt']?></a>&nbsp;<?
$queryNumber = mysql_query("select count(Id_Hot) as total from tbl_hotels where (IdRsrt_Hot='". $q['Id_Rsrt'] ."') and (Act_Hot='1')");
$resultNumber = mysql_fetch_array($queryNumber); ?>
<span style="font-size:12px; color:#900; vertical-align:1px;">[<? echo $resultNumber['total'];?>]</span>
<br/>
<? } ?>
<!-- End Regions -->
<br/>
<p><strong>Star Rating:</strong></p>
<!-- Star Rating -->
<?php
if ($sqlregion==""){
$s=mysql_query("select DISTINCT IdCat_Hot from tbl_hotels WHERE IdCat_Hot IN (1,2,3,4,5) and (Act_Hot='1') order by IdCat_Hot ASC");
while($b=mysql_fetch_assoc($s)){ ?>
<input type="checkbox" name="star[]" value="<?=$b['IdCat_Hot']?>" onClick="javascript:checkRefresh()" <?=((in_array($b['IdCat_Hot'], $_REQUEST['star'])) ? "checked=\\"checked\\" " : "")?> class="inline" /> <?=$b['IdCat_Hot']?> Star<br/>
<? } ?>	
<? } else {
$s=mysql_query("select DISTINCT IdCat_Hot from tbl_hotels WHERE IdRsrt_Hot IN (". $regionData .") and (Act_Hot='1') order by IdCat_Hot ASC");
while($b=mysql_fetch_assoc($s)){ ?>
<input type="checkbox" name="star[]" value="<?=$b['IdCat_Hot']?>" onClick="javascript:checkRefresh()" <?=((in_array($b['IdCat_Hot'], $_REQUEST['star'])) ? "checked=\\"checked\\" " : "")?> class="inline" /> <?=$b['IdCat_Hot']?> Star<br/>
	<? } ?>
<? } ?>
<!-- End Star rating -->

</form>
</div>
<div id="result_Right_Results">

<?
// Build result query on values of the array
$sql.="select * from tbl_hotels where Act_Hot='1' ";
	if ($sqlregion==""){
	$sql.= " and IdCntry_Hot='".$selectCountry."'";
	} else {
	$sql.= "" .$sqlregion . "";
	}
	if ($sqlstar==""){
	$sql.= " and IdCat_Hot IN (1,2,3,4,5,6)";
	} else {
	$sql.= "" .$sqlstar . "";
	}
$sql.= " order by Id_Hot ASC";
$result = mysql_query($sql);
while($q=mysql_fetch_assoc($result)){
?>
<div class="result_Hotel">
<p style="position:relative; margin:10px;"><?=$q['Nom_Hot']?></p>
</div>
<? }
// End Build query	
?>

</div>
</div>
<div style="position:relative; width:100%; height:2px; clear:both"></div>
</div>
<?php require_once('footer.php'); ?>

<!-- Black Overlay Script -->
<script type="text/javascript">
      jQuery.fn.center = function () {
        this.css("position","absolute");
        this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
        this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
        return this;
      }

      $(document).ready(function() {
        $("input:checkbox").on("change", function(e) {
		document.form1.submit();
          $("#result").hide();
          //var v = ($(this).val());

          $('<div id="black_overlay"></div>').appendTo('body');
          $('<div id="loading">Loading Content</div>').appendTo('body').center();

          $.ajax({
            //type : "POST",
            //url : "sleep.php",
           // data  : 'v=' + v,
            success : function(res) {
              $("#loading").remove();
			
              $('#black_overlay').fadeOut('slow', function() {
                $('#black_overlay').remove();
               // $("#result").text("You chose option " + res).css("display", "inline-block");
              });
			
            }
          });
        });
      });
</script>
<!-- End Black Overlay Script -->
</body>
</html>

Where is your code for building the URL?

This is the problem as far as I can see, there isnt any, but what happens by the looks is that when a checkbox is selected it call this function in the javascript at the bottom of the page, which includes the document.form1.submit()


$(document).ready(function() {
        $("input:checkbox").on("change", function(e) {
		document.form1.submit();

Then when I look in the url it shows the id of the region i selected and where I need also to add the current country selection, so when I select a country it looks like this:

http://devchecksafetyfirst.csf.dcmanaged.com/index.php?Country=57

Then when i select a region within that country the url becomes:

http://devchecksafetyfirst.csf.dcmanaged.com/index.php?regions[]=289

Added Country as a hidden field to your form.

<?php if (isset($_GET['Country'])) { ?>
 <input type="hidden" name="Country" value="<?=$_GET['Country'];?>" />
<?php } ?>

Eventually, you may need to make this even smarter so it just adds all get variables as hidden fields (something like so)

<?php foreach ($_GET as $key => $val) { ?>
 <input type="hidden" name="<?=$key;?>" value="<?=$val;?>" />
<?php } ?>

Ah right, I see.

I understand the first example, but not sure at all about the second one.

Is it a way of joining the hidden fields up one go, is that it?

Yes, however, keep in mind that it will take everything sent via GET and write it as a hidden field (it may still need some work too). So if you have a clever user, they could inject their own form hidden fields by passing them in the URL.

I’m using the code below that you posted a little while ago, but rather than connecting two tables, how would it work if I needed to connect to 3 tables.


<?
$r=mysql_query("SELECT DISTINCT Id_Tem, Id_Hot, Id_Tem, Id_Hot, IdCntry_Hot, IdRsrt_Hot, Act_Hot FROM tbl_tematics LEFT JOIN tbl_hotels ON (tbl_resorts.Id_Rsrt=tbl_hotels.IdRsrt_Hot) WHERE (tbl_resorts.IdCntry_Rsrt='".$selectCountry."') AND (tbl_resorts.Id_show='1') AND (tbl_hotels.Act_Hot='1') ORDER BY Nom_Rsrt");
?>