Jump to content

Recommended Posts

Hi All

 

I am struggling to get the results i need from a search form i am using.

 

my form consists of both drop down lists and checkboxes. the results are diplaying all the users rather than displaying just the users with those matching criteria

 

this is the select method i am using

 

$gender = $_REQUEST['gender'];
$height = $_REQUEST['height'];
$sexuality = $_REQUEST['sexuality'];
$build = $_REQUEST['build'];
$cities = $_REQUEST['cities'];
$membertype = $_REQUEST['membertype'];
$agegroup = $_REQUEST['agegroup'];
$lookingfor = $_REQUEST['lookingfor'];
$activities = $_REQUEST['activities'];

SELECT DISTINCT * , GROUP_CONCAT( DISTINCT value )
FROM jos_users a
JOIN jos_community_users b ON a.id = b.userid
JOIN jos_community_fields_values c ON b.userid = c.user_id
AND ( field_id=2 and value='$gender') OR (field_id=20 and value='$sexuality') OR (field_id=18 and value='$height') OR
(field_id=19 and value='$build') OR (field_id=10 and value='$cities') OR 
(field_id=22 and value='$membertype') OR (field_id=21 and value='$lookingfor') OR 
(value = '$activities') GROUP BY a.id  DESC 

Is there a way of just singling out the users that only have those values?

 

thanks in advance!!!

 

What table does field_id refer to ?

 

try

SELECT DISTINCT * , GROUP_CONCAT( DISTINCT value )
FROM jos_users a
LEFT JOIN jos_community_users b ON a.id = b.userid
LEFT JOIN jos_community_fields_values c ON b.userid = c.user_id
WHERE (c.field_id=2 and c.`value`='$gender') OR (c.field_id=20 and c.`value`='$sexuality') OR (c.field_id=18 and c.`value`='$height') OR
(c.field_id=19 and c.`value`='$build') OR (c.field_id=10 and c.`value`='$cities') OR
(c.field_id=22 and c.`value`='$membertype') OR (field_id=21 and c.`value`='$lookingfor') OR
(c.`value` = '$activities') 
GROUP BY a.id  DESC 

Hi

 

Further to the above, should the last clause ((value = '$activities')) have a check for a particular field id?

 

If you want to keep the clauses in the ON condition then I think you also need an extra set of brackets around all the OR'ed items.

 

All the best

 

Keith

would some form of isset work maybe?

 

You could try something like this (excuse likely typos):-

 

$gender = $_REQUEST['gender'];
$height = $_REQUEST['height'];
$sexuality = $_REQUEST['sexuality'];
$build = $_REQUEST['build'];
$cities = $_REQUEST['cities'];
$membertype = $_REQUEST['membertype'];
$agegroup = $_REQUEST['agegroup'];
$lookingfor = $_REQUEST['lookingfor'];
$activities = $_REQUEST['activities'];

$whereClause = "";
$whereClause .= ".((isset($gender)) ? ((isset($whereClause) ? " OR " : " WHERE " )."( field_id=2 and value='$gender') " : "")." 
$whereClause .= ".((isset($sexuality)) ? ((isset($whereClause) ? " OR " : " WHERE " )."(field_id=20 and value='$sexuality')" : "")." 
$whereClause .= ".((isset($height)) ? ((isset($whereClause) ? " OR " : " WHERE " )."(field_id=18 and value='$height') " : "")." 
$whereClause .= ".((isset($build)) ? ((isset($whereClause) ? " OR " : " WHERE " )."(field_id=19 and value='$build') " : "")." 
$whereClause .= ".((isset($cities)) ? ((isset($whereClause) ? " OR " : " WHERE " )."(field_id=10 and value='$cities') " : "")." 
$whereClause .= ".((isset($membertype)) ((isset($whereClause) ? " OR " : " WHERE " )."(field_id=22 and value='$membertype') " : "")." 
$whereClause .= ".((isset($lookingfor)) ((isset($whereClause) ? " OR " : " WHERE " )."(field_id=21 and value='$lookingfor') " : "")." 
$whereClause .= ".((isset($activities)) ((isset($whereClause) ? " OR " : " WHERE " )."(value = '$activities') " : "")." 

