现在在我的课堂上,我们正在为我的学校构建一个 android 应用程序。这堂课没有实际的开会时间,所以我班上的一些人没有面对面交谈过。其中一个人一直在为不同的提要编写 json 和 php。问题是他给我发了一个 php 文件,我不完全确定如何解析其中的信息以便能够在应用程序上显示提要。
我相当精通 Java 和 XML,而不是 PHP(我现在正在学习)或 JSON(大约一年前使用过一次)
所以我有3个问题
1.我的应用需要什么发送页面才能访问不同的提要?
2.数据库应该放在哪里?
3.我应该如何解析从这个页面发送给我的信息。
仅供参考,该应用程序现在只是一个基本设计,因此无需担心课程或任何事情。
这是PHP文件
<?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;
}
?>
其中还包含一个 html 文件,
<!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>
我真的希望我能和作者见面,但我似乎不想锻炼。
您能提供的任何帮助将不胜感激。谢谢