HAALO ketemu lagi, kali ini kita akan melanjutkan pembuatan website multi language dengan crud sistem menggunakan php dan mysql sebagai databasenya.
Daftar isi :
1. Penyesuaian terhadap tabel
2. Update kode untuk function.php, home.php dan index.php
3. Membuat CRUD sistem
1. Penyesuaian Terhadap Table
Pada bagian pertama kita sudah membuat table “tbl_lang”, dibagian ke 2 ini saya menambahkan field “icon” untuk menampung gambar icon dari setiap bahasa, seperti gambar dibawah ini
di tabel tbl_post pada bagian 1 kita akan menambahkan field “title” untuk menampung title dari setiap post yang akan kita buat, jadi tbl_post akan tampak seperti dibawah ini
2 Update kode untuk function.php, home.php dan index.php
kita akan mengupdate kode di file function.php, home.php dan index.php
2.1 Function.php
<?php
session_start();
$_SESSION['lang'] = 'NULL';
function pdo_connect_mysql() {
// Update the details below with your MySQL details
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = '';
$DATABASE_NAME = 'db_multilang';
try {
return new PDO('mysql:host=' . $DATABASE_HOST . ';dbname=' . $DATABASE_NAME . ';charset=utf8', $DATABASE_USER, $DATABASE_PASS);
} catch (PDOException $exception) {
// If there is an error with the connection, stop the script and display the error.
exit('Failed to connect to database!');
}
}
// Template header, feel free to customize this
function template_header($title) {
echo <<<EOT
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Multi language sistem dengan php dan MySQL</title>
<link href="style.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css">
</head>
<body>
<header>
<div class="content-wrapper">
<h1>Multi language sistem dengan php dan MySQL</h1>
<div class="link-icons">
<span><a href='index.php?lang=en'><img src='admin/upload/en.jpg'></a></span>
<span style="padding-left:7px;"></span>
<span><a href='index.php?lang=id'><img src='admin/upload/id.jpg'></a></span>
<span style="padding-left:7px;"></span>
<span><a href='index.php?lang=de'><img src='admin/upload/germany.jpg'></a></span>
</div>
</div>
</header>
<main>
EOT;
}
// Template footer
function template_footer() {
$year = date('Y');
echo <<<EOT
</main>
<footer>
<div class="content-wrapper">
<p>© $year, multi langugage System</p>
</div>
</footer>
<script src="script.js"></script>
</body>
</html>
EOT;
}
?>
2.2 index.php
<?php
// Include functions and connect to the database using PDO MySQL
include 'function.php';
$pdo = pdo_connect_mysql();
// Page is set to home (home.php) by default, so when the visitor visits that will be the page they see.
$page = isset($_GET['page']) && file_exists($_GET['page'] . '.php') ? $_GET['page'] : 'home';
// Include and show the requested page
include $page . '.php';
//echo "SELECT * FROM tbl_post where lang_code='$lang_active' ORDER BY id DESC LIMIT 1";
if (isset($_GET['lang']))
{
if ($_GET['lang'] == "en") {
//$a = include "language/english.php";
$lang_active='en';
$stmt = $pdo->prepare("SELECT * FROM tbl_post where lang_code='$lang_active' ORDER BY id DESC");
}
else if ($_GET['lang'] == "id") {
//$a = include "language/indonesian.php";
$lang_active='id';
$stmt = $pdo->prepare("SELECT * FROM tbl_post where lang_code='$lang_active' ORDER BY id DESC");
}
else {
$lang_active='de';
$stmt = $pdo->prepare("SELECT * FROM tbl_post where lang_code='$lang_active' ORDER BY id DESC");
}
} else {
$lang_active='id';
$stmt = $pdo->prepare("SELECT * FROM tbl_post where parent='0' and lang_code='$lang_active'");
}
$stmt->execute();
$recently_added_products = $stmt->fetchAll(PDO::FETCH_ASSOC);
$i=0;
foreach ($recently_added_products as $data_content):
$i++;
echo '<div style="margin: auto;
width: 50%;
border: 3px solid green;
padding: 10px;">'.$i.'. ' .$data_content['lang_post'].'</div>';
endforeach;
?>
2.3 home.php
<?php
if (isset($_GET['lang']))
{
if ($_GET['lang'] == "en") {
//$a = include "language/english.php";
$lang_active='en';
$stmt = $pdo->prepare("SELECT * FROM tbl_post where lang_code='.$lang_active.' ORDER BY id DESC");
}
else if ($_GET['lang'] == "id") {
//$a = include "language/indonesian.php";
$lang_active='id';
$stmt = $pdo->prepare("SELECT * FROM tbl_post where lang_code='.$lang_active.' ORDER BY id DESC");
}
else {
$lang_active='fr';
$stmt = $pdo->prepare("SELECT * FROM tbl_post where lang_code='.$lang_active.' ORDER BY id DESC");
}
} else {
$lang_active='id';
$stmt = $pdo->prepare("SELECT * FROM tbl_post where lang_code='.$lang_active.' ORDER BY id");
}
$stmt->execute();
$recently_added_products = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<?=template_header('Home')?>
<?php
foreach ($recently_added_products as $data_content):
echo $data_content['lang_post'];
endforeach;
?>
<?=template_footer()?>
3 Membuat CRUD sistem
Disini kita akan membuat sistem CRUD untuk mengolah konten dari masing-masing bahasa
3.1 struktur foldernya seperti dibawah ini
Sekarang kita akan membuat file yang ada folder admin yaitu :
File index.php
<div class="products content-wrapper">
<h2>MENU ADMIN PAGE</h2>
<ul>
<li><a href='langdata.php'>Language</a></li>
<li><a href='content.php'>Content</a></li>
</ul>
</div>
File langdata.php
<?php
include '../function.php';
$pdo = pdo_connect_mysql();
// The amounts of products to show on each page
$num_products_on_each_page = 40;
// The current page, in the URL this will appear as index.php?page=products&p=1, index.php?page=products&p=2, etc...
$current_page = isset($_GET['p']) && is_numeric($_GET['p']) ? (int)$_GET['p'] : 1;
// Select products ordered by the date added
$stmt = $pdo->prepare('SELECT * FROM tbl_lang ORDER BY id ASC LIMIT ?,?');
// bindValue will allow us to use integer in the SQL statement, we need to use for LIMIT
$stmt->bindValue(1, ($current_page - 1) * $num_products_on_each_page, PDO::PARAM_INT);
$stmt->bindValue(2, $num_products_on_each_page, PDO::PARAM_INT);
$stmt->execute();
// Fetch the products from the database and return the result as an Array
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Get the total number of products
$total_products = $pdo->query('SELECT * FROM tbl_lang')->rowCount();
?>
<div class="products content-wrapper">
<h1>Data language</h1>
<a href='add.php'>Add New</a>
<p><?=$total_products?> Data Found</p>
<div class="products-wrapper">
<table >
<tr><td>Lang Code</td><td>Lang name</td><td>Icon</td><td>Action</td>
<?php foreach ($products as $product):
echo "<tr><td>";
echo "<div>".$product['lang_code']."</div>";
echo "</td>";
echo "<td>";
echo "<div>".$product['lang_name']."</div>";
echo "</td>";
echo "<td>";
echo '<img src="upload/'.$product['icon'].'">';
echo "</td>";
echo "<td>";
echo "<a href='edit.php?id=".$product['id']."'><button>edit</button></a>";
echo " <a href='delete.php?id=".$product['id']."'><button>delete</button></a>";
echo "</td>";
echo "</tr>";
endforeach;
?>
</table>
</div>
<p></p>
<a href='index.php'>BACK</a>
</div>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
padding:10px;
}
</style>
File edit.php
<?php
include '../function.php';
$id = $_GET['id'];
$pdo = pdo_connect_mysql();
$stmt = $pdo->prepare("SELECT * FROM tbl_lang where id='$id' ORDER BY id DESC");
$stmt->execute();
$recently_added_products = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($recently_added_products as $data_content):endforeach;
?>
<h3>EDIT DATA</h3>
<form method="post" action="edit_berhasil.php" enctype="multipart/form-data">
<input type="hidden" name='langid' value='<?php echo $id; ?>'>
Language Code : <input type="text" name='langcode' value='<?php echo $data_content['lang_code']; ?>'><br><br>
Language name : <input type="text" name='langname' value='<?php echo $data_content['lang_name']; ?>'><br><br>
Language icon : <img src='upload/<?php echo $data_content['icon']; ?>'> <input type="file" name='iconfile'><br><br>
<input type="submit" value="Save">
</form>
File edit_content_berhasil.php
<?php
include '../function.php';
$langcode = $_POST['langcode'];
$id = $_POST['langid'];
$parent = $_POST['parent'];
$title = $_POST['title'];
$content = $_POST['content'];
$pdo = pdo_connect_mysql();
if($parent==0)
{
$stmt = $pdo->prepare("update tbl_post set title='$title', lang_post='$content' where id='$id' and lang_code='$langcode'");
} else {
$stmt = $pdo->prepare("update tbl_post set title='$title', lang_post='$content' where parent='$id' and lang_code='$langcode'");
}
$stmt->execute();
$sukses = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($sukses='true')
{
echo '<script>window.alert("Update sucsessful"); window.location.replace("http://localhost/lang/admin/content.php");</script>';
} else {
echo '<script>window.alert("Update Failed"); window.location.replace("http://localhost/lang/admin/content.php");</script>';
}
?>
File edit_berhasil.php
<?php
include '../function.php';
$id2 = $_POST['langid'];
if(empty($_FILES['iconfile']['tmp_name']) || !is_uploaded_file($_FILES['iconfile']['tmp_name']))
{
$id = $_POST['langid'];
$langcode = $_POST['langcode'];
$langname = $_POST['langname'];
//echo "update tbl_lang set lang_code='$langcode', lang_name='$langname' where id='$id'";
$pdo = pdo_connect_mysql();
$stmt = $pdo->prepare("update tbl_lang set lang_code='$langcode', lang_name='$langname' where id='$id'");
$stmt->execute();
$sukses = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($sukses='true')
{
echo '<script>window.alert("Update sucsessful"); window.location.replace("http://localhost/lang/admin/");</script>';
} else {
echo '<script>window.alert("Update Failed"); window.location.replace("http://localhost/lang/admin/edit.php?id='.$id.'");</script>';
}
} else {
// get data file
$namaFile = $_FILES['iconfile']['name'];
$namaSementara = $_FILES['iconfile']['tmp_name'];
// file location
$dirUpload = "upload/";
// moving file
$terupload = move_uploaded_file($namaSementara, $dirUpload.$namaFile);
$id = $_POST['langid'];
$langcode = $_POST['langcode'];
$langname = $_POST['langname'];
//echo "update tbl_lang set lang_code='$langcode', lang_name='$langname' where id='$id'";
$pdo = pdo_connect_mysql();
$stmt = $pdo->prepare("update tbl_lang set lang_code='$langcode', lang_name='$langname', icon='$namaFile' where id='$id'");
$stmt->execute();
$sukses = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($sukses='true')
{
echo '<script>window.alert("Update sucsessful"); window.location.replace("http://localhost/lang/admin/");</script>';
} else {
echo '<script>window.alert("Update Failed"); window.location.replace("http://localhost/lang/admin/edit.php?id='.$id.'");</script>';
}
}
?>
file detail.php
<?php
$id=$_GET['id'];
$lang=$_GET['lang'];
$parent=$_GET['parent'];
include '../function.php';
$pdo = pdo_connect_mysql();
// The amounts of products to show on each page
$num_products_on_each_page = 40;
// The current page, in the URL this will appear as index.php?page=products&p=1, index.php?page=products&p=2, etc...
$current_page = isset($_GET['p']) && is_numeric($_GET['p']) ? (int)$_GET['p'] : 1;
// Select products ordered by the date added
$stmt = $pdo->prepare("SELECT * FROM tbl_post where id='$id' and lang_code='$lang' ORDER BY id ASC LIMIT ?,?");
// bindValue will allow us to use integer in the SQL statement, we need to use for LIMIT
$stmt->bindValue(1, ($current_page - 1) * $num_products_on_each_page, PDO::PARAM_INT);
$stmt->bindValue(2, $num_products_on_each_page, PDO::PARAM_INT);
$stmt->execute();
// Fetch the products from the database and return the result as an Array
$postcontent = $stmt->fetchAll(PDO::FETCH_ASSOC);
$total_products = $pdo->query("SELECT * FROM tbl_post where id='$id' and lang_code='$lang'")->rowCount();
if ($total_products>0)
{
foreach ($postcontent as $data_post):;
// Get the total number of products
$total_products = $pdo->query("SELECT * FROM tbl_post where id='$id' and lang_code='$lang'")->rowCount();
?>
<h3>EDIT DATA</h3>
<form method="post" action="edit_content_berhasil.php" enctype="multipart/form-data">
<input type="hidden" name='langid' value='<?php echo $id; ?>'>
<input type="hidden" name='langcode' value='<?php echo $lang; ?>'>
<input type="hidden" name='parent' value='<?php echo $parent; ?>'>
<input type="hidden" name='langcode' value='<?php echo $data_post['lang_code']; ?>'>
Title: <br><input type="text" name='title' value='<?php echo $data_post['title']; ?>'><br><br>
Language name : <br><textarea name='content'><?php echo $data_post['lang_post']; ?></textarea><br><br>
<input type="submit" value="Save">
</form>
<?php
endforeach;
} else {
$stmt = $pdo->prepare("SELECT * FROM tbl_post where parent='$id' and lang_code='$lang' ORDER BY id ASC LIMIT ?,?");
// bindValue will allow us to use integer in the SQL statement, we need to use for LIMIT
$stmt->bindValue(1, ($current_page - 1) * $num_products_on_each_page, PDO::PARAM_INT);
$stmt->bindValue(2, $num_products_on_each_page, PDO::PARAM_INT);
$stmt->execute();
// Fetch the products from the database and return the result as an Array
$postcontent = $stmt->fetchAll(PDO::FETCH_ASSOC);
$total_products = $pdo->query("SELECT * FROM tbl_post where parent='$id' and lang_code='$lang'")->rowCount();
if ($total_products>0)
{
foreach ($postcontent as $data_post):;
?>
<h3>EDIT DATA</h3>
<form method="post" action="edit_content_berhasil.php" enctype="multipart/form-data">
<input type="hidden" name='langid' value='<?php echo $id; ?>'>
<input type="hidden" name='langcode' value='<?php echo $data_post['lang_code']; ?>'>
<input type="hidden" name='parent' value='<?php echo $data_post['parent']; ?>'>
Title: <br><input type="text" name='title' value='<?php echo $data_post['title']; ?>'><br><br>
Language name : <br><textarea name='content'><?php echo $data_post['lang_post']; ?></textarea><br><br>
<input type="submit" value="Save">
</form>
<?php
endforeach;
} else {
?>
<h3>ADD NEW DATA <?php echo $lang; ?></h3>
<form method="post" action="addcontent.php?id=id" enctype="multipart/form-data">
<input type="hidden" name='id' value='<?php echo $id; ?>'>
<input type="hidden" name='langcode' value='<?php echo $lang; ?>'>
Title: <br><input type="text" name='title'><br><br>
Language name : <br><textarea name='content'></textarea><br><br>
<input type="submit" value="Save">
</form>
<?php
}
}
?>
File delete.php
<?php
include '../function.php';
$id = $_GET['id'];
$pdo = pdo_connect_mysql();
$stmt = $pdo->prepare("SELECT * FROM tbl_lang where id='$id'");
// bindValue will allow us to use integer in the SQL statement, we need to use for LIMIT
$stmt->execute();
// Fetch the products from the database and return the result as an Array
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($products as $product):
$filename = $product['icon'];
endforeach;
//echo "update tbl_lang set lang_code='$langcode', lang_name='$langname' where id='$id'";
$stmt = $pdo->prepare("delete from tbl_lang where id='$id'");
$stmt->execute();
$sukses = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($sukses='true')
{
$delfilename ='upload/'.$filename;
unlink($delfilename);
echo '<script>window.alert("Deleted sucsessful"); window.location.replace("http://localhost/lang/admin/");</script>';
} else {
echo '<script>window.alert("Deleted Failed"); window.location.replace("http://localhost/lang/admin/edit.php?id='.$id.'");</script>';
}
?>
File delcon.php
<?php
include '../function.php';
$id = $_GET['id'];
$pdo = pdo_connect_mysql();
//echo "update tbl_lang set lang_code='$langcode', lang_name='$langname' where id='$id'";
$stmt = $pdo->prepare("delete from tbl_post where id='$id'");
$stmt->execute();
$sukses = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($sukses='true')
{
echo '<script>window.alert("Deleted sucsessful"); window.location.replace("http://localhost/lang/admin/content.php");</script>';
} else {
echo '<script>window.alert("Deleted Failed"); window.location.replace("http://localhost/lang/admin/content.php");</script>';
}
?>
File content.php
<?php
include '../function.php';
$pdo = pdo_connect_mysql();
// The amounts of products to show on each page
$num_products_on_each_page = 40;
// The current page, in the URL this will appear as index.php?page=products&p=1, index.php?page=products&p=2, etc...
$current_page = isset($_GET['p']) && is_numeric($_GET['p']) ? (int)$_GET['p'] : 1;
// Select products ordered by the date added
$stmt = $pdo->prepare('SELECT * FROM tbl_post where lang_code="id" and parent="0" ORDER BY id ASC LIMIT ?,?');
// bindValue will allow us to use integer in the SQL statement, we need to use for LIMIT
$stmt->bindValue(1, ($current_page - 1) * $num_products_on_each_page, PDO::PARAM_INT);
$stmt->bindValue(2, $num_products_on_each_page, PDO::PARAM_INT);
$stmt->execute();
// Fetch the products from the database and return the result as an Array
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Get the total number of products
$total_products = $pdo->query('SELECT * FROM tbl_post where lang_code="id"')->rowCount();
?>
<div class="products content-wrapper">
<h1>Content</h1>
<a href='addcontent.php'>Add New</a>
<p><?=$total_products?> Content</p>
<div class="products-wrapper">
<table >
<tr><td>Title</td><td>Content</td><td>Language</td><td>Action</td>
<?php foreach ($products as $product):
echo "<tr><td>";
echo "<div>".$product['title']."</div>";
echo "</td>";
echo "<td>";
echo "<div>".$product['lang_post']."</div>";
echo "</td>";
echo "<td>";
$stmt2 = $pdo->prepare('SELECT * FROM tbl_lang ORDER BY id ASC LIMIT ?,?');
$stmt2->bindValue(1, ($current_page - 1) * $num_products_on_each_page, PDO::PARAM_INT);
$stmt2->bindValue(2, $num_products_on_each_page, PDO::PARAM_INT);
$stmt2->execute();
$langtbl = $stmt2->fetchAll(PDO::FETCH_ASSOC);
foreach ($langtbl as $viewlang):
echo '<a href="detail.php?id='.$product['id'].'&lang='.$viewlang['lang_code'].'&parent='.$product['parent'].'"><img src="upload/'.$viewlang['icon'].'"></a> ';
endforeach;
echo "</td>";
echo "<td>";
echo " <a href='delcon.php?id=".$product['id']."'><button>delete</button></a>";
echo "</td>";
echo "</tr>";
endforeach;
?>
</table>
</div>
<p></p>
<a href='index.php'>BACK</a>
</div>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
padding:10px;
}
</style>
File addcontent.php
<?php
include '../function.php';
$pdo = pdo_connect_mysql();
if(isset($_GET['action']))
{
$title = $_POST['title'];
$content = $_POST['content'];
$pdo = pdo_connect_mysql();
$stmt = $pdo->prepare("insert into tbl_post (parent,title,lang_code,lang_post) values ('0','$title','id','$content')");
$stmt->execute();
$sukses = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($sukses='true')
{
echo '<script>window.alert("Insert sucsessful"); window.location.replace("http://localhost/lang/admin/content.php");</script>';
} else {
echo '<script>window.alert("Insert Failed"); window.location.replace("http://localhost/lang/admin/addcontent.php");</script>';
}
} else {
if(!empty($_POST['langcode']))
{
$id = $_POST['id'];
$title = $_POST['title'];
$content = $_POST['content'];
$langcode = $_POST['langcode'];
$pdo = pdo_connect_mysql();
$stmt = $pdo->prepare("insert into tbl_post (parent,title,lang_code,lang_post) values ('$id','$title','$langcode','$content')");
$stmt->execute();
$sukses = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($sukses='true')
{
echo '<script>window.alert("Insert sucsessful"); window.location.replace("http://localhost/lang/admin/content.php");</script>';
} else {
echo '<script>window.alert("Insert Failed"); window.location.replace("http://localhost/lang/admin/addcontent.php");</script>';
}
} else {
?>
<h3>ADD NEW DATA</h3>
<p>Defaut language = 'id'</p>
<form method="post" action="addcontent.php?action=new" enctype="multipart/form-data">
<input type="hidden" name='langid' value='<?php echo $id; ?>'>
<?php
// Language : <select name='langcode'>
//$stmt = $pdo->prepare('SELECT * FROM tbl_lang ORDER BY id ASC');
// $stmt->execute();
// $products = $stmt->fetchAll(PDO::FETCH_ASSOC);
// foreach ($products as $product):
// echo "<option value=".$product['lang_code'].">".$product['lang_name']."</option>";
// endforeach;
//</select><br><br>
?>
Title: <br><input type="text" name='title'><br><br>
Language name : <br><textarea name='content'></textarea><br><br>
<input type="submit" value="Save">
</form>
<?php
}
}
?>
File add.php
<?php
include '../function.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$langcode = $_POST['langcode'];
$langname = $_POST['langname'];
if(empty($_FILES['iconfile']['tmp_name']) || !is_uploaded_file($_FILES['iconfile']['tmp_name']))
{
echo '<script>window.alert("Language Icon empty"); window.location.replace("http://localhost/lang/admin/add.php");</script>';
} else {
// get data file
$namaFile = $_FILES['iconfile']['name'];
$namaSementara = $_FILES['iconfile']['tmp_name'];
// file location
$dirUpload = "upload/";
// moving file
$terupload = move_uploaded_file($namaSementara, $dirUpload.$namaFile);
$pdo = pdo_connect_mysql();
$stmt = $pdo->prepare("insert into tbl_lang (lang_code,lang_name,icon) values ('$langcode','$langname','$namaFile')");
$stmt->execute();
$sukses = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($sukses='true')
{
echo '<script>window.alert("Insert sucsessful"); window.location.replace("http://localhost/lang/admin/");</script>';
} else {
echo '<script>window.alert("Insert Failed"); window.location.replace("http://localhost/lang/admin/add.php");</script>';
}
}
} else {
?>
<h3>ADD NEW DATA</h3>
<a href='index.php'>Back</a> <br><br>
<form method="post" enctype="multipart/form-data">
Language Code : <input type="text" name='langcode'><br><br>
Language name : <input type="text" name='langname'><br><br>
Language icon : <input type="file" name="iconfile"><br><br>
<input type="submit" value="Save" name='save'>
</form>
<?php
}
?>
Demikian tutorial pembuatan website multi language dengan crud sistem menggunakan php dan mysql sebagai databasenya ini semoga bermanfaat.