$sql = "SELECT DISTINCT * , GROUP_CONCAT( DISTINCT value )
FROM jos_users a
JOIN jos_community_users b ON a.id = b.userid
JOIN jos_community_fields_values c ON b.userid = c.user_id
$whereClause
GROUP BY a.id  DESC ";

 

All the best

 

Keith

Hi

 

Probably a load of missing semi colons on the end of the lines assigning $whereClause.

 

With those typos fixed:-

 

$gender = $_REQUEST['gender'];
$height = $_REQUEST['height'];
$sexuality = $_REQUEST['sexuality'];
$build = $_REQUEST['build'];
$cities = $_REQUEST['cities'];
$membertype = $_REQUEST['membertype'];
$agegroup = $_REQUEST['agegroup'];
$lookingfor = $_REQUEST['lookingfor'];
$activities = $_REQUEST['activities'];

$whereClause = "";
$whereClause .= ".((isset($gender)) ? ((isset($whereClause)) ? " OR " : " WHERE " )."( field_id=2 and value='$gender') " : "")." ;
$whereClause .= ".((isset($sexuality)) ? ((isset($whereClause)) ? " OR " : " WHERE " )."(field_id=20 and value='$sexuality')" : "")." ;
$whereClause .= ".((isset($height)) ? ((isset($whereClause)) ? " OR " : " WHERE " )."(field_id=18 and value='$height') " : "")." ;
$whereClause .= ".((isset($build)) ? ((isset($whereClause)) ? " OR " : " WHERE " )."(field_id=19 and value='$build') " : "")." ;
$whereClause .= ".((isset($cities)) ? ((isset($whereClause)) ? " OR " : " WHERE " )."(field_id=10 and value='$cities') " : "")." ;
$whereClause .= ".((isset($membertype)) ((isset($whereClause)) ? " OR " : " WHERE " )."(field_id=22 and value='$membertype') " : "")." ;
$whereClause .= ".((isset($lookingfor)) ((isset($whereClause)) ? " OR " : " WHERE " )."(field_id=21 and value='$lookingfor') " : "")." ;
$whereClause .= ".((isset($activities)) ((isset($whereClause)) ? " OR " : " WHERE " )."(value = '$activities') " : "")." ;

$sql = "SELECT DISTINCT * , GROUP_CONCAT( DISTINCT value )
FROM jos_users a
JOIN jos_community_users b ON a.id = b.userid
JOIN jos_community_fields_values c ON b.userid = c.user_id
$whereClause
GROUP BY a.id  DESC ";

 

All the best

 

Keith

Hi

 

Sorry, load more typos:-

 

$gender = $_REQUEST['gender'];
$height = $_REQUEST['height'];
$sexuality = $_REQUEST['sexuality'];
$build = $_REQUEST['build'];
$cities = $_REQUEST['cities'];
$membertype = $_REQUEST['membertype'];
$agegroup = $_REQUEST['agegroup'];
$lookingfor = $_REQUEST['lookingfor'];
$activities = $_REQUEST['activities'];

$whereClause = "";
$whereClause .= ((empty($gender)) ? ((empty($whereClause)) ? " OR " : " WHERE " )."( field_id=2 and value='$gender') " : "");
$whereClause .= ((empty($sexuality)) ? ((empty($whereClause)) ? " OR " : " WHERE " )."(field_id=20 and value='$sexuality')" : "");
$whereClause .= ((empty($height)) ? ((empty($whereClause)) ? " OR " : " WHERE " )."(field_id=18 and value='$height') " : "") ;
$whereClause .= ((empty($build)) ? ((empty($whereClause)) ? " OR " : " WHERE " )."(field_id=19 and value='$build') " : "") ;
$whereClause .= ((empty($cities)) ? ((empty($whereClause)) ? " OR " : " WHERE " )."(field_id=10 and value='$cities') " : "") ;
$whereClause .= ((empty($membertype)) ((empty($whereClause)) ? " OR " : " WHERE " )."(field_id=22 and value='$membertype') " : "") ;
$whereClause .= ((empty($lookingfor)) ((empty($whereClause)) ? " OR " : " WHERE " )."(field_id=21 and value='$lookingfor') " : "") ;
$whereClause .= ((empty($activities)) ((empty($whereClause)) ? " OR " : " WHERE " )."(value = '$activities') " : "");

