Reputation: 5557
I'm passing a string param
in the following format 7//3,6
and so on...so it represents a list of events seperated with // and each event has individual items seperated with a comma
my Handler first breaks this into individual arrays containing 7
and 3,6
and then again into individual arrays containing each of the single digits. These digits are passed to my Compare class and a response of the following format is returned:
{"ress":[{"res":[{"ids":"7","ans":null}]},{"res":[{"ids":"3","ans":null},{"ids":"6","ans":"2"}]}
My problem is that after I receive this result, I have trouble identyfying which res
corresponds to which event in the initial string that I pass. To solve this I would like to pass a unique id which I have already generated in the string, and obtain that id as part of the result. So for example I'd like to pass something like this.
(23ddasc)7//(dsefwifw43)3,6
where the string in brackets is the unique ID (it doesn't have to be in this specific format, I'm just using it to illustrate the problem.) and then in the result I'd like to obtain something of this sort so that each event can be uniquely identified.
{"ress":[{"res": "uniqueid": "23ddasc" [{"ids":"7","ans":null}]},{"res": "uniqueid": "23ddasc" [{"ids":"3","ans":null},{"ids":"6","ans":"2"}]}
( I know I haven't formatted the JSON properly here, I just inserted the uniqueid parameter for illustrative purposes). My question is what alterations should I make in my php code to be able to do this, I've tried adding response[$uniqueid]
and making my php code extract each of the ids from the brackets and store them in an array but it didn't work at all. Thanks for your help :)
Handler.php
<?php
// get tag
$param= $_POST['param'];
$event = explode("//",$param);
$len = count($event);
$ultresponse["ress"] = array();
for ($x = 0; $x < $len; $x++) {
$ids = explode(",",$event[$x]);
$lens = count($ids);
require_once 'Compare.php';
$db = new Compare();
$response["res"] = array();
for($y = 0; $y < $lens; $y++) {
$result= $db->compimp($ids[$y]);
array_push($response["res"], $result);
}
array_push($ultresponse["ress"], $response);
}
echo json_encode($ultresponse);
?>
Compare.php
<?php
class Compare {
function __construct() {
require_once 'DB_Connect.php';
$this->db = new DB_Connect();
$this->db->connect();
}
function __destruct() {
}
public function compimp($ids) {
$conn=mysqli_connect("....");
$result = mysqli_query($conn,"SELECT ids , ans FROM mylist WHERE ids = '$ids'");
$no_of_rows = mysqli_num_rows($result);
if ($no_of_rows > 0) {
return mysqli_fetch_array($result,MYSQLI_ASSOC);
}
}
}
?>
Upvotes: 2
Views: 89
Reputation: 6862
I suggest that you change the format of your param
variable and use JSON, so you can send it back and forth, with something like this:
JavaScript:
paramObj = new Array();
data1 = new Array();
data1.push({id: 7});
ids1 = {"uniqueid": "23ddasc", "data": data1};
paramObj.push(ids1);
data2 = new Array();
data2.push({id: 6});
data2.push({id: 2});
ids2 = {"uniqueid": "dsefwifw43", "data": data2};
paramObj.push(ids2);
param = JSON.stringify(paramObj);
// send param to PHP script
You'll get this JSON:
[
{"uniqueid":"23ddasc","data":[
{"id":7}
]},
{"uniqueid":"dsefwifw43","data":[
{"id":6},
{"id":2}
]}
]
PHP:
<?php
$param = json_decode($_POST['param']);
require_once 'Compare.php';
$db = new Compare();
foreach ($param as &$event)
{
foreach($event["data"] as &$ids)
{
$result = $db->compimp($ids["id"]);
$ids["ans"] = $result["ans"];
}
unset($ids);
}
unset($event);
echo json_encode($param);
This will output:
[
{"uniqueid":"23ddasc","data":[
{"id":7,"ans":"null"}]
},
{"uniqueid":"dsefwifw43","data":[
{"id":6,"ans":"null"},
{"id":2,"ans":"2"}
]}
]
And by the way, don't forget to escape any strings you obtain from GET/POST before you send it to SQL, so you avoid any SQL injection attacks.
Upvotes: 1