kreeSeeker
kreeSeeker

Reputation: 553

PHP JSON and Android

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.

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)

So I have 3 questions

1.what does my app need to send the page to be able to access the different feeds?

2.Where should the database reside?

3.What way should I parse the info that is sent to me from this page.

FYI the App is just a basic design right now so there is no classes or anything to worry about.

Here is the PHP file

<?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']) && 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']) && 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&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->entry as $entry) {
        $title = stripslashes($entry -> title);
        $content = stripslashes($entry -> content);
        $eventsArray[] = array('Title' => $title, 'content' => $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;
}
?>

There was an html file that was included with this as well,

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>dbphp</title>
        <meta name="author" content="Michael" />
        <!-- Date: 2012-05-03 -->
    </head>
    <body>
        <form action="dbphp.php" method="get" >
            <!--<input type="hidden" name="cmd" value="staff">
            <input type="hidden" name="fname" value="m">
            <input type="hidden" name="lname" value="j"/><br />-->
            <input type="hidden" name="cmd" value="event">
            <input type="hidden" name="current" value="5/10/2012">
            <input type="hidden" name="future" value="5/15/2012"/>
            <input type="submit" name="submit" value="enter staff member" />

        </form>

    </body>
</html>

I really wish I could just meet up with the Author but i doesn't seem to want to work out.

Any help you all could provide would be very much appreciated. Thanks

Upvotes: 1

Views: 369

Answers (1)

Ken Y-N
Ken Y-N

Reputation: 15018

This is a PHP-only question, by the looks of things. The API appears to use ? arguments at the end of the PHP file, so we have:

  1. dbphp.php?cmd=staff&lname=<last_name>&fname=<first_name>
    • Look up a user in the staff table.
  2. dbphp.php?cmd=<table_name>
    • Get everything out of the table_name table. Eeek!
  3. dbphp.php?cmd=event?current=<date>&future=<date>
    • Look up michael.herrera1977's Google calendar. The json return value gets horrendously clobbered before returning it

The database lives on the same server that the PHP is on, localhost. To parse the output, there are the standard JSONArray and JSONObject classes to help you out. I'd also delete all the str_replace and preg_replace code as they might make the JSON output from Google invalid.

Upvotes: 1

Related Questions