Postingan lainnya
Memilih tabel user di database dan menamiplkanya di php
saya mau bertanya kira2 cara mengambil data user dari database dan mengeluarkanya dalam bentuk checklis seperti gambar ini gimana ya gann...???
ini list yang bakal saya tampilkan dengan php database Piran Table users kolom usersname
16 Jawaban:
mas bisa coba pake :
<?php
$q= mysql_query("SELECT * FROM users");
while ($r = mysql_fetch_array($q)) {
echo "<input type='checkbox' value='$r[username]'> $r[username] <br>";
}
?>
$q itu maksudnya apa ya gan hee,,?? saya masi agak binggung2 masalah database
$q itu maksudnya untuk membuat variable yang dipanggil eksekusi query nya dan memperpendek pemanggilannya
$q = mysql_query("SELECT * FROM users");
while ($r = mysql_fetch_array($q)) {
# code
}
kalau tanpa variable $q pemanggilan pada while nya jadi seperti ini
while ($r = mysql_fetch_array("SELECT * FROM users")) {
# code
}
error kaya gini gan setelah saya coba panggil file class.usernya
Fatal error: Uncaught Error: Call to undefined function mysql_query() in C:\xampp\htdocs\Piran\master_ref\customer\tambah_customer.php:80 Stack trace: #0 {main} thrown in C:\xampp\htdocs\Piran\master_ref\customer\tambah_customer.php on line 80
ini class users
require_once('../user/class.user.php');
Coba lihat koding pada C:\xampp\htdocs\Piran\master_ref\customer\tambah_customer.php baris ke 80 gan
ini baris ke 80
$q= mysql_query("SELECT * FROM users");
niat saya mau nampilin data di database dengan kolom user ini kode lengkapnya
<div class="tab-pane" id="team"><?php
$q= mysql_query("SELECT * FROM users");
while ($r = mysql_fetch_array($q)) {
echo "<input type='checkbox' value='$r[username]'> $r[username] <br>";
}
?></div>
koneksi ke database nya uda di include belum ?
bisa kirim full scriptnya ngk mas? dan mohon ditandain yg error dimana..
kalau diliat dari errornya, fungsi 'mysql_query' ngk dikenali... tpi entah mengapa :D oh iya mas, script koneksinya juga boleh dikirim ngk ?
<?php
$con = mysql_connect('localhost', 'root', 'nama_database');
$q = mysql_query("SELECT * FROM users", $con);
while ($r = mysql_fetch_array($q)) {
# code
} ?>
ini pesan error yang di tampilkan
Fatal error: Uncaught Error: Call to undefined function mysql_query() in C:\xampp\htdocs\Piran\master_ref\customer\tambah_customer.php:81 Stack trace: #0 {main} thrown in C:\xampp\htdocs\Piran\master_ref\customer\tambah_customer.php on line 81
ini script keseluruhan
<?php
session_start();
require_once('../../config/koneksi.php');
require_once('class.customer.php');
$cust = new customer($pdo);
if(!empty($_POST['customer'])){
$kategori = $_POST['kategori'];
$customer = $_POST['customer'];
$jenis = $_POST['jenis'];
$created_by = $_SESSION['s_user'];
if($cust->create($kategori,$customer,$jenis,$created_by)){
$sg = "ok";
$msg1 = "Berhasil...Berhasill...Berhasil..";
$alert='alert-success';
}else{
$g = "err";
$msg2 = "Data tidak bisa dimasukan";
$alert='alert-error';
}
}
?>
<form id="forms" method="post" onSubmit="return submitForm('<?php echo $_SERVER['PHP_SELF']; ?>')" class="form-horizontal">
<fieldset>
<legend>Tambah Vendor</legend>
<span>
<?php
if(isset($sg) and $sg=='ok'){
echo "
<div class='alert $alert'>
<button type='button' class='close' data-dismiss='alert'>×</button>
$msg1
</div>";
}elseif(isset($sg) and $sg=='err'){
echo "
<div class='alert $alert'>
<button type='button' class='close' data-dismiss='alert'>×</button>
$msg2
</div>";}
?>
</span>
<!-- tabs -->
<div class="tabbable">
<ul class="nav nav-tabs">
<li class="active"><a href="#general" data-toggle="tab">General</a></li>
<li><a href="#team" data-toggle="tab">Team</a></li>
<li><a href="#lampiran" data-toggle="tab">Lampiran</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="general"><div class="control-group">
<label class="control-label"> Kategori</label>
<div class="controls">
<select name="kategori" id="kategori" required class="chzn-select" data-placeholder="Pilih kategori.............">
<option value=""></option>
<option value="Animasi">Animasi</option>
<option value="Modeling 3D">Modeling 3D</option>
<option value="Still images">Still Images</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="customer" >Vendor</label>
<div class="controls">
<input type="text" id="customer" name="customer" required>
</div>
</div>
<div class="control-group">
<label class="control-label">Type</label>
<div class="controls">
<select name="jenis" id="jenis" required class="chzn-select" data-placeholder="Pilih Model">
<option value="BEV">BEV</option>
<option value="MEV">MEV</option>
</select>
</div>
</div></div>
<div class="tab-pane" id="team">
<?php
$q = mysql_query("SELECT * FROM users", $con);
while ($r = mysql_fetch_array($q)) {
echo "<input type='checkbox' value='$r[username]'> $r[username] <br>";
} ?>
</div>
<div class="tab-pane" id="lampiran"><form name="form" method="post" enctype="multipart/form-data" action="proses.php">
<input type="file" name="imageUpload" id="imageUpload" class="hide"/>
<label for="imageUpload" class="btn btn-large">Select file</label><br/><br/><br/>
<img src="" id="imagePreview" alt="Preview Image" width="100%"/></div>
</form>
</div>
</div>
<!-- /tabs -->
<div class="form-actions">
<div class="controls">
<button type="submit" class="btn btn-primary">Tambah</button>
<button type="button" id="close" class="btn btn-primary" >Tutup</button>
</div>
</div>
</fieldset>
</form>
<script type="text/javascript">
$(document).ready(function(){
$("#close").click(function(){
$("#form-nest").hide("slow");
});
$(".chzn-select").chosen();
});
</script>
<script type="text/javascript">
$('#imageUpload').change(function(){
readImgUrlAndPreview(this);
function readImgUrlAndPreview(input){
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#imagePreview').attr('src', e.target.result);
}
};
reader.readAsDataURL(input.files[0]);
}
});
</script>
<script type="text/javascript" >
$(document).ready(function() {
$('#photoimg').change(function(){
$("#preview").html('');
$("#preview").html('<img src="loader.gif" alt="Uploading...."/>');
$("#imageform").ajaxForm({
target: '#preview'
}).submit();
});
});
</script>
<style type="text/css">
.hide{display:none;}
.btn {
display: inline-block;
padding: 4px 12px;
margin-bottom: 0;
font-size: 14px;
line-height: 20px;
color: #333333;
text-align: center;
vertical-align: middle;
cursor: pointer;
border: 1px solid #bbbbbb;
border-color: #e6e6e6 #e6e6e6 #bfbfbf;
border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25);
border-bottom-color: #a2a2a2;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
}
.btn {
border-color: #c5c5c5;
border-color: rgba(0, 0, 0, 0.15) rgba(0, 0, 0, 0.15) rgba(0, 0, 0, 0.25);
}
.btn-large {
padding: 11px 19px;
font-size: 17.5px;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
}
</style>
ini baris 80-84
<?php
$q = mysql_query("SELECT * FROM users", $con);
while ($r = mysql_fetch_array($q)) {
echo "<input type='checkbox' value='$r[username]'> $r[username] <br>";
} ?>
include koneksinya
<?php
$con = require_once('../../config/koneksi.php');
?>
Baru pemanggilan pada variable $q nya
<?php
$q = mysql_query("SELECT * FROM users", $con);
while ($r = mysql_fetch_array($q)) {
echo "<input type='checkbox' value='$r[username]'> $r[username] <br>";
} ?>
kalau mas pake PDO, kurang lebih gitu kok mas modelnya.. mas sisa ganti2 aja beberapa script biar sesuai ama pdo.. kayak mysql_query,dan mysql_fetch_array nya disesuakan aja mas..
<?php
$q = mysql_query("SELECT * FROM users");
while ($r = mysql_fetch_array($q)) {
echo "<input type='checkbox' value='$r[username]'> $r[username] <br>";
} ?>
dan untuk di -> echo "<input type='checkbox' value='$r[username]'> $r[username] <br>";
mas bisa berikan class sesuai selera mas pada input nya biar tampilannya lebih menarik.. -> echo "<input type='checkbox' class='blablabla' value='$r[username]'> $r[username] <br>";