$sql = "SELECT DISTINCT * , GROUP_CONCAT( DISTINCT value )
FROM jos_users a
JOIN jos_community_users b ON a.id = b.userid
JOIN jos_community_fields_values c ON b.userid = c.user_id
$whereClause
GROUP BY a.id  DESC ";

 

All the best

 

Keith

If your going to build your query may I suggest this way!

<?php
$SQL = "SELECT *
FROM jos_users a
LEFT JOIN jos_community_users b ON a.id = b.userid
LEFT JOIN jos_community_fields_values c ON b.userid = c.user_id
WHERE ";
$SQL .= (empty($gender))?"":sprintf(" (c.field_id=2 and c.`value`='%s') OR",mysql_real_escape_string($gender));
$SQL .= (empty($cities))?"":sprintf(" (c.field_id=10 and c.`value`='%s') OR",mysql_real_escape_string($cities));
$SQL .= (empty($height))?"":sprintf(" (c.field_id=18 and c.`value`='%s') OR",mysql_real_escape_string($height));
$SQL .= (empty($build))?"":sprintf(" (c.field_id=19 and c.`value`='%s') OR",mysql_real_escape_string($build));
$SQL .= (empty($sexuality))?"":sprintf(" (c.field_id=20 and c.`value`='%s') OR",mysql_real_escape_string($sexuality));
$SQL .= (empty($lookingfor))?"":sprintf(" (c.field_id=21 and c.`value`='%s') OR",mysql_real_escape_string($lookingfor));
$SQL .= (empty($membertype))?"":sprintf(" (c.field_id=22 and c.`value`='%s') OR",mysql_real_escape_string($membertype));
$SQL .= (empty($activities))?"":sprintf(" (c.`value`='%s') OR",mysql_real_escape_string($activities));
$SQL = substr($SQL,0,-2);
$SQL .= "GROUP BY a.id  DESC "
?>

 

 

hey

 

thanks for that suggestion but this does not work either, its not returning any results.

 

i am using this form

 

<form action="http://www.site.com/includes/search/searchresults.php" method="post">
  <table width="900" border="0" cellspacing="6" cellpadding="0">
    <tr>
      <td><strong>Gender:</strong></td>
      <td><? $query="select options from jos_community_fields where id=2";
$result=mysql_query($query);

// start the select box
echo "<select name='gender' style='width:180px;'><option selected value=\"\"></option>\n";
while($row=mysql_fetch_assoc($result)){
  echo "<option value=\"$row[options]\" >$row[options]</option>\n";
}
echo "</select>"; 
?></td>
      <td rowspan="7" valign="top"><strong>What they are into:</strong>
          <table width="100%" border="0" cellspacing="1" cellpadding="0">
            <tr>
              <td>Straight sex</td>
              <td><? $query="select options from jos_community_fields where id=17";
$result=mysql_query($query);

// start the select box

while($row=mysql_fetch_row($result)){
  echo "<input type=\"checkbox\" value=\"$row[options]\" >$row[options]</option>\n";
}

?></td>
              <td>ANY</td>
              <td> </td>
            </tr>
        </table></td>
    </tr>
    <tr>
      <td><strong>Height:</strong></td>
      <td><? $query2="select options from jos_community_fields where id=18";
$result=mysql_query($query2);

