Pemrograman Web Dinamis
Pemrograman Web Dinamis
PHP Arrays
Array digunakan untuk menyimpan satu atau lebih nilai pada sebuah nama variabel.
Jenis-jenis Array
Contoh 1
$names = array("Peter","Quagmire","Joe");
Example 2
Pada contoh ini kita memberikan nilai pada kunci ID secara manual.
$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";
Program8-1.php
<?php
$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";
?>
PEMROGRAMAN WEB DINAMIS
Output program:
Associative Arrays
Contoh 1
Pada contoh ini kita menggunakan sebuah array untuk memberikan nilai umur pada beberapa
orang yang berbeda.
Output program:
Multidimensional Arrays
$families = array
"Griffin"=>array
"Peter",
"Lois",
"Megan",
),
"Quagmire"=>array
"Glenn"
),
"Brown"=>array
PEMROGRAMAN WEB DINAMIS
"Cleveland",
"Loretta",
"Junior"
);
Array di atas akan terlihat seperti di bawah ini jika dituliskan ke output.
Array
[0] =>Cleveland
)
PEMROGRAMAN WEB DINAMIS
Looping/Pengulangan
Statemen Looping statements digunakan untuk mengeksekusi blok program yang sama
beberapa kali.
Jenis-jenis Looping
while
do...while
for
foreach
Statemen while
while (condition)
code to be executed;
Program9-1.php
<html>
<body>
<?php
$i=1;
while($i<=5)
$i++;
?>
</body>
</html>
Statemen do...while
do
code to be executed;
PEMROGRAMAN WEB DINAMIS
while (condition);
Statemen for
code to be executed;
Program9-3.php
<html>
<body>
<?php
?>
</body>
</html>
PHP $_GET
variabel $_GET digunakan untuk mengambil nilai dari form menggunakan metode
“get”.
Variabel $_GET
Program12-1.php
</form>
Ketika user mengklik tombol “submit”, URL yang dikirm akan berbentuk seperti di bawah
ini.
http://www.w3schools.com/welcome.php?name=Peter&age=37
Program12-2.php
Note: Dengan menggunakan $_GET, nama variabel dan nilainya akan ditampilkan di address
bar.
Note: $_GET tidak dapat digunakan untuk mengirim variabel yang besar, nilai yang dapat
dikirim tidak dapat melebihi 100 karakter.
Variabel $_REQUEST
Variabel $_REQUEST dapat digunakan untuk mengambil data dari form yang dikirim
mengunakan variabel $_GET maupun $_POST.
Program12-3.php
Program15-1.php
Diasumsikan bahwa kita mempunyai file header dengan nama “header.php”. Untuk memakai
file ini pada halaman web kita seperti di bawah ini.
<html>
PEMROGRAMAN WEB DINAMIS
<body>
<?php include("header.php"); ?>
<h1>Welcome to my home page</h1>
<p>Some text</p>
</body>
</html>
Program15-2.php
Sekarang, kita asumsikan bahwa kita mempunyai file standar menu yang akan digunakan
pada seluruh halaman (file include biasanya berektensi *.php). Penggunaannya seperti di
bawah ini.
<html>
<body>
<a href=" default.php">Home</a> |
<a href=" about.php">About Us</a> |
<a href=" contact.php">Contact Us</a>
<?php
include("wrongFile.php");
echo "Hello World!";
?>
</body>
PEMROGRAMAN WEB DINAMIS
</html>
Error message:
Warning: include(wrongFile.php) [function.include]:
failed to open stream:
No such file or directory in C:\home\website\test.php on line 5
Warning: include() [function.include]:
Failed opening 'wrongFile.php' for inclusion
(include_path='.;C:\php5\pear')
in C:\home\website\test.php on line 5
Hello World!
<?php
require("wrongFile.php");
echo "Hello World!";
?>
</body>
</html>
Error message:
Warning: require(wrongFile.php) [function.require]:
failed to open stream:
No such file or directory in C:\home\website\test.php on line 5
Fatal error: require() [function.require]:
Failed opening required 'wrongFile.php'
(include_path='.;C:\php5\pear')
in C:\home\website\test.php on line 5
PHP File Handling
Dalam PHP, fungsi fopen() digunakan untuk membuka file.
Membuka File
Program16-1.php
<html>
<body>
<?php
$file=fopen("welcome.txt","r");
?>
</body>
</html>
w Write only. Opens and clears the contents of file; or creates a new file if it
doesn't exist
w+ Read/Write. Opens and clears the contents of file; or creates a new file if it
doesn't exist
a Append. Opens and writes to the end of the file or creates a new file if it
doesn't exist
x Write only. Creates a new file. Returns FALSE and an error if file already
exists
Catatan: Jika fopen() tidak dapat membuka file, maka akan mengembalikan nilai 0 (false).
Program16-2.php
<html>
<body>
<?php
$file=fopen("welcome.txt","r") or exit("Unable to open file!");
?>
</body>
</html>
Menutup File
Program16-3.php
<?php
$file = fopen("test.txt","r");
//some code to be executed
fclose($file);
?>
Memeriksa EOF (End Of File)
Catatan: Kita tidak dapat membaca file yang terbuka dalam mode w, a, dan x!
if (feof($file)) echo "End of file";
Membaca file baris per baris (fgets())
Program16-4.php
<?php
PEMROGRAMAN WEB DINAMIS
Program16-5.php
<?php
$file=fopen("welcome.txt","r") or exit("Unable to open file!");
while (!feof($file))
{
echo fgetc($file);
}
fclose($file);
?>
Program17-1.php
<html>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
upload_file.php
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
PEMROGRAMAN WEB DINAMIS
}
?>
Dengan menggunakan array global PHP $_FILES kita dapat meng-upload file dari client ke server.
Parameter pertama adalah nama input dan yang kedua adalah dapat berupa "name", "type", "size", "tmp_name"
atau "error". Seperti berikut ini:
$_FILES["file"]["name"] – Nama file yang akan di-upload.
$_FILES["file"]["type"] – Type dari file yang akan di-upload.
$_FILES["file"]["size"] – Ukuran dalam byte dari file yang akan di-upload.
$_FILES["file"]["tmp_name"] – Nama kopian sementara dari file yang disimpan di server.
$_FILES["file"]["error"] – Kode error dari file yang di-upload.
Hal ini sangat mudah untuk dilakukan. Untuk alas an keamanan, kita seharusnya menerapkan kebijakan siapa
saja user yang dapat meng-upload file ke server.
Program17-3.php
<?php
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
Membuat Database
Program24-1.php
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
mysql_close($con);
?>
Membuat Tabel
Program24-2.php
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// Create database
if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
// Create table in my_db database
mysql_select_db("my_db", $con);
$sql = "CREATE TABLE Person
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";
mysql_query($sql,$con);
mysql_close($con);
?>
decimal(size,d) Hold numbers with fractions. The maximum number of digits can
double(size,d) be specified in the size parameter. The maximum number of
float(size,d) digits to the right of the decimal is specified in the d parameter
char(size) Holds a fixed length string (can contain letters, numbers, and
special characters). The fixed size is specified in parenthesis
varchar(size) Holds a variable length string (can contain letters, numbers, and
special characters). The maximum size is specified in parenthesis
Age int
)";
mysql_query($sql,$con);
UPDATE table_name
Pilih database yang akan di expor, kemudian klik expor, dan klik buton kirim, kemudian hasil
yang telah di expor akan muncul di file download komputer anda.
Masuk ke database kalian yang ada di phpmyadmin, kemudian klik impor, setelah itu pilih
file yang akan di upload (file tersebut berbentuk .sql), kemudain setelah selesai klik kirim.
Tugas :