title: ArrayList<String> Android to PHP. Insert MySQL
body: <p>I have problems trying to send an ArrayList from Android to PHP and then Insert this ArrayList into a MySQL table.</p>
  
  <p>So, first of all I send List with all the parameters.</p>
  
  <pre><code>List&lt;NameValuePair&gt; list = new ArrayList&lt;NameValuePair&gt;();
          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&lt;String&gt;
          list.add(new BasicNameValuePair("idMatch", idMatch));
  </code></pre>
  
  <p>If I print the parameters list: [tag=registerCall, hourMatch=0931, dateMatch=2013-08-07, listPlayers=[8, 10], idMatch=7].</p>
  
  <p>And then in the PHP, I collect the parameters and send it to the function.</p>
  
  <pre><code>if ($tag == 'registerCall') {
  
      $hourMatch = $_POST['hourMatch'];
      $dateMatch = $_POST['dateMatch'];
      $idMatch = $_POST['idMatch'];
      $listPlayers = $_POST['listPlayers'];
  
  
      $call = $db-&gt;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);
      }
  
  
  }
  </code></pre>
  
  <p>And the function is:</p>
  
  <pre><code>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;
  
      }
  
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Edit: I found the solution. </p>
  
  <p>We have an ArrayList and we have to create a string. We can use a lot of methods but I use:</p>
  
  <pre><code>Iterator it = seleccionados.iterator();
          StringBuilder listIdPlayers = new StringBuilder();
          int i = 0;
          while (it.hasNext()) {
              if (i &gt; 0) {
                  listIdPlayers.append(" ").append(it.next());
                  i++;
              } else {
                  listIdPlayers.append(it.next());
                  i++;
              }
          }
  </code></pre>
  
  <p>With this we have a string like this XX XX XX XX XX. With a space between the words. </p>
  
  <p>Now we go to our php and we use the method explode(). With explode() we create the array.</p>
  
  <pre><code>$players = explode(" ", $listPlayers);
  </code></pre>
  
  <p>And then we check that the result is an array and iterate it.</p>
  
  <pre><code>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;
  
  
          }
  </code></pre>
  
  <p>Very important to put the return out of the foreach. </p>
  
  <p>Thank you</p>