// start the select box
echo "<select name='height' style='width:180px;'><option selected value=\"pleaseselect\">Please Select</option>\n";
while($row=mysql_fetch_assoc($result)){
  echo "<option value=\"$row[options]\">$row[options]</option>\n";
}
echo "</select>"; 
?></td>
    </tr>
    <tr>
      <td><strong>Sexuality:</strong></td>
      <td><? $query4="select options from jos_community_fields where id=20";
$result=mysql_query($query4);

// start the select box
echo "<select name='sexuality' style='width:180px;'><option selected value=\"pleaseselect\">Please Select</option>\n";
while($row=mysql_fetch_assoc($result)){
  echo "<option value=\"$row[options]\">$row[options]</option>\n";
}
echo "</select>"; 
?></td>
    </tr>
    <tr>
      <td><strong>Build:</strong></td>
      <td><? $query6="select options from jos_community_fields where id=19";
$result=mysql_query($query6);

// start the select box
echo "<select name='build' style='width:180px;'><option selected value=\"pleaseselect\">Please Select</option>\n";
while($row=mysql_fetch_assoc($result)){
  echo "<option value=\"$row[options]\">$row[options]</option>\n";
}
echo "</select>"; 
?></td>
    </tr>
    <tr>
      <td><strong>City:</strong></td>
      <td><? $query8="select options from jos_community_fields where id=10";
$result=mysql_query($query8);

// start the select box
echo "<select name='cities' style='width:180px;'><option selected value=\"pleaseselect\">Please Select</option>\n";
while($row=mysql_fetch_assoc($result)){
  echo "<option value=\"$row[options]\">$row[options]</option>\n";
}
echo "</select>"; 
?></td>
    </tr>
    <tr>
      <td><strong>Membership:</strong></td>
      <td><? $query1="select options from jos_community_fields where id=22";
$result=mysql_query($query1);

// start the select box
echo "<select name='membertype' style='width:180px;'><option selected value=\"pleaseselect\">Please Select</option>\n";
while($row=mysql_fetch_assoc($result)){
  echo "<option value=\"$row[options]\">$row[options]</option>\n";
}
echo "</select>"; 
?></td>
    </tr>
    <tr>
      <td><strong>Age Group:</strong></td>
      <td><? $query3="select options from jos_community_fields where id=23";
$result=mysql_query($query3);

// start the select box
echo "<select name='agegroup' style='width:180px;'><option selected value=\"pleaseselect\">Please Select</option>\n";
while($row=mysql_fetch_assoc($result)){
  echo "<option value=\"$row[options]\">$row[options]</option>\n";
}
echo "</select>"; 
?></td>
    </tr>
    <tr>
      <td><strong> Looking for:</strong></td>
      <td><strong>
        <? $query5="select options from jos_community_fields where id=21";
$result=mysql_query($query5);

// start the select box
echo "<select name='lookingfor' style='width:180px;'><option selected value=\"pleaseselect\">Please Select</option>\n";
while($row=mysql_fetch_assoc($result)){
  echo "<option value=\"$row[options]\">$row[options]</option>\n";
}
echo "</select>"; 
?>
      </strong></td>
      <td><label>
          <div align="right">
            <input type="submit" name="button" id="button" value="Submit">
          </div>
        </label></td>
    </tr>
  </table>
</form>

 

now using this search results page

 <?


$gender = $_REQUEST['gender'];
$height = $_REQUEST['height'];
$sexuality = $_REQUEST['sexuality'];
$build = $_REQUEST['build'];
$cities = $_REQUEST['cities'];
$membertype = $_REQUEST['membertype'];
$agegroup = $_REQUEST['agegroup'];
$lookingfor = $_REQUEST['lookingfor'];
$activities = $_REQUEST['activities'];

