I have problems trying to send an ArrayList from Android to PHP and then Insert this ArrayList into a MySQL table.
So, first of all I send List with all the parameters.
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("tag", "registerCall"));
list.add(new BasicNameValuePair("hourMatch", hourMatch));
list.add(new BasicNameValuePair("dateMatch", dateMatch));
list.add(new BasicNameValuePair("listPlayers", seleccionados
.toString())); //ARRAYLIST<String>
list.add(new BasicNameValuePair("idMatch", idMatch));
If I print the parameters list: [tag=registerCall, hourMatch=0931, dateMatch=2013-08-07, listPlayers=[8, 10], idMatch=7].
And then in the PHP, I collect the parameters and send it to the function.
if ($tag == 'registerCall') {
$hourMatch = $_POST['hourMatch'];
$dateMatch = $_POST['dateMatch'];
$idMatch = $_POST['idMatch'];
$listPlayers = $_POST['listPlayers'];
$call = $db->registerCallMatch($dateMatch, $hourMatch, $idMatch, $listPlayers);
if ($call != false) {
$response["success"] = 1;
$response["msg"] = "Insertados";
echo json_encode($response);
} else {
$response["error"] = 1;
$response["error_msg"] = "Error en la inserción";
echo json_encode($response);
}
}
And the function is:
public function registerCallMatch($dateCall, $timeCall, $idMatch, $listPlayers)
{
$result = mysql_query("INSERT INTO call_match (date_call, time_call, idMatch) VALUES ('$dateCall', '$timeCall', '$idMatch')");
if ($result != null) {
$idCall = mysql_insert_id();
if (is_array($listPlayers)) {
foreach ($listPlayers as $idPlayer) {
$result = mysql_query("INSERT INTO call_player (idCall, idPLayer) VALUES ('$idCall','$idPlayer')")or
die ('unable' . mysql_error());
return true;
}
} else {
return false;
}
} else {
return false;
}
}
The problem is that the PHP doesn't recognize the ArrayList so I can't insert the strings into the database. But if I create an Array in the PHP, the function works well.
Edit: Ok. I found the problem. If I sent a string, I can´t use it as a Array. Now I have to convert the string into and array. Any idea? This is the string: [8, 10]. I have to put 8 and 10 in array.
Edit: I found the solution.
We have an ArrayList and we have to create a string. We can use a lot of methods but I use:
Iterator it = seleccionados.iterator();
StringBuilder listIdPlayers = new StringBuilder();
int i = 0;
while (it.hasNext()) {
if (i > 0) {
listIdPlayers.append(" ").append(it.next());
i++;
} else {
listIdPlayers.append(it.next());
i++;
}
}
With this we have a string like this XX XX XX XX XX. With a space between the words.
Now we go to our php and we use the method explode(). With explode() we create the array.
$players = explode(" ", $listPlayers);
And then we check that the result is an array and iterate it.
if ($result != null) {
$idCall = mysql_insert_id();
if (is_array($players)) {
foreach ($players as $idPlayer) {
$result = mysql_query("INSERT INTO call_player (idCall, idPLayer) VALUES ('$idCall','$idPlayer')")or
die ('unable' . mysql_error());
}
return true;
}
Very important to put the return out of the foreach.
Thank you