Link to home
Start Free TrialLog in
Avatar of awd2004
awd2004

asked on

PHP MYSQL Advanced search code problem - dynamic form and checkboxes

I have a php page which has a form on it, on the form there is checkboxes which are drawn dynamically from a database.

The Code for the check boxes is

<?php
  while (!$RsPropertyFeatures->EOF) { // horizontal looper
?>
                                        <td><table width="100" border="0" cellpadding="0" cellspacing="0">
                                            <tr>
                                              <td><table width="200"  border="0" cellspacing="2" cellpadding="2">
                                                  <tr>
                                                    <td width="36"><img src="PortfolioFiles/PropertyFeatures/<?php echo $RsPropertyFeatures->Fields('PicFile'); ?>"> </td>
                                                    <td width="124"><?php echo $RsPropertyFeatures->Fields('PropertyFeatureRef'); ?></td>
                                                    <td width="20"><input name="propertyfeature" type="checkbox" id="propertyfeature" value="checkbox"></td>
                                                  </tr>
                                              </table></td>
                                              <?php
   
?>
                                            </tr>
                                        </table></td>
                                        <?php
    $RsPropertyFeatures->MoveNext();
    if (!isset($nested_RsPropertyFeatures)) {
      $nested_RsPropertyFeatures= 1;
    }
    if (!$RsPropertyFeatures->EOF && $nested_RsPropertyFeatures++%2==0) {
      echo "</tr><tr>";
    }
    //end horizontal looper
  }
?>

Now I have a search function which constructs a mysql query for the options that are chosen on the html form.

This is the function so far

<?php
//Connection statement
require_once('../Connections/RondaConnections.php');

// begin Recordset
$query_RsProperties = "SELECT * FROM   rcproperty   LEFT OUTER JOIN rctowns ON (rcproperty.Location = rctowns.idLocation)   LEFT OUTER JOIN rclocale ON (rcproperty.Locale = rclocale.idLocale) WHERE rcproperty.idAccount = 6";
$RsProperties = $RondaConnections->SelectLimit($query_RsProperties) or die($RondaConnections->ErrorMsg());
$totalRows_RsProperties = $RsProperties->RecordCount();
// end Recordset
 //PHP ADODB document - made with PHAkt 2.7.3?>
<?php

//Declare Form Variables

$Ref=$_POST['Ref'];
$HouseType=$_POST['PropertyType'];
$Region=$_POST['Region'];
$Town=$_POST['Town'];
//$LPrice=$_POST['LPrice'];
//$HPrice=$_POST['HPrice'];
$Bedrooms=$_POST['Bedrooms'];
$Bathrooms=$_POST['Bathrooms'];
$Locale=$_POST['Locale'];



// SQL Query
$SQLQUERY = " ";
// Declare optional SQL Statement
// Ref No
if ($Ref != "") {
          $SQLQUERY .= "AND RefNo = '$Ref'";
          }
// Property Type
if ( !empty($HouseType) && $HouseType == 'all' ) {
;
}
else {
          $SQLQUERY .= "AND rcproperty.idPropertyType = '$HouseType'";
          }
// Region
if ( !empty($Region) && $Region == 'all' ) {
;
}
else {
       $SQLQUERY .= "AND Location = '$Region'";
          }
// Town
if ( !empty($Town) && $Town == 'all' ) {
;
}
else {
        $SQLQUERY .= "AND TownName = '$Town'";
          }
// Bedrooms
if ( !empty($Bedrooms) && $Bedrooms == 'all' ) {
;
}
else {
         $SQLQUERY .= "AND rcproperty.Bedrooms = '$Bedrooms'";
          }
           
// Bathrooms
if ( !empty($Bathrooms) && $Bathrooms == 'all' ) {
;
}
else {
          $SQLQUERY .= "AND Bathrooms = '$Bathrooms'";
         }
// Locale
if ( !empty($Locale) && $Locale == 'all' ) {
;
}
else {
          $SQLQUERY .= "AND Locale = '$Locale'";
         }

// Store Results in Variable
$result = mysql_query($query_RsProperties . $SQLQUERY) or die(mysql_error() . " - $query_RsProperties" . $SQLQUERY);
?>
<?php while($row = mysql_fetch_assoc($result)) {
echo "" .$row['PropertyName']. "" . "<BR>";
}
 ?>

Which works Fine as far as the drop down boxes and text fields are concerned.
Now the problem is the checkboxes.  the checkboxes are property features which are drawn from another table in the database.  The propertytofeatures table has this structure

CREATE TABLE `rcpropertytofeatures` (
  `idPropertyToFeature` tinyint(1) NOT NULL auto_increment,
  `IdProperty` tinyint(4) default NULL,
  `idPropertyFeature` tinyint(4) default NULL,
  PRIMARY KEY  (`idPropertyToFeature`),
  UNIQUE KEY `idPropertyToFeature` (`idPropertyToFeature`)
) TYPE=MyISAM

and related to that table is is the actual propertfeatures table which has this structure

CREATE TABLE `rcpropertyfeatures` (
  `idPropertyFeature` int(1) NOT NULL auto_increment,
  `PropertyFeatureRef` varchar(32) default NULL,
  `idAccount` int(11) default NULL,
  `Description` varchar(128) default NULL,
  `PicFile` varchar(100) default NULL,
  `idLanguage` tinyint(4) default NULL,
  PRIMARY KEY  (`idPropertyFeature`),
  UNIQUE KEY `idPropertyFeatures` (`idPropertyFeature`),
  UNIQUE KEY `idPropertyFeature` (`idPropertyFeature`),
  KEY `idProperty` (`idAccount`)
) TYPE=MyISAM

Now what i need to do (I think) Is query the $result again to find out which properties have which property features if they have selected any of them.

How do i do this????
ps this was the original question I asked before i realised it wouldnt work!!!
https://www.experts-exchange.com/questions/21143846/Search-Function-with-PHP-Checkboxes-Question.html
ASKER CERTIFIED SOLUTION
Avatar of x-tinct
x-tinct

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of frugle
frugle

I'd think a split would be the best way forward, as we both solve bits of the question - x-tinct shows how to get the input into an array and I show how to use that information in the new page.

Mike
I agree