$query = "SELECT *
FROM jos_users a
LEFT JOIN jos_community_users b ON a.id = b.userid
LEFT JOIN jos_community_fields_values c ON b.userid = c.user_id
WHERE ";
$query .= (empty($gender))?"":sprintf(" (c.field_id=2 and c.`value`='%s') OR",mysql_real_escape_string($gender));
$query .= (empty($cities))?"":sprintf(" (c.field_id=10 and c.`value`='%s') OR",mysql_real_escape_string($cities));
$query .= (empty($height))?"":sprintf(" (c.field_id=18 and c.`value`='%s') OR",mysql_real_escape_string($height));
$query .= (empty($build))?"":sprintf(" (c.field_id=19 and c.`value`='%s') OR",mysql_real_escape_string($build));
$query .= (empty($sexuality))?"":sprintf(" (c.field_id=20 and c.`value`='%s') OR",mysql_real_escape_string($sexuality));
$query .= (empty($lookingfor))?"":sprintf(" (c.field_id=21 and c.`value`='%s') OR",mysql_real_escape_string($lookingfor));
$query .= (empty($membertype))?"":sprintf(" (c.field_id=22 and c.`value`='%s') OR",mysql_real_escape_string($membertype));
$query .= (empty($activities))?"":sprintf(" (c.`value`='%s') OR",mysql_real_escape_string($activities));
$query = substr($SQL,0,-2);
$query .= "GROUP BY a.id  DESC ";
$result = mysql_query($query);



if ($row = mysql_fetch_array($result)) {


              $i = 1;

              do { // This loop outputs each result.

                     print"
				 <table width=\"80%\" border=\"0\" cellspacing=\"0\" cellpadding=\"6\" style=\"border: thin solid #CCCCCC; font-family:arial, sans-serif;\">
  <tr>
    <td colspan=\"3\" style=\"font-size:10px; color:#CC33CC; border-bottom:thin solid #CCCCCC; background:#eaeaea;\">RESULT NO:{$i}. | USER NO. ".$row['userid']."</td>
  </tr>
  <tr>
    <td width=\"18%\" rowspan=\"3\"><img src=\"http://www.maskerade-swingers.com/".$row['avatar']."\" width=\"100px\" height=\"100px\"></td>
    <td height=\"15px\" width=\"82%\" colspan=\"2\" style=\"border-bottom:thin solid #CCCCCC; color:#cc33cc; font-size:13px;\">".$row['username']."
				 <font color=\"#CCCCCC\" style=\"font-size:11px;\">".$row['status']."</font></td>
  </tr>
  <tr>
    <td colspan=\"2\"></td>
  </tr>
  <tr>
    <td height=\"15px\" style=\"font-size:10px; color:#CC33CC; border-top:thin solid #CCCCCC; background:#eaeaea;\"><div align=\"left\">
				  Friends: ".$row['friendcount']." | Matching Details: ".$row['GROUP_CONCAT( DISTINCT value )']."</div></td>
    <td style=\"font-size:10px; color:#CC33CC; border-top:thin solid #CCCCCC; background:#eaeaea;\"><div align=\"right\"> <a href=\"http://www.maskerade-swingers.com/index.php?option=com_community&view=profile&userid=".$row['userid']."\">View Member </div></td>
  </tr>
</table><br>\n"; // Here you can format the search results and display whatever you want about the result.

                     $i++;

              } while ($row = mysql_fetch_array($result));

       } else {

              print"<br />Your search returned no results.";


}?> 

 

the results are just not coming through

 

thanks

echo of the query

 

SELECT DISTINCT * , GROUP_CONCAT( DISTINCT value ) FROM jos_users a JOIN jos_community_users b ON a.id = b.userid JOIN jos_community_fields_values c ON b.userid = c.user_id AND ( field_id=2 AND value='Male') OR (field_id=20 AND value='pleaseselect') OR (field_id=18 AND value='pleaseselect') OR (field_id=19 AND value='pleaseselect') OR (field_id=10 AND value='pleaseselect') OR (field_id=22 AND value='pleaseselect') OR (field_id=23 AND value='pleaseselect') OR (field_id=21 AND value='pleaseselect') OR (field_id=17 AND value = '') GROUP BY a.id ORDER BY GROUP_CONCAT( DISTINCT value ) DESC

 

have reverted back to my original as this is the closes i am coming to it, the results are coming up but all the search areas are coming up in most relevant option rather than the users with those specific qualities.

