- Beranda
- Programmer Forum
[ASK] Menampilkan JSON DATA berdasarkan user ID
...
TS
anjrit.banget
[ASK] Menampilkan JSON DATA berdasarkan user ID
Hallo gan... adakah yang sedia menjawab pertanyaan ane gan?
jadi ane ingin menampilkan konten tergantung pada user id. pas pengguna log-in ke web, setelah berhasil login, dia akan melihat konten yang tergantung pada "id" nya yang diambil dari file JSON yang terhubung dengan database mysql.
contoh:
user A input data mobil ke database, ketika ia login ke web, user A bakalan liat data mobil yang dia input aja.
user B input data mobil juga tapi beda jenis, pas dia login yang muncul cuma data mobil yang dia input aja.
monggo di check scriptnya
sisi php: [url]https://S E N S O R/vb8q[/url]
<?php
session_start();
if (empty($_SESSION['namauser']) AND empty($_SESSION['passuser'])){
echo "[removed]window.alert('you have to login first');
[removed]=('../index.php')[removed]";
}
else{
?>
<html>
<head>
<title><?php include "../cf/ttl.php"; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UAS E N S O Rpatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1" />
[removed][removed]
[removed][removed]
[removed][removed]
<link rel="nofollow" type="text/css" href="../cf/style.css">
<link rel="nofollow" href="../favicon.ico">
<style type="text/css" title="currentStyle">
@import "../cf/css/demo_table_jui.css";
@import "../cf/css/jquery-ui-1.8.4.custom.css";
</style>
[removed]
$(document).ready(function() {
oTable = $('#example').dataTable({
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"bProcessing": true,
"bServerSide": true,
<!-- sort kolom klasifikasi dan uraian -->
<!-- "aaSorting": [[1, "asc"], [2, "desc"] ], -->
"aaSorting": [[1, "asc"]],
"sAjaxSource": "../json/jsondata.php"
});
} );
[removed]
</head>
<body>
<header id="header">
<?php include "../cf/header.php"; ?>
</header>
<div class="navbar navbar-default navbar-static-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse">
<?php include "atas.php"; ?>
</div>
</div>
</div>
<h1 class="page-title">JSON LIST DATA</h1>
<table cellpadding="0" cellspacing="0" class="display" id="example">
<thead>
<tr>
<th>No</th>
<th width="3%">Kode Klasifikasi</th>
<th width="35%">Uraian</th>
<th width="17%">Tanggal Dokumen</th>
<th>Kurun Waktu</th>
<th>Kondisi Arsip</th>
<th>Gedung</th>
<th>Lemari</th>
<th>Baris</th>
<th>Boks</th>
<th>Detail Arsip</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="11" class="dataTables_empty">Loading data from server</td>
</tr>
</tbody>
</table>
<br /><br />
</div>
<footer><?php include "../cf/ft.php"; ?></footer>
[removed][removed]
</body>
</html>
<?php } ?>
jsondatanya: [url]https://S E N S O R/vb8s[/url]
<?php
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Easy set variables
*/
/* Array of database columns which should be read and sent back to DataTables. Use a space where
* you want to insert a non-database field (for example a counter or static image)
*/
$aColumns = array( 'idb', 'kode_klas', 'uraian', 'jra_akhir_inaktif', 'periode', 'nama_kondisi', 'gedung', 'lemari', 'baris', 'boks', 'id_berkas', 'idaksi');
/* Indexed column (used for fast and accurate table cardinality) */
$sIndexColumn = "id_berkas";
/* DB table to use */
$sTable = "v_berkas";
/* Database connection information
$gaSql['user'] = "root";
$gaSql['password'] = "password";
$gaSql['db'] = "db_1";
$gaSql['server'] = "localhost";*/
include "cnf.php";
/* REMOVE THIS LINE (it just includes my SQL connection user/pass)
include( $_SERVER['DOCUMENT_ROOT']."/datatables/mysql.php" ); */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP server-side, there is
* no need to edit below this line
*/
/*
* Local functions
*/
function fatal_error ( $sErrorMessage = '' )
{
header( $_SERVER['SERVER_PROTOCOL'] .' 500 Internal Server Error' );
die( $sErrorMessage );
}
/*
* MySQL connection
*/
if ( ! $gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) )
{
fatal_error( 'Could not open connection to server' );
}
if ( ! mysql_select_db( $gaSql['db'], $gaSql['link'] ) )
{
fatal_error( 'Could not select database ' );
}
/*
* Paging
*/
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
$sLimit = "LIMIT ".intval( $_GET['iDisplayStart'] ).", ".
intval( $_GET['iDisplayLength'] );
}
/*
* Ordering
*/
$sOrder = "";
if ( isset( $_GET['iSortCol_0'] ) )
{
$sOrder = "ORDER BY ";
for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
{
if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
{
$sOrder .= "`".$aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."` ".
($_GET['sSortDir_'.$i]==='asc' ? 'asc' : 'desc') .", ";
}
}
$sOrder = substr_replace( $sOrder, "", -2 );
if ( $sOrder == "ORDER BY" )
{
$sOrder = "";
}
}
/*
* Filtering
* NOTE this does not match the built-in DataTables filtering which does it
* word by word on any field. It's possible to do here, but concerned about efficiency
* on very large tables, and MySQL's regex functionality is very limited
*/
$sWhere = "";
if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
{
$sWhere = "WHERE (";
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" )
{
$sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
}
}
$sWhere = substr_replace( $sWhere, "", -3 );
$sWhere .= ')';
}
/* Individual column filtering */
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
{
if ( $sWhere == "" )
{
$sWhere = "WHERE ";
}
else
{
$sWhere .= " AND ";
}
$sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
}
}
/*
* SQL queries
* Get data to display
*/
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS `".str_replace(" , ", " ", implode("`, `", $aColumns))."`
FROM $sTable
$sWhere
$sOrder
$sLimit
";
$rResult = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
/* Data set length after filtering */
$sQuery = "
SELECT FOUND_ROWS()
";
$rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
$aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
$iFilteredTotal = $aResultFilterTotal[0];
/* Total data set length */
$sQuery = "
SELECT COUNT(`".$sIndexColumn."`)
FROM $sTable
";
$rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
$aResultTotal = mysql_fetch_array($rResultTotal);
$iTotal = $aResultTotal[0];
/*
* Output
*/
$output = array(
"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array()
);
include "../../cf/fungsi_indotgl.php";
$n=1;
while ( $aRow = mysql_fetch_array( $rResult ) )
{
$row = array();
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $aColumns[$i] == 'idb' )
{
$idb = $aRow[ $aColumns[$i] ];
$row[] = $n + $_GET['iDisplayStart'];
}
elseif ( $aColumns[$i] == 'jra_akhir_inaktif' )
{
$jra_akhir_inaktif = $aRow[ $aColumns[$i] ];
$row[] = tgl_indo($aRow[ $aColumns[$i] ]);
}
elseif ( $aColumns[$i] == "id_berkas" )
{
/* Special output formatting for 'id' */
$id_berkas = $aRow[ $aColumns[$i] ];
$row[] .= ''."<a href='tampil_arsip_i.php?id=$id_berkas'><center><img ></center></a>".' ';
}
elseif ( $aColumns[$i] == "idaksi" )
{
/* Special output formatting for 'id_pinjam' */
$id_pinjam = $aRow[ $aColumns[$i] ];
if ($id_pinjam.'value' != "0")
{
$row[] .= ''."<a ><img src='../images/edit.png' title='Edit'></a>
<!--a href=hapus_ai.php?id=$id_berkas onclick=\"return confirm('really want delete this file??')\"><img src='../images/delete.png' title='delete'></a-->".'';
}
}
elseif ( $aColumns[$i] == "version" )
{
/* Special output formatting for 'version' column */
$row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
}
else if ( $aColumns[$i] != ' ' )
{
/* General output */
$row[] = $aRow[ $aColumns[$i] ];
}
}
$output['aaData'][] = $row;
$n++;
}
echo json_encode( $output );
?>
Thank you berat gan.....
jadi ane ingin menampilkan konten tergantung pada user id. pas pengguna log-in ke web, setelah berhasil login, dia akan melihat konten yang tergantung pada "id" nya yang diambil dari file JSON yang terhubung dengan database mysql.
contoh:
user A input data mobil ke database, ketika ia login ke web, user A bakalan liat data mobil yang dia input aja.
user B input data mobil juga tapi beda jenis, pas dia login yang muncul cuma data mobil yang dia input aja.
monggo di check scriptnya
sisi php: [url]https://S E N S O R/vb8q[/url]
Spoiler for Sisi Php (menu.php):
<?php
session_start();
if (empty($_SESSION['namauser']) AND empty($_SESSION['passuser'])){
echo "[removed]window.alert('you have to login first');
[removed]=('../index.php')[removed]";
}
else{
?>
<html>
<head>
<title><?php include "../cf/ttl.php"; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UAS E N S O Rpatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1" />
[removed][removed]
[removed][removed]
[removed][removed]
<link rel="nofollow" type="text/css" href="../cf/style.css">
<link rel="nofollow" href="../favicon.ico">
<style type="text/css" title="currentStyle">
@import "../cf/css/demo_table_jui.css";
@import "../cf/css/jquery-ui-1.8.4.custom.css";
</style>
[removed]
$(document).ready(function() {
oTable = $('#example').dataTable({
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"bProcessing": true,
"bServerSide": true,
<!-- sort kolom klasifikasi dan uraian -->
<!-- "aaSorting": [[1, "asc"], [2, "desc"] ], -->
"aaSorting": [[1, "asc"]],
"sAjaxSource": "../json/jsondata.php"
});
} );
[removed]
</head>
<body>
<header id="header">
<?php include "../cf/header.php"; ?>
</header>
<div class="navbar navbar-default navbar-static-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse">
<?php include "atas.php"; ?>
</div>
</div>
</div>
<h1 class="page-title">JSON LIST DATA</h1>
<table cellpadding="0" cellspacing="0" class="display" id="example">
<thead>
<tr>
<th>No</th>
<th width="3%">Kode Klasifikasi</th>
<th width="35%">Uraian</th>
<th width="17%">Tanggal Dokumen</th>
<th>Kurun Waktu</th>
<th>Kondisi Arsip</th>
<th>Gedung</th>
<th>Lemari</th>
<th>Baris</th>
<th>Boks</th>
<th>Detail Arsip</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="11" class="dataTables_empty">Loading data from server</td>
</tr>
</tbody>
</table>
<br /><br />
</div>
<footer><?php include "../cf/ft.php"; ?></footer>
[removed][removed]
</body>
</html>
<?php } ?>
jsondatanya: [url]https://S E N S O R/vb8s[/url]
Spoiler for Json Datanya (jsondata.php):
<?php
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Easy set variables
*/
/* Array of database columns which should be read and sent back to DataTables. Use a space where
* you want to insert a non-database field (for example a counter or static image)
*/
$aColumns = array( 'idb', 'kode_klas', 'uraian', 'jra_akhir_inaktif', 'periode', 'nama_kondisi', 'gedung', 'lemari', 'baris', 'boks', 'id_berkas', 'idaksi');
/* Indexed column (used for fast and accurate table cardinality) */
$sIndexColumn = "id_berkas";
/* DB table to use */
$sTable = "v_berkas";
/* Database connection information
$gaSql['user'] = "root";
$gaSql['password'] = "password";
$gaSql['db'] = "db_1";
$gaSql['server'] = "localhost";*/
include "cnf.php";
/* REMOVE THIS LINE (it just includes my SQL connection user/pass)
include( $_SERVER['DOCUMENT_ROOT']."/datatables/mysql.php" ); */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP server-side, there is
* no need to edit below this line
*/
/*
* Local functions
*/
function fatal_error ( $sErrorMessage = '' )
{
header( $_SERVER['SERVER_PROTOCOL'] .' 500 Internal Server Error' );
die( $sErrorMessage );
}
/*
* MySQL connection
*/
if ( ! $gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) )
{
fatal_error( 'Could not open connection to server' );
}
if ( ! mysql_select_db( $gaSql['db'], $gaSql['link'] ) )
{
fatal_error( 'Could not select database ' );
}
/*
* Paging
*/
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
$sLimit = "LIMIT ".intval( $_GET['iDisplayStart'] ).", ".
intval( $_GET['iDisplayLength'] );
}
/*
* Ordering
*/
$sOrder = "";
if ( isset( $_GET['iSortCol_0'] ) )
{
$sOrder = "ORDER BY ";
for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
{
if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
{
$sOrder .= "`".$aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."` ".
($_GET['sSortDir_'.$i]==='asc' ? 'asc' : 'desc') .", ";
}
}
$sOrder = substr_replace( $sOrder, "", -2 );
if ( $sOrder == "ORDER BY" )
{
$sOrder = "";
}
}
/*
* Filtering
* NOTE this does not match the built-in DataTables filtering which does it
* word by word on any field. It's possible to do here, but concerned about efficiency
* on very large tables, and MySQL's regex functionality is very limited
*/
$sWhere = "";
if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
{
$sWhere = "WHERE (";
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" )
{
$sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
}
}
$sWhere = substr_replace( $sWhere, "", -3 );
$sWhere .= ')';
}
/* Individual column filtering */
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
{
if ( $sWhere == "" )
{
$sWhere = "WHERE ";
}
else
{
$sWhere .= " AND ";
}
$sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
}
}
/*
* SQL queries
* Get data to display
*/
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS `".str_replace(" , ", " ", implode("`, `", $aColumns))."`
FROM $sTable
$sWhere
$sOrder
$sLimit
";
$rResult = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
/* Data set length after filtering */
$sQuery = "
SELECT FOUND_ROWS()
";
$rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
$aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
$iFilteredTotal = $aResultFilterTotal[0];
/* Total data set length */
$sQuery = "
SELECT COUNT(`".$sIndexColumn."`)
FROM $sTable
";
$rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
$aResultTotal = mysql_fetch_array($rResultTotal);
$iTotal = $aResultTotal[0];
/*
* Output
*/
$output = array(
"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array()
);
include "../../cf/fungsi_indotgl.php";
$n=1;
while ( $aRow = mysql_fetch_array( $rResult ) )
{
$row = array();
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $aColumns[$i] == 'idb' )
{
$idb = $aRow[ $aColumns[$i] ];
$row[] = $n + $_GET['iDisplayStart'];
}
elseif ( $aColumns[$i] == 'jra_akhir_inaktif' )
{
$jra_akhir_inaktif = $aRow[ $aColumns[$i] ];
$row[] = tgl_indo($aRow[ $aColumns[$i] ]);
}
elseif ( $aColumns[$i] == "id_berkas" )
{
/* Special output formatting for 'id' */
$id_berkas = $aRow[ $aColumns[$i] ];
$row[] .= ''."<a href='tampil_arsip_i.php?id=$id_berkas'><center><img ></center></a>".' ';
}
elseif ( $aColumns[$i] == "idaksi" )
{
/* Special output formatting for 'id_pinjam' */
$id_pinjam = $aRow[ $aColumns[$i] ];
if ($id_pinjam.'value' != "0")
{
$row[] .= ''."<a ><img src='../images/edit.png' title='Edit'></a>
<!--a href=hapus_ai.php?id=$id_berkas onclick=\"return confirm('really want delete this file??')\"><img src='../images/delete.png' title='delete'></a-->".'';
}
}
elseif ( $aColumns[$i] == "version" )
{
/* Special output formatting for 'version' column */
$row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
}
else if ( $aColumns[$i] != ' ' )
{
/* General output */
$row[] = $aRow[ $aColumns[$i] ];
}
}
$output['aaData'][] = $row;
$n++;
}
echo json_encode( $output );
?>
Thank you berat gan.....
0
3.7K
Kutip
2
Balasan
Guest
Tulis komentar menarik atau mention replykgpt untuk ngobrol seru
Mari bergabung, dapatkan informasi dan teman baru!
Programmer Forum
20.2KThread•4.2KAnggota
Terlama
Guest
Tulis komentar menarik atau mention replykgpt untuk ngobrol seru