我有一个应用程序,可以根据某些条件(州、城市、价格、车库等)从 MySQL 数据库中获取公寓
我正在阅读有关 PHP 脚本中的 range、BETWEEN 和数组的内容,但我找不到解决此问题的方法,因为除了从数据库中获取值之外,我还需要在 ListView 中显示公寓,前提是其价格在价格范围之间:
我的 Android 应用程序中有一个微调器,它有一定的价格范围($500.000 - $1.000.000、$1.000.000 - $2.000.000、$2.000.000 - $5.000.000),显示公寓的条件如下:
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
pDialog.dismiss();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Event event = new Event();
event.setImovel_id(obj.getString("imovel_id"));
event.setThumbnailUrl(obj.getString("arquivo"));
event.setNegocio(obj.getString("negocio"));
event.setImovel(obj.getString("imovel"));
event.setMobilia(obj.getString("mobilia"));
event.setGaragem(obj.getString("garagem"));
event.setPreco(obj.getString("preco"));
city = obj.getString("city");
statee = obj.getString("state");
checkNegocio = obj.getString("negocio");
checkImovel = obj.getString("imovel");
checkMobilia = obj.getString("mobilia");
checkGaragem = obj.getString("garagem");
checkPreco = obj.getString("preco");
if(estado.getText().toString().equalsIgnoreCase(statee) &&
cidade.getText().toString().equalsIgnoreCase(city) &&
checkN.getText().toString().equalsIgnoreCase(checkNegocio) &&
checkI.getText().toString().equalsIgnoreCase(checkImovel) &&
checkM.getText().toString().equalsIgnoreCase(checkMobilia) &&
checkG.getText().toString().equalsIgnoreCase(checkGaragem) &&
checkP.getText().toString().equalsIgnoreCase(checkPreco)){
// adding event to events array
eventList.add(event);
}
} catch (JSONException e) {
e.printStackTrace();
}
} if(eventList.size() > 0){
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);
}else{
noEvent.setText("No results");
}
adapter.notifyDataSetChanged();
}
}
这是 json 编码的 PHP 文件
<?php
$link_list = array();
$link = array();
include_once 'db_connect.php';
$sql = "SELECT * FROM iMoveis";
$result = mysqli_query($mysqli, $sql);
while($row = mysqli_fetch_array($result)){
$link['imovel_id'] = $row['imovel_id'];
$link['arquivo'] = $row['arquivo'];
$link['state'] = $row['state'];
$link['city'] = $row['city'];
$link['negocio'] = $row['negocio'];
$link['imovel'] = $row['imovel'];
$link['mobilia'] = $row['mobilia'];
$link['garagem'] = $row['garagem'];
$link['preco'] = $row['preco'];
array_push($link_list, $link);
}
echo json_encode($link_list, JSON_UNESCAPED_SLASHES);
?>
$row['preco'] 从数据库中获取公寓的价格。
最好的方法是什么?有没有办法在我的java类中做到这一点?我认为这会更容易。提前致谢。
编辑: 我需要展示位于微调器给出的平均价格范围内的公寓。我在数据库中将公寓的价格作为单个值(例如 1.200.000 美元)。但微调器对每个要选择的项目都有一个价格范围:
- 第一 = 1,000,000 美元到 2,000,000 美元
- 第二 = $2.000.000 到 $5.000.000 等等..
我需要的是仅当数据库值在微调器的平均值内时才在 ListView 上显示公寓。 Spinner 发送的是 $1.000.000 到 $2.000.000,而 json 仅打印 $1.200.000。所以它不匹配并且不会在 ListView 上向我显示任何内容。应该显示它,因为 $1.200.000 在 $1.000.000 到 $2.000.000 之间。抱歉,我无法发送打印屏幕以便您更好地查看
最佳答案
我自己通过更改微调器选项解决了这个问题。设置新的旋转器的最小值和最大值,如果价格在这些值之间,我会显示公寓。
关于php - 如何在 Android + PHP json 请求中处理一系列价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33274864/