Hi

 

Problem with your original solution is the precedence of AND and OR.

 

If you want to do it that way, at the VERY least you need some extra brackets:-

 

SELECT DISTINCT * , GROUP_CONCAT( DISTINCT value ) FROM jos_users a JOIN jos_community_users b ON a.id = b.userid JOIN jos_community_fields_values c ON b.userid = c.user_id AND (( field_id=2 AND value='Male') OR (field_id=20 AND value='pleaseselect') OR (field_id=18 AND value='pleaseselect') OR (field_id=19 AND value='pleaseselect') OR (field_id=10 AND value='pleaseselect') OR (field_id=22 AND value='pleaseselect') OR (field_id=23 AND value='pleaseselect') OR (field_id=21 AND value='pleaseselect') OR (field_id=17 AND value = '')) GROUP BY a.id ORDER BY GROUP_CONCAT( DISTINCT value ) DESC

 

Without those the JOIN is likely to bring back very strange results (basically the JOIN condition on userid / user_id would be ignored most of the time). This is one reason to move the checks against the values into a WHERE clause.

 

All the best

 

Keith

Humm kinda confused as your echo doesn't match the posted code!

 

<?php
$query = "SELECT *
FROM jos_users a
LEFT JOIN jos_community_users b ON a.id = b.userid
LEFT JOIN jos_community_fields_values c ON b.userid = c.user_id
";
$where = "";
$where .= (empty($gender))?"":sprintf(" (c.field_id=2 and c.`value`='%s') OR",mysql_real_escape_string($gender));
$where .= (empty($cities))?"":sprintf(" (c.field_id=10 and c.`value`='%s') OR",mysql_real_escape_string($cities));
$where .= (empty($height))?"":sprintf(" (c.field_id=18 and c.`value`='%s') OR",mysql_real_escape_string($height));
$where .= (empty($build))?"":sprintf(" (c.field_id=19 and c.`value`='%s') OR",mysql_real_escape_string($build));
$where .= (empty($sexuality))?"":sprintf(" (c.field_id=20 and c.`value`='%s') OR",mysql_real_escape_string($sexuality));
$where .= (empty($lookingfor))?"":sprintf(" (c.field_id=21 and c.`value`='%s') OR",mysql_real_escape_string($lookingfor));
$where .= (empty($membertype))?"":sprintf(" (c.field_id=22 and c.`value`='%s') OR",mysql_real_escape_string($membertype));
$where .= (empty($activities))?"":sprintf(" (c.`value`='%s') OR",mysql_real_escape_string($activities));
$query .= (!empty($where))?"WHERE ".substr($where,0,-2):"";
$query .= "GROUP BY a.id  DESC ";
$result = mysql_query($query) or die($query.mysql_error());
?>

 

 

 

have reverted back to my original as this is the closes i am coming to it, the results are coming up but all the search areas are coming up in most relevant option rather than the users with those specific qualities.

 

hey reverted back as it was the only thing coming close. the suggestions were not returning any results at all

 

 

hey reverted back as it was the only thing coming close. the suggestions were not returning any results at all

 

That is why I asked about the SQL it generated. If we can see that then we stand a better chance of identifying the problem.

 

Your original has at least an issue with the brackets.

 

All the best

 

Keith

ah sorry i get you

 

SELECT * FROM jos_users a LEFT JOIN jos_community_users b ON a.id = b.userid LEFT JOIN jos_community_fields_values c ON b.userid = c.user_id WHERE (c.field_id=2 and c.`value`='Male') OR (c.field_id=10 and c.`value`='pleaseselect') OR (c.field_id=18 and c.`value`='5ft-5ft5') OR (c.field_id=19 and c.`value`='Athletic') OR (c.field_id=20 and c.`value`='pleaseselect') OR (c.field_id=21 and c.`value`='pleaseselect') OR (c.field_id=22 and c.`value`='pleaseselect') GROUP BY a.id DESC

 

its showing results but not defining them by the selected criteria

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.