Mobile Apps–Memanggil Senarai Dari Database

9:36 PTG 0 Comments A+ a-

service 11.html (local)

<!DOCTYPE html>
<html>

<head>
<style>
h1 {
border-bottom: 3px solid #cc9900;
color: #996600;
font-size: 30px;
}
table, th , td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd) {
background-color: #f1f1f1;
}
table tr:nth-child(even) {
background-color: #ffffff;
}
</style>
</head>

<body>

<h1>Nama Sekolah</h1>
<div id="id01"></div>

<script>
var xmlhttp = new XMLHttpRequest();
var url = "http://ambo-kena.besaba.com/mreport/namasekolah.php";

xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
myFunction(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();

function myFunction(response) {
var arr = JSON.parse(response);
var i;
var out = "<table>";

for(i = 0; i < arr.length; i++) {
out += "<tr><td>" +
arr[i].Id +
"</td><td>" +
arr[i].Title +
"</td><td>" +
arr[i].Content +
"</td></tr>";
}
out += "</table>"
document.getElementById("id01").innerHTML = out;
}
</script>

</body>
</html>


 



 



namasekolah.php



<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

$conn = new mysqli("mysql.hostinger.my", "contoh_username_bro", "password_bro", "contoh_nama_database");

$result = $conn->query("SELECT post_id,post_title, post_content FROM posts");

$outp = "[";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
if ($outp != "[") {$outp .= ",";}
$outp .= '{"Id":"' . $rs["post_id"] . '",';
$outp .= '"Title":"' . $rs["post_title"] . '",';
$outp .= '"Content":"'. $rs["post_content"] . '"}';
}
$outp .="]";

$conn->close();

echo($outp);
?>


 



Contoh printscreen:



mobile-apps-tarikdata



 



SQL:




-- phpMyAdmin SQL Dump
-- version 3.5.2.2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jan 07, 2015 at 05:35 AM
-- Server version: 5.1.67
-- PHP Version: 5.2.17

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `u641767033_mrpt`
--

-- --------------------------------------------------------

--
-- Table structure for table `posts`
--

CREATE TABLE IF NOT EXISTS `posts` (
`post_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`post_title` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`post_content` text COLLATE utf8_unicode_ci NOT NULL,
`post_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`post_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=90 ;

--
-- Dumping data for table `posts`
--

INSERT INTO `posts` (`post_id`, `post_title`, `post_content`, `post_date`) VALUES
(1, 'MAAHAD MUHAMMADI (LELAKI)', '09-744 4003', '0000-00-00 00:00:00'),
(2, 'MAAHAD MUHAMMADI (PEREMPUAN)', '09-748 2549', '0000-00-00 00:00:00'),
(3, 'MAAHAD PENGAJIAN ISLAM', '09-748 3804', '0000-00-00 00:00:00');

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;