使用数组

时间:2016-09-07 08:47:50

标签: php arrays json

尝试使用多个参数和字段创建搜索,包括复选框和下拉列表。将所有数据合并到一个json对象中,然后向用户显示结果,但到目前为止,我在这里遗漏了一些东西。

截至目前,我正在使用ajax和php。我首先JSON.stringify数组(例如,服务 - 用户最多可以选择24个服务),同样适用于该语言。

//append languages to formdata
var languagesArray = new Array();
$("#nannyLanguages option:selected").each(function() {
    languagesArray.push($(this).val());
});
formData.append('languagesArray', JSON.stringify(languagesArray));

//push services to array
var servicesArray = new Array();
$("input:checkbox:checked[class='servicesCheckboxes']").each(function() {
    servicesArray.push($(this).val());

});
formData.append('userServices', JSON.stringify(servicesArray));

在服务器端,我解码后期数据并获取值:

if (isset($_POST['userServices']) && !empty($_POST['userServices'])){
    $userServices = $_POST['userServices'];
    $serv_decode = json_decode($userServices);
    $services = implode(',', array_values($serv_decode));
    $result = count($userServices);

    $search = $user_home->runQuery("

    SELECT nanny_services.user_id, userFirstName, tbl_users.userLastName, tbl_users.user_id, nanny_info.description 
    FROM nanny_services 

    JOIN services on nanny_services.service_id = services.service_id 

    JOIN tbl_users ON tbl_users.user_id = nanny_services.user_id 

    LEFT JOIN nanny_info 
    ON nanny_info.user_id = tbl_users.user_id

    WHERE (services.service_id IN (:service_id)) 

    GROUP BY nanny_services.user_id HAVING COUNT(DISTINCT services.service_id) = :count

    ");
    $search->execute(array(
        ':service_id'        => $services, 
        ':count'             => $result, 

    ));
    $search_results_services = $search->fetchAll(PDO::FETCH_ASSOC);  

}
if (isset($_POST['languagesArray']) && !empty($_POST['languagesArray'])){
    $languagesArray = $_POST['languagesArray'];
    $lang_decode = json_decode($languagesArray);
    $languages = implode(',', array_values($lang_decode));
    $languagesCount = count($languagesArray);


    $searchLanguage = $user_home->runQuery("

    SELECT user_language.user_id 
    FROM user_language 

    JOIN tbl_users ON user_language.user_id = tbl_users.user_id 

    AND (user_language.user_language_id IN (:language_id))
    GROUP BY user_language.user_id HAVING COUNT(DISTINCT user_language.user_language_id) = :languagescount

    ");
    $searchLanguage->execute(array(
        ':language_id'       => $languages, 
        ':languagescount'    => $languagesCount, 

    ));
    $search_results_language = $searchLanguage->fetchAll(PDO::FETCH_ASSOC);  

}

MYSQL查询后,我将这些查询推送到数组和json_encode。

$results[] = array(
    'languages' => $search_results_language,
    'services' => $search_results_services
);


echo json_encode(array("searchData" => $results));

给了我这个:

enter image description here

无论如何,我可以将结果合并到一个对象中吗?因此,每个用户都在user_id下列出了自己的服务,我可以将用户显示为一个?

所以它会是这样的:searchData -> user_id -> user_languages AND user_services AND all the other data

修改

添加语言和服务的HTML

<div class="row">
    <div class="col-md-12">
        <label class="control-label">Services</label>
        <div class="form-group form-group-custom" id="servicesCheckboxes">
            <div class="checkbox checkbox-inline">
                <input class="servicesCheckboxes" type="checkbox" id="serviceSelectAll" />
            </div>
            <?php //services array $services=$ user_home->runQuery("SELECT * FROM services"); $services->execute(); $services_array = $services->fetchAll(PDO::FETCH_ASSOC); $i = 0; echo '
            <div class="row">'; foreach ( $services_array as $service => $service_id ) { echo '
                <div class="col-md-3">
                    <div class="checkbox checkbox-inline">
                        <input class="servicesCheckboxes" type="checkbox" id="service' . $service_id['service_id'] . '" value="' . $service_id['service_id'] . '" />
                        <label for="service' . $service_id['service_id'] . '">' . $service_id['service'] . '</label>
                    </div>
                </div>'; } echo '</div>'; ?>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-md-12">
        <label class="control-label">Languages</label>
        <div class="form-group form-group-custom">
            <select name="nannyLanguages[]" class="selectpicker" id="nannyLanguages" multiple="multiple" data-live-search="true">
                <optgroup>
                    <!-- languages -->
                    <?php $languages=$ user_home->runQuery("SELECT * FROM languages"); $languages->execute(); $languages_array = $languages->fetchAll(PDO::FETCH_ASSOC); foreach ( $languages_array as $language => $language_id ) { echo "
                    <option value='".$language_id[' user_language_id ']."'>".$language_id['languages']."</option>"; } ?>
                </optgroup>
            </select>
        </div>
    </div>
</div>

编辑2

稍微更改了我的逻辑并尝试将条件推送到数组中,如果变量已设置且不为空。

我在PHP中遇到此错误:Warning: implode(): Argument must be an array in D:\Programs\wamp64\www\Hoidja.ee\PHP\scripts\generalSearch.php on line <i>128</i >Notice: Array to string conversion in D:\Programs\wamp64\www\Hoidja.ee\PHP\scripts\generalSearch.php on line <i>128</i >

下面是代码:

   $base_query = "SELECT * FROM tbl_users";
$join = "JOIN";
$on = "ON";
$and = "AND";
$where = "WHERE";
$groupBY = "GROUP BY";

$conditions = array();

if (isset($_POST['nannyCity']) && !empty($_POST['nannyCity'])){
    $nannyCity = $_POST['nannyCity'];

    $conditions[] = "user_address ".$on." tbl_users.user_id = user_address.user_id ".$where." tbl_users.userType ='hoidja' ".$and." user_address.city = :nannyCity ".$groupBY." tbl_users.user_id";
}
//if there are conditions
if (count($conditions) > 0) {
    $base_query .= implode("JOIN" .$conditions);
}

//run the query
$search = $user_home->runQuery($base_query);

//bind parameters
if (isset($_POST['nannyCity'])){
    $search->bindParam(':nannyCity', $nannyCity, PDO::PARAM_STR);
}
//execute
$search->execute();
//fetch
$searchResults = $search->fetchAll(PDO::FETCH_ASSOC); 

echo json_encode(array("searchData" => $searchResults));

0 个答案:

没有答案