Display results by building up the search criteria using checkboxes

It should so long as your values are unique.
You could put the following line in a foreach as you wrrite each checkbox (the value 2 will obviously come from a variable, and you’d need to use that variable in all three places in the below line)

foreach ($items as $key => checkboxValue)
{
  echo '<input type="checkbox" name="check[]" value="' . $checkboxValue . '" onClick="javascript:checkRefresh()" ';
  if (in_array($checkboxValue, $_REQUEST['check']))
    echo 'checked="checked" ';
  echo '/>' . $checkboxValue;
}

OK thats new stuff for me, so are you saying that when im creating the checkboxes for the regions to display according to the country ID, that will be inside as below:


<?php
$r=mysql_query("select * from tbl_hotels");
while($q=mysql_fetch_assoc($r)){

foreach ($items as $key => checkboxValue)
{
  echo '<input type="checkbox" name="check[]" value="' . $q['checkboxValue'] . '" onClick="javascript:checkRefresh()" ';
  if (in_array($q['checkboxValue'], $_REQUEST['check']))
    echo 'checked="checked" ';
  echo '/>' . $q['checkboxValue'];
}

?>

MMM, no thats not right is it, can you see where I’m trying to get my head around it. Sorry cpradio, once again you thankfully joined in to help and the level rocketed by ten fold, and left me behind a little. Sorry.

Paste your original code for generating the checkboxes, and let’s start from there.

Ok thanks, I hadn’t started doing it dynamically yet, but will set it out now, its pretty basic at this point to be honest, as the user chooses the country before coming to this page and thus it has an ID, so basically the regions will then be chosen according to that ID as below.


<form name="form1">
<?php
$r=mysql_query("select * from tbl_contnreg where (Id_Cntry='1')");
while($q=mysql_fetch_assoc($r)){ ?>
<input type="checkbox" name="check[]" value="<?=$q['Id_Rsrt']?>" onClick="javascript:checkRefresh()"/><?=$q['Id_Rsrt']?>
<? } ?>
</form>

There will be around 4-5 of these sections in the end, one for regions, one for star rating, one for ammenities and another for groups as in hotel groups, might be more but dont think so.

So the idea is after the page loads once the country is selected they see all the hotels from that country, then they can check checkboxes to get a more specific result, and each time they check a box, the page refreshes and the display changes.

Here is the page in full so far;


<?
session_start();
error_reporting(E_ALL);
ini_set('display_errors','On');
include("config.php");
?>
<?
$url = $_SERVER['REQUEST_URI'];
print "<pre>";

print $url;

$url_parsed = parse_url($url);
print_r($url_parsed);

parse_str($url_parsed['query'], $url_parts);
print_r($url_parts);

print "</pre>";
?>

<head>
<script language="JavaScript">
function checkRefresh(value)
{
	document.form1.submit();
}
</script>

</head>

<body>
<div id="wrapper">
<form name="form1">
<?php
$r=mysql_query("select * from tbl_contnreg where (Id_Cntry='1')");
while($q=mysql_fetch_assoc($r)){ ?>
<input type="checkbox" name="check[]" value="<?=$q['Id_Rsrt']?>" onClick="javascript:checkRefresh()"/><?=$q['Id_Rsrt']?>
<? } ?>
</form>

I would think something like this:

<form name="form1"> 
<?php   
$r=mysql_query("select * from tbl_contnreg where (Id_Cntry='1')");  
while($q=mysql_fetch_assoc($r)){ ?> 
<input type="checkbox" name="check[]" value="<?=$q['Id_Rsrt']?>" onClick="javascript:checkRefresh()" <?=((in_array($q['Id_Rsrt'], $_REQUEST['check'])) ? "checked=\\"checked\\"" : "")?> /><?=$q['Id_Rsrt']?> 
<? } ?> 
</form>

Thanks cpradio, that worked perfectly.

I’m thinking of the next step now, and will give that a go.

The next step is to build the query up to display the hotels going by the values in the array I suppose, as those numbers are the Id’s of the regions.

I will do some investigating and make a start and will likely return, but thank you for the help so far.

I havent started on the other bit yet, as I needed to draw the names out of the database of the regions rather than the id’s to show, so I used the code below, which works, but it only seems to draw out a couple of the names of the regions not all of them.

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


<?php
$r=mysql_query("select * from tbl_contnreg where (Id_Cntry='1')");
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\\"" : "")?> />
	<?php
	$m=mysql_query("select Id_Rsrt, Nom_Rsrt from tbl_resorts where (Id_Rsrt=".$q['Id_Rsrt'].")");
	if($b=mysql_fetch_assoc($m)){ ?>
	<?=$b['Nom_Rsrt']?>
	<? } ?><br/>
