php - 如何在一个变量中存储多行,将其用作 android studio 中的 JSONArray
问题描述
我正在尝试在我的地图应用程序上标记一些点,这些点的坐标存储在数据库中。我已经设法解决了 java 的问题JSONArray
,但我现在的问题是PHP
从数据库发送的只有一行,而不是全部作为数组发送。我试图将它们存储在一个变量中,尝试实现implode
,但仍然无法将所有行发送到应用程序。在应用程序上,JSONArray 应该从location
.
.php 代码仅发送 1 行:
$problema = $_POST['problema'];
$stmt = $conn->prepare("SELECT latitudine, longitudine, tip_problema FROM alerte WHERE tip_problema = ?");
$stmt->bind_param("s", $problema);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows > 0){
$stmt->bind_result($latitudine, $longitudine, $tip_problema);
$stmt->fetch();
$locatie = array(
'latitudine'=>$latitudine,
'longitudine'=>$longitudine,
'tip_problema'=>$tip_problema
);
$response['error'] = false;
$response['message'] = 'Alerta raportata';
$response['locatie'] = $locatie;
}else{
$response['error'] = true;
$response['message'] = 'Eroare de identificare a alertelor';
}
从逻辑上讲,应该有某种while
可以逐行进行的,但是由于我是初学者,PHP
所以我无法自己实现。搜索了一些方法,但对我没有任何帮助。谢谢!
JSON 代码:
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//hiding the progressbar after completion
try {
//converting response to json object
JSONObject obj = new JSONObject(s);
//if no error in response
if (!obj.getBoolean("error")) {
Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
JSONArray locatieArray = obj.getJSONArray("locatie");
for (int i = 0; i < locatieArray.length(); i++) {
JSONObject locatie = locatieArray.getJSONObject(i);
// check latitudine and longitudine is not null and if not null then cast these values and call the addMarker() method.
if(!locatie.isNull("latitudine") && !locatie.isNull("longitudine")) {
latitudine_sql =Double.valueOf(locatie.getString("latitudine"));
longitudine_sql = Double.valueOf(locatie.getString("longitudine"));
addMarker(latitudine_sql, longitudine_sql); // this method is implemented below
}
tip_problema_sql = locatie.getString("tip_problema");
}
} else {
Toast.makeText(getApplicationContext(), "Some error occurred", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
解决方案
您必须使用循环遍历结果while
并创建$response
一个像这样的多维数组:
$problema = $_POST['problema'];
$stmt = $conn->prepare("SELECT latitudine, longitudine, tip_problema FROM alerte WHERE tip_problema = ?");
$stmt->bind_param("s", $problema);
$stmt->execute();
$stmt->store_result();
$response = array();
$x = 0;
if($stmt->num_rows > 0){
$stmt->bind_result($latitudine, $longitudine, $tip_problema);
while($stmt->fetch()) {
$x++;
$locatie = array(
'latitudine'=>$latitudine,
'longitudine'=>$longitudine,
'tip_problema'=>$tip_problema
);
$response[$x]['error'] = false;
$response[$x]['message'] = 'Alerta raportata';
$response[$x]['locatie'] = $locatie;
}
}else{
$response['error'] = true;
$response['message'] = 'Eroare de identificare a alertelor';
}
请注意,如果结果为 0,$response
则将是一维数组。
while
您可以在官方文档中了解更多信息。
推荐阅读
- ios - 如何更改结构数组中的数据?
- ios - Swift5 ImagePicker Only Video 从 iPhone 照片库输入到 collectionView Cell
- python - 使用python的beautifulSoup库从IMDB网站上抓取电影评论
- django - 来自 django 视图集中请求的额外字段
- angular - SAP Spartacus,@ngrx/store:状态中不存在功能名称“stores”,因此 createFeatureSelector 无法访问它
- rpgle - 如何在没有事先输入的情况下遇到错误 UPDATE 和 DELETE WHEN 想要更新到 RPGLE 中的 SUBFILE
- python - 为什么这段代码运行正确?(python 全局关键字)
- c# - 如何将具有不同配置的第二个 eventthub 客户端注入我的 Azure 函数?
- python - 如何获取重定向的 URL?
- connection - Aws 与 Teradata 的胶水连接