title: PHP JSON and Android
body: <p>Right now in my class we are building an android app for my school. This class does not have a actual meeting time so some of the people in my class I have not talked to face to face. One of these individuals has been programming the json and php for different feeds. The problem is that he has sent me a php file and I am not entirely sure how to parse the information in it to be able to display the feed on the app.</p>
  
  <p>I am fairly proficient in Java and XML, not so much in PHP(I am taking it now) or JSON(used it once about a year ago)</p>
  
  <p>So I have 3 questions</p>
  
  <p>1.what does my app need to send the page to be able to access the different feeds?</p>
  
  <p>2.Where should the database reside?</p>
  
  <p>3.What way should I parse the info that is sent to me from this page.</p>
  
  <p>FYI the App is just a basic design right now so there is no classes or anything to worry about.</p>
  
  <p>Here is the PHP file</p>
  
  <pre><code>&lt;?php
  
  $server = "localhost";
  $user_name = "web";
  $password = "internet";
  $db_name = "cptc";
  
  @$db = mysqli_connect($server, $user_name, $password, $db_name);
  
  if (mysqli_connect_errno()) {
      echo 'Error: could not connect to database.  Please try again later.';
      exit ;
  }
  
  mysqli_select_db($db, $db_name) or die("could not connect");
  
  $commandType = 'unknown';
  $feedURL="";
  if (isset($_GET['cmd'])) {
      $commandType = trim($_GET['cmd']);
      $commandType = strtolower($commandType);
  }
  if ($commandType == 'staff') {
  
      if (isset($_GET['fname']) &amp;&amp; isset($_GET['lname'])) {
  
          $fname = trim($_GET['fname']);
          $fname = strtolower($fname);
          $lname = trim($_GET['lname']);
          $lname = strtolower($lname);
  
          $query = "SELECT * FROM staff WHERE fname like '" . $fname . "%' AND '" . $lname . "%'";
  
      } else if (isset($_GET['lname'])) {
          $commandType = trim($_GET['cmd']);
          $commandType = strtolower($commandType);
          $lname = trim($_GET['lname']);
          $lname = strtolower($lname);
          $query = "SELECT * FROM staff WHERE lname like '" . $lname . "%'";
  
      } else if (isset($_GET['fname'])) {
          $commandType = trim($_GET['cmd']);
          $commandType = strtolower($commandType);
          $lname = trim($_GET['fname']);
          $lname = strtolower($fname);
          $query = "SELECT * FROM staff WHERE lname like '" . $fname . "%'";
      } else {
          $commandType = trim($_GET['cmd']);
          $commandType = strtolower($commandType);
          $query = "SELECT * FROM " . $commandType;
      }
  
      $result = mysqli_query($db, $query);
  
      if (mysqli_num_rows($result)) {
  
          echo '{"Data":[';
  
          $first = true;
          $row = mysqli_fetch_assoc($result);
  
          while ($row = mysqli_fetch_row($result)) {
  
              if ($first) {
                  $first = false;
              } else {
                  echo ',';
              }
              echo json_encode($row);
          }
          echo ']}';
      } else {
          echo '[]';
      }
  } else if ($commandType == 'event') {
      if (isset($_GET['current']) &amp;&amp; isset($_GET['future'])) {
          $current = $_GET['current'];
          $current = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
          $currentDate = date("Y-m-d", $current);
  
          $future = $_GET['future'];
          $future = mktime(0, 0, 0, date("m"), date("d") + 7, date("Y"));
          $futureDate = date("Y-m-d", $future);
          $userid = 'michael.herrera1977%40gmail.com';
          $magicWord = '200f9cbc38a87ae4f65051f26d19e5a4';
          $feedURL = "http://www.google.com/calendar/feeds/$userid/private-$magicWord/basic?start-min=$currentDate&amp;start-max=$futureDate";
  
      } else if (isset($_GET['current'])) {
          $current = $_GET['current'];
          $current = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
          $currentDate = date("Y-m-d", $current);
          $userid = 'michael.herrera1977%40gmail.com';
          $magicWord = '200f9cbc38a87ae4f65051f26d19e5a4';
          $feedURL = "http://www.google.com/calendar/feeds/$userid/private-$magicWord/basic?start-min=$currentDate";
  
      } else if (isset($_GET['future'])) {
          $future = $_GET['future'];
          $future = mktime(0, 0, 0, date("m"), date("d") + 7, date("Y"));
          $futureDate = date("Y-m-d", $future);
          $userid = 'michael.herrera1977%40gmail.com';
          $magicWord = '200f9cbc38a87ae4f65051f26d19e5a4';
          $feedURL = "http://www.google.com/calendar/feeds/$userid/private-$magicWord/basic?start-max=$futureDate";
      }
  
      // read feed into SimpleXML object
      $xmlSimple = simplexml_load_file($feedURL);
  
      //gets info based on CMD this will be pulled up if CMD = events
      //if(isset($_GET['events']))
  
      //put each title and content from each date into the array
      foreach ($xmlSimple-&gt;entry as $entry) {
          $title = stripslashes($entry -&gt; title);
          $content = stripslashes($entry -&gt; content);
          $eventsArray[] = array('Title' =&gt; $title, 'content' =&gt; $content);
  
      }
      //clean up json string for easy cheesy use and to get the when, where and event seperated
      $json = json_encode($eventsArray);
      $json = str_replace('"content":', '', $json);
      $json = str_replace('\n', '', $json);
      $json = str_replace('"When:', '"When":"', $json);
      $json = str_replace('Where:', '","Where":"', $json);
      $json = str_replace('Event Status: confirmed', '', $json);
      $json = str_replace('Event Description:', '","Event Description":"', $json);
      $json = preg_replace('/\s+/', '%20', $json);
  
      echo $json;
  }
  ?&gt;
  </code></pre>
  
  <p>There was an html file that was included with this as well,</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
  "http://www.w3.org/TR/html4/strict.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" lang="en"&gt;
      &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
          &lt;title&gt;dbphp&lt;/title&gt;
          &lt;meta name="author" content="Michael" /&gt;
          &lt;!-- Date: 2012-05-03 --&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;form action="dbphp.php" method="get" &gt;
              &lt;!--&lt;input type="hidden" name="cmd" value="staff"&gt;
              &lt;input type="hidden" name="fname" value="m"&gt;
              &lt;input type="hidden" name="lname" value="j"/&gt;&lt;br /&gt;--&gt;
              &lt;input type="hidden" name="cmd" value="event"&gt;
              &lt;input type="hidden" name="current" value="5/10/2012"&gt;
              &lt;input type="hidden" name="future" value="5/15/2012"/&gt;
              &lt;input type="submit" name="submit" value="enter staff member" /&gt;
  
          &lt;/form&gt;
  
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I really wish I could just meet up with the Author but i doesn't seem to want to work out.</p>
  
  <p>Any help you all could provide would be very much appreciated. Thanks</p>