<? } ?>

cant think of the problem

Oh hang on, it doesnt look like its a code issue, more of what is being displayed.

need to look into this, sorry should have checked first.

Ye sorry about that, it was my fault, all looking good now.

First bit done!

Probably a query issue, but if you need help, describe the problem in a bit more detail and I’ll be glad to assist.

Whilst looking at another area I’m also looking at how to get the array set properly to use it when selecting from the database, and I have below sort of working, but its bringing back the results and also it seems to be doubling up, and doing some strange things.

There something not quite right with it.


if (is_array($_GET['regions'])) {
     foreach($_GET['regions'] as $regions) {
		 $regionsArray[] = '\\''.$regions.'\\'';
       //echo $regions;
	 $ids = implode(',', $regionsArray);
       echo $ids;
     }
}

You can see the array in the dev area building below, and the added values in the array

http://devchecksafetyfirst.csf.dcmanaged.com/?regions[]=160&regions[]=155&regions[]=288&regions[]=157&regions[]=255

Its your code :slight_smile: You have the echo statement inside the foreach loop, dope! So you are echoing it out on every iteration.

if (is_array($_GET['regions'])) { 
     $regionsArray = array();
     foreach($_GET['regions'] as $regions) { 
         $regionsArray[] = '\\''.$regions.'\\''; 
     } 
       //echo $regions; 
     $ids = implode(',', $regionsArray); 
     echo $ids; 
} 

LOL, i’ve just set myself back 5 years…

Sorry cpradio, yep thats it, works fine now.

(
[regions] => Array
(
[0] => 160
[1] => 155
[2] => 288
[3] => 157
[4] => 255
[5] => 305
[6] => 156
[7] => 340
)

)

‘160’,‘155’,‘288’,‘157’,‘255’,‘305’,‘156’,‘340’

Cheers

I’m moving through this a bit every day now, and im now trying to tie things up in the select from database operation, but the code below is now not working, when the simpler version was.


$sql.= "SELECT * FROM tbl_hotels " . $sqlregion . " ";
$sql.= " order by Id_Hot";
while($q=mysql_fetch_assoc($sql)){ ?>
<p><?=$q['Nom_Hot']?></p>

The simpler version


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

What is the value of $sqlregion?

Its the array for the regions bit


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

So at this very moment its - WHERE IdRsrt_Hot IN (‘155’,‘157’,‘255’,‘340’ )

http://devchecksafetyfirst.csf.dcmanaged.com/?regions[]=155&regions[]=157&regions[]=255&regions[]=340

I suppose this below would be more like where Im trying to get too, or build up.

Im going to use if statements to see whether to use the different arrays or not.


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

Okay, two things.

  1. Be aware of what is in your foreach! (see comments below)
if (is_array($_GET['regions'])) { 
     $regionsArray = array(); 
     foreach($_GET['regions'] as $regions) { 
     $regionsArray[] = '\\''.$regions.'\\''; 
     $regionData = implode(',', $regionsArray); // should be outside your foreach
     $sqlregion =  'WHERE IdRsrt_Hot IN ('. $regionData .' )';  // should be outside your foreach
     } 
     //echo $regionData; 
     //echo $sqlregion; 
} 
  1. Call mysql_query; mysql_fetch_assoc only works with a mysql_query result, you never called mysql_query passing in your $sql…
$sql.= " select * from tbl_hotels "; 
$sql.= " " .$sqlregion . ""; 
$sql.= " order by Id_Hot"; 
while($q=mysql_fetch_assoc($sql)){ ?> 
<p><?=$q['Nom_Hot']?></p>

Ok thank you on the first bit, didnt realise to do that, but yes it does make sense now.


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

The second bit i added it in and now i get white out:


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

Yeah, that isn’t a legal syntax there…

This would be prefered:

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

That code cpradio is causing it to output the same thing over and over, the page is taking a long time to load.

It takes ages to display, then when it did all i could see was the first result over and over again.


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