gpt4 book ai didi

php - 如何获得更多的变量值?

转载 作者:行者123 更新时间:2023-11-30 03:04:35 25 4
gpt4 key购买 nike

很久以前有一个在android中用json解析数据的例子。

.java

public static final String TAG_SUCCESS = "success";
public static final String TAG_DAFTAR_RS = "daftar_rs";
public static final String TAG_ID_RS = "id_rs";
public static final String TAG_NAMA_RS = "nama_rs";
public static final String TAG_LINK_IMAGE_RS = "link_image_rs";
public static final String TAG_ALAMAT_RS = "alamat_rs";
public static final String TAG_TELEPON_RS = "telepon_rs";

.php

$result = mysql_query("SELECT id, nama, link_image, alamat, telepon FROM tbl_rumah_sakit ORDER BY nama ASC  ") or die(mysql_error());

if (mysql_num_rows($result) > 0) {

$response["daftar_rs"] = array();

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

$daftar_rs = array();
$daftar_rs["id_rs"] = $row["id"];
$daftar_rs["nama_rs"] = stripslashes($row["nama"]);
$daftar_rs["link_image_rs"] = stripslashes($row["link_image"]);
$daftar_rs["alamat_rs"] = stripslashes($row["alamat"]);
$daftar_rs["telepon_rs"] = stripslashes($row["telepon"]);
array_push($response["daftar_rs"], $daftar_rs);
}

$response["success"] = 1;

echo json_encode($response);
} else {

$response["success"] = 0;
$response["message"] = "error";

echo json_encode($response);
}

效果很好,每个人都很开心。但是有一天,一个像霍比特人一样的男孩决定改变这个例子,因为他有自己的任务。当然,他摧毁了一切。他做了什么?他做到了

    public static final String TAG_SUCCESS = "success";
public static final String TAG_LIST = "list_k";
public static final String TAG_ID = "id_k";
public static String TAG_TEXT_NAME;
public static String TAG_LINK_IMAGE;

TAG_TEXT_NAME 和 TAG_LINK_IMAGE 的值他通过按钮 putExtra 从另一个 Activity 中获得。变量可以有值“text1”、“text2”、“text3”和“image1”、“image2”、“image3”,这取决于单击按钮 1、按钮 2 或按钮 3。所以他必须改变和 php 脚本,他也改变了它。

$result = mysql_query("SELECT * FROM tbl_rumah_sakit ORDER BY id ASC  ") or die(mysql_error());

if (mysql_num_rows($result) > 0) {


$response["list_k"] = array();

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

$list_k = array();
$list_k["id_k"] = $row["id"];
$list_k["text1"] = stripslashes($row["text1"]);
$list_k["image1"] = stripslashes($row["image1"]);

$list_k["text2"] = stripslashes($row["text2"]);
$list_k["image2"] = stripslashes($row["image2"]);

$list_k["text3"] = stripslashes($row["text3"]);
$list_k["image3"] = stripslashes($row["image3"]);
array_push($response["list_k"], $list_k);
}

$response["success"] = 1;

echo json_encode($response);
} else {

$response["success"] = 0;
$response["message"] = "error";

echo json_encode($response);
}

他希望它能工作,但他遇到了麻烦。它只对 text1 和 image1 有效。他很不高兴。但他没有放下手。他是这样修改的

switch(true){
case "text1":
$list_k = array();
$list_k["id_k"] = $row["id"];
$list_k["text1"] = stripslashes($row["text1"]);
$list_k["image1"] = stripslashes($row["image1"]);
array_push($response["list_k"], $list_k);
break;
case "text2":
$list_k = array();
$list_k["id_k"] = $row["id"];
$list_k["text2"] = stripslashes($row["text2"]);
$list_k["image2"] = stripslashes($row["image2"]);
array_push($response["list_k"], $list_k);
break;
case "text3":
$list_k = array();
$list_k["id_k"] = $row["id"];
$list_k["text3"] = stripslashes($row["text3"]);
$list_k["image3"] = stripslashes($row["image3"]);
array_push($response["list_k"], $list_k);
break;
default: echo"You didn't give me a value how I can work? Please, give me text1 or text2 or text3";
}

但它仅适用于第一个值 (text1)。他的失望很深。但他没有放弃。他将他的 php 脚本更改为此

if('text1'){   
$list_k = array();
$list_k["id_k"] = $row["id"];
$list_k["text1"] = stripslashes($row["text1"]);
$list_k["image1"] = stripslashes($row["image1"]);
array_push($response["list_k"], $list_k);
}else if('text2'){
$list_k = array();
$list_k["id_k"] = $row["id"];
$list_k["text2"] = stripslashes($row["text2"]);
$list_k["image2"] = stripslashes($row["image2"]);
array_push($response["list_k"], $list_k);
}else if("text3"){
$list_k = array();
$list_k["id_k"] = $row["id"];
$list_k["text3"] = stripslashes($row["text3"]);
$list_k["image3"] = stripslashes($row["image3"]);
array_push($response["list_k"], $list_k);
}

但是他的应用程序一次又一次地崩溃。请,如果你看到这篇文章,如果你能提供帮助,请不要忽视他。他是个非常光荣的男孩。日志

02-25 08:55:24.920: W/System.err(2822):     org.json.JSONException: No value for text2(text3 give the same logcat error)
02-25 08:55:24.930: W/System.err(2822): at org.json.JSONObject.get(JSONObject.java:354)
02-25 08:55:24.930: W/System.err(2822): at org.json.JSONObject.getString(JSONObject.java:510)
02-25 08:55:24.930: W/System.err(2822): at com.example.testtaskmyapp.MainActivity$Activity.doInBackground(MainActivity.java:158)
02-25 08:55:24.930: W/System.err(2822): at com.example.testtaskmyapp.MainActivity$Activity.doInBackground(MainActivity.java:1)
02-25 08:55:24.930: W/System.err(2822): at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-25 08:55:24.930: W/System.err(2822): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
02-25 08:55:24.930: W/System.err(2822): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
02-25 08:55:24.940: W/System.err(2822): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
02-25 08:55:24.940: W/System.err(2822): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
02-25 08:55:24.940: W/System.err(2822): at java.lang.Thread.run(Thread.java:1019)

最佳答案

请检查你的第一个变体,检查,它必须工作(不是最优的但它必须工作)

while ($row = mysql_fetch_array($result)) {
$list_k = array();
$list_k["id_k"] = $row["id"];
$list_k["text1"] = stripslashes($row["text1"]);
$list_k["image1"] = stripslashes($row["image1"]);

$list_k["text2"] = stripslashes($row["text2"]);
$list_k["image2"] = stripslashes($row["image2"]);

$list_k["text3"] = stripslashes($row["text3"]);
$list_k["image3"] = stripslashes($row["image3"]);
array_push($response["list_k"], $list_k);
}

关于php - 如何获得更多的变量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22008216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com