发出数据库查询结果

时间:2013-11-25 03:19:03

标签: php mysql sql

我们有一个2字段搜索表单。第一个字段是案例选择框,值在字段框中填充。我们检查了元素,可以在选择框中看到字段值作为实际名称。我们的第二个框是与城市/州相关联的文本框。这个想法是第一个选择框包含类别,用户将选择一个类别并输入他们的城市/州,并且能够在数据库中搜索与所选类别和输入的城市/州匹配的任何结果。截至目前,结果正在搜索城市正确但是类别要么没有正确查询,要么没有被正确过滤,因为我们得到多个结果,其中包括未从搜索结果中的选择框中选择的类别。

以下是选择框的代码:

function gmw_fl_fields_dropdown($gmw, $id, $class) {

$total_fields = ( isset($gmw['profile_fields']) ) ? $gmw['profile_fields'] : array();

if( isset($gmw['profile_fields_date']) ) array_unshift($total_fields, $gmw['profile_fields_date']);

echo '<div id="'.$id.'" class="'.$class.'">';

foreach ( $total_fields as $field_id ) {

    $field_data = new BP_XProfile_Field ($field_id);
    $fieldName = explode(' ', $field_data->name);
    $fieldName = preg_replace("/[^A-Za-z0-9]/","",$fieldName[0]);

    $get_field = (isset($_GET[$fieldName . '_' . $field_id])) ? $_GET[$fieldName . '_' . $field_id] : '';
    $get_field_to = (isset($_GET[$fieldName . '_' . $field_id. '_to'])) ? $_GET[$fieldName . '_' . $field_id. '_to'] : '';

    $children = $field_data->get_children ();

    switch ($field_data->type) {

        case 'datebox':
            echo '<div class="editfield field_'.$field_id.' datebox">';
            echo '<span class="label">Age Range (min - max)</span>';
            echo '<input size="3" type="text" name="'. $fieldName . '_' . $field_id.'" value="' . $get_field . '" placeholder="Min" style="width:10%" />';
            echo '&nbsp;-&nbsp;';
            echo '<input size="3" type="text" name="'. $fieldName . '_' . $field_id . '_to" value="' . $get_field_to . '" placeholder="Max" style="width:10%" />';
            echo '</div>';
            break;

        case 'multiselectbox':
        case 'selectbox':
            case 'selectbox':
            echo '<div class="editfield_'.$field_id.' selectbox">';
            echo '<select>';
            foreach ($children as $child) {
                $child->name = trim ($child->name);
                $selected = ($child->name == $get_field )? "selectd='selected'": "";
                echo '<option type="selectbox"  name="'. $fieldName . '_' . $field_id. '[]"  value="'.$child->name.'">'.$child->name.'</option>';
            }
            echo '</select>';
            echo '</div>';
            break;

以下是查询功能的代码:

function gmw_fl_query_fields($gmw) {
global $bp, $wpdb;
$total_fields = false;

$total_fields = ( isset( $gmw['profile_fields'] ) ) ? $gmw['profile_fields'] : array();



if( isset( $gmw['profile_fields_date'] ) && !empty( $gmw['profile_fields_date'] ) ) array_unshift( $total_fields, $gmw['profile_fields_date'] );

if ( !isset($total_fields) || empty($total_fields) ) return;

$empty_fields = array();
$userids = false;

foreach ($total_fields as $field_id) {
    $field_data = new BP_XProfile_Field ($field_id);
    $fieldName = explode(' ', $field_data->name);
    $fieldName = preg_replace("/[^A-Za-z0-9]/","",$fieldName[0]);
    $children = $field_data->get_children ();

    $value = ( isset($_GET[$fieldName . '_'. $field_id]) ) ? $_GET[$fieldName . '_'. $field_id] : '';
    $to = ( isset($_GET[$fieldName . '_' . $field_id. '_to']) ) ?  $_GET[$fieldName . '_' . $field_id. '_to'] : '';

    if ($value) array_push($empty_fields, $value);

    if( $value || $to || $children ) {

        switch ($field_data->type) {

            case 'selectbox':

                $sql = "SELECT user_id from {$bp->profile->table_name_data}";
                $sql .= " WHERE field_id = $field_id ";

            break;

            case 'radio':

                $sql = "SELECT user_id from {$bp->profile->table_name_data}";
                $sql .= " WHERE field_id = $field_id ";
                $like = array();

                foreach ($value as $curvalue)
                    $like[] = "value = '$curvalue' OR value LIKE '%\"$curvalue\"%' ";
                $sql .= ' AND ('. implode (' OR ', $like). ')';
            break;
        }//switch //

    $results = $wpdb->get_col($sql, 0);

        if ( !is_array($userids) ) $userids = $results; else $userids = array_intersect($userids, $results);

    } // if value //
} // for eaech //

/* build SQL filter from profile fields results - member ids array */
if ( isset($userids) && !empty($userids) ) return $userids;
/* if no results and profile fields are not empty - buba is going to stop the function */
else if ( !empty($empty_fields) ) return "buba";

}

踢球者是这个...案例单选按钮的查询功能就像一个魅力!但是我们在下拉选择框中有30多个类别,它比搜索表单中的30多个单选按钮更清晰。

任何帮助或想法都会受到高度赞赏,因为我们无法确定正确的陈述需要让案例选择框正确查询。

0 个答案:

没有答案