I'm trying to generate a table of all user accounts and then have buttons next to each user to either delete or change the account level of the user (admin or not admin). What would be the best way to go about this?
Here's my code:
<?php
$query = mysql_query("SELECT user_name,user_id,user_email,user_level
FROM users
ORDER BY user_name ASC");
echo '<table class="table table-hover">
<thead class="thead-default">
<tr>
<th>Username</th>
<th>User ID</th>
<th>Email</th>
<th>Level</th>
<th>Options</th>
</tr>
</thead>';
while($row = mysql_fetch_array($query)){
echo '<tbody>
<tr>
<td class="col-3">' .$row['user_name'].'</td>
<td class="col-3">' .$row['user_id'].'</td>
<td class="col-3">' .$row['user_email'].'</td>
<td class="col-3">' .$row['user_level'].'</td>
<td class="col-3"><a class="btn btn-success" href="#" data-toggle="tooltip" title="Edit"><span class="glyphicon glyphicon-pencil"></span></a>
<a class="btn btn-success" href="#" data-toggle="tooltip" title="Promote"><span class="glyphicon glyphicon-arrow-up"></span></a>
<a class="btn btn-danger" href="#"><span class="glyphicon glyphicon-arrow-down" data-toggle="tooltip" title="Demote"></span></a>
<a class="btn btn-danger" href="delete.php" data-toggle="tooltip" title="Delete"><span class="glyphicon glyphicon-trash"></span></a>
</td>
</tr>
</tbody>';
}
echo '</table>'; ?>
Any help would be appreciated :)
Edit: The admin/standard user is set via user_level with 0 being standard user and 1 being admin
edit 2: Added code
<?php
include 'connect.php';
include 'header.php';
mysql_query("UPDATE users SET user_level='1' WHERE user_id='".$_GET['user_id']."'");
die("User promoted to admin.");
include 'footer.php';
?>
Getting no luck with it, will try to add if statements for feedback on if database row changes
This here is example of code, because your questions lack some details, so i am sharing my code.
<table>
<tr>
<th>User Email </th>
<th>Date & Time </th>
<th>Complain Number</th>
<th>Complain Type</th>
<th>Description</th>
<th>Status</th>
</tr>
<?php
$ccount =1;
$email= $_SESSION["email"];
$query = mysqli_query($con,"Select * from new_complain where new_email =
'$email'");
while($rows = $query->fetch_assoc())
{
?>
<tr>
<input type="hidden" name="<?php echo 'sstd' . $ccount ; ?>" value="<?php
echo $rows['complain_type']; ?>" placeholder="Student Name" />
<td><?php echo $_SESSION["email"]; ?></td>
<td><?php echo $rows['complain_date']; ?></td>
<td><?php echo $rows['new_id']; ?></td>
<td><?php echo $rows['complain_type']; ?></td>
<td><?php echo $rows['new_complain']; ?></td>
<td><?php echo $rows['comlain_status']; ?></td>
</tr>
<?php
$ccount++;
} ?>
</table>
Without full details, it would be something like this:
<a class="btn btn-success" href="promote.php?id='.$row['user_id'].'" data-toggle="tooltip" title="Promote"><span class="glyphicon glyphicon-arrow-up"></span></a>
<a class="btn btn-danger" href="demote.php?id='.$row['user_id'].'"><span class="glyphicon glyphicon-arrow-down" data-toggle="tooltip" title="Demote"></span></a>
Then you need a promote.php in the same directory as this file, which would look like this:
<?php
mysql_query("UPDATE users SET user_level='1' WHERE user_id='".$_GET['user_id']."'");
die("User promoted to admin user.");
And a demote.php like so:
<?php
mysql_query("UPDATE users SET user_level='0' WHERE user_id='".$_GET['user_id']."'");
die("User demoted to standard user.");
Related
I have this code and I want to make the BIT column Color 1 by pressing the check button and I really can't handle the php. I need help, please!
There is the table and how I access each data from the SQL server, it shows fine but the button is not doing anything when I'm trying to _POST in php. I have been looking for an answer but I couldn't find anything.
<table class="table table-hover">
<thead class="thead-light">
<tr>
<th scope="col">Nr Expediente</th>
<th scope="col">Fecha</th>
<th scope="col">Tipo</th>
<th scope="col">Responsable</th>
<th scope="col">Proveedor</th>
<th scope="col">Observaciones</th>
<th scope="col">Hora</th>
<th scope="col">Check</th>
</tr>
</thead>
<tbody>
<?php
$query="SELECT dbo.Transportes.IdExpediente, dbo.Operaciones.IdTransporte, dbo.Operaciones.Id, dbo.Operaciones.Color, dbo.Operaciones.CreadoPor, dbo.Expedientes.Numero, dbo.Expedientes.ProveedorNombre, dbo.Operaciones.Fecha, dbo.Operaciones.Tipo, dbo.Operaciones.Hora, dbo.Operaciones.Instrucciones FROM dbo.Expedientes
INNER JOIN dbo.Transportes ON dbo.Expedientes.Id = dbo.Transportes.IdExpediente INNER JOIN dbo.Operaciones ON dbo.Operaciones.IdTransporte = dbo.Transportes.Id
WHERE convert(date, convert(varchar(30), dbo.Operaciones.Fecha), 101) = CONVERT (date, GETDATE()) AND dbo.Operaciones.Tipo = 0 ORDER BY dbo.Operaciones.Id DESC";
$res=sqlsrv_query($con,$query);
while ($row=sqlsrv_fetch_array($res)){
?>
<tr <?php if ($row['Color'] == 1) {
echo 'class="table-success"';
} else { echo 'class="table-warning"';} ?> ><td><?php echo $row['Numero']; ?></td>
<td><?php echo $row['Fecha']->format('d/m/Y'); ?></td>
<td><?php if ($row['Tipo'] == 1) {
echo "Descarga";
} else { echo "Carga";} ?></td>
<td><?php echo $row['CreadoPor']; ?></td>
<td><?php echo $row['ProveedorNombre']; ?></td>
<td><?php echo $row['Instrucciones']; ?></td>
<td><?php echo $row['Hora']; ?></td>
<td>
<form method="post">
<div class="btn-group">
<button type="button" name="Checking" class="btn btn-success">Check</button>
<input type="hidden" name="id" value="' .$row['Id']. '"/>
<input type="hidden" name="color" value="1"/>
<button type="button" class="btn btn-success dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="sr-only">Toggle Dropdown</span>
</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">In progress</a>
<a class="dropdown-item" href="#">Save for later</a>
<div class="dropdown-divider"></div>
<button type="butoon" name="Unchecking">Uncheck</a>
</div>
</div> </form></td></tr>
<?php
}
?>
</tr>
</tbody>
</table>
<?php if(isset($_POST['Checking'])){
$sql = "UPDATE dbo.Operaciones SET
Color = '1'
WHERE Id = {_POST['id']} ";
$result = sqlsrv_query($con,$sql) or die(sqlsrv_errors());
}
?>
change type="button" to type="submit"
and
change
$sql = "UPDATE dbo.Operaciones SET
Color = '1'
WHERE Id = ".$_POST['id'] ;
//a table to display file from the database
<thead class="thead-inverse">
<tr>
<th>Description </th>
<th>Category</th>
<th>Upload By</th>
<th>Date</th>
<th></th>
</tr>
<thead>
<tbody class="sc">
<?php
// fetch the records from db
while ($row = mysql_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row['fdesc']; ?></td> //filename
<td><?php echo $row['category']; ?></td>
<td><?php echo $row['username']; ?></td> //user who have upload the file
<td><?php echo $row['fdatein']; ?></td>
//the download button
<td><a href="" ><button type="button" class="btn btn-unique active" data-toggle="tooltip" data-placement="right" title="Download"><i class="fa fa-download"></i> // get the specific to download
</button></a></td>
</tr>
<?php
}
?>
</tbody>
</table>
you can add the id in the the link url like so:
<a href="d1.php?fileId=<?php echo $row['fileID']; ?>" >
then in d1.php the id will be in the variable $_GET['fileID']
Screenshoot 1 contain all of my data on database table while screenshoot 2 is detail of someone data whenever i click on search icon on screenshoot 1.
What i need is, how to print only someone data without pass the ID/parameter on my url for calling the controller? Btw, im using DOMPDF as my pdf generator.
Here my code :
Model
public function view_kartu($kode_pasien)
{
$query = $this->db->query("SELECT * FROM tb_pasien where kode_pasien='$kode_pasien' LIMIT 1");
return $query->result_array();
}
Controller
public function cetakkartu($id) {
//set a value for $kode_pasien
$kode_pasien = $id;
// Load all views as normal
$data['title'] = "Data Pasien | Praktik Dokter Umum";
$data['kartu_pasien'] = $this->a_model->view_kartu($kode_pasien);
$this->load->view('cetak-kartu', $data);
// Get output html
$html = $this->output->get_output();
// Load library
$this->load->library('dompdf_gen');
// Convert to PDF
$this->dompdf->load_html($html);
$this->dompdf->render();
$this->dompdf->stream("cetak-kartu" . ".pdf", array ('Attachment' => 0));}
View for all data in my database table (screenshoot 1)
<div class="box">
<div class="box-body">
<div class="btn-group">
<a href="<?=base_url();?>index.php/a_controller/regismanual"/>
<button class="btn-success btn">
Tambah Pasien</button>
</a>
</div>
<br>
<br>
<div class="table-responsive">
<table id="rekam" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>Kode Pasien</th>
<th>Nama Pasien</th>
<th>Email Pasien</th>
<th>Alamat Pasien</th>
<th>Tanggal Lahir</th>
<th>Umur</th>
<th>JK</th>
<th>No. Telp</th>
<th>Opsi</th>
</tr>
</thead>
<tr>
<tbody>
<?php
if(isset($pasien)){
$no=0; foreach ($pasien as $isi): $no++;
# code...
?>
<td><?php echo $isi->kode_pasien?></td>
<td><?php echo $isi->nama_pasien?></td>
<td><?php echo $isi->email_pasien?></td>
<td><?php echo $isi->alamat_pasien?></td>
<td><?php echo $isi->tanggal_lahir?></td>
<td><?php echo $isi->umur?></td>
<td><?php echo $isi->kode_jk?></td>
<td><?php echo $isi->no_telp?></td>
<td>
<a href="<?=base_url()?>index.php/a_controller/updatepasien/<?php echo $isi->kode_pasien?>">
<button type="button" class="btn-success btn"><span class="glyphicon glyphicon-pencil"></span></button>
</a>
<a href="<?=base_url()?>index.php/a_controller/deletepasien/<?php echo $isi->kode_pasien?>" onclick="return confirm('Apakah anda yakin akan menghapus data ini?')">
<button type="button" class="btn-inverse btn"><span class="glyphicon glyphicon-trash"></span></button>
</a>
<a href="<?=base_url()?>index.php/a_controller/detail/<?php echo $isi->kode_pasien?>">
<button type="button" class="btn-warning btn"><span class="glyphicon glyphicon-search"></span></button>
</a>
</td>
</tr>
</tbody>
<?php endforeach;}?>
</table>
</div>
View for my detail data (screenshoot 2)
<div class="box">
<div class="box-body">
<div class="btn-group">
<a href="<?php echo base_url("index.php/a_controller/cetakkartu/4");?>"/>
<button class="btn-success btn">
Cetak Kartu Berobat</button>
</a>
</div>
<br>
<br>
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover">
<?php
if(isset($kartu)){
$no=0; foreach ($kartu as $isi): $no++;
# code...
?>
<tr>
<td>Kode Pasien</td>
<td><?php echo $isi->kode_pasien?></td>
</tr>
<tr>
<td>Nama Pasien</td>
<td><?php echo $isi->nama_pasien?></td>
</tr>
<tr>
<td>Email Pasien</td>
<td><?php echo $isi->email_pasien?></td>
</tr>
<tr>
<td>Alamat Pasien</td>
<td><?php echo $isi->alamat_pasien?></td>
</tr>
<tr>
<td>Tanggal Lahir</td>
<td><?php echo $isi->tanggal_lahir?></td>
</tr>
<tr>
<td>Umur</td>
<td><?php echo $isi->umur?></td>
</tr>
<tr>
<td>Jenis Kelamin</td>
<td><?php echo $isi->kode_jk?></td>
</tr>
<tr>
<td>No. Telp</td>
<td><?php echo $isi->no_telp?></td>
</tr>
<?php endforeach;}?>
</table>
</div>
</div>
</div>
Like you see, i need to pass someone ID like this
<a href="<?php echo base_url("index.php/a_controller/cetakkartu/4");?>"/>
<button class="btn-success btn">
Cetak Kartu Berobat</button>
</a>
It will only print someone with ID = 4 , it makes me cant print others person detail data. Did i need to change the ID whenever i want to print someone data? It's really hard.
anyone can help me?
<a href="<?php echo base_url("index.php/a_controller/cetakkartu").$kartu[0]['kode_pasien'];?>"/>
<button class="btn-success btn">
Cetak Kartu Berobat
</button>
</a>
The following code is showing only the record list and i want when we click on a link then it go to another page which will suppose to show only the record details of a selected title. Currently, it always shows the same details. now what is the solution of show only the records details of a selected title
<table class="table table-striped table-bordered bootstrap-datatable datatable">// the record list table
<thead>
<tr>
<th>ID</th>
<th>Course Name</th>
<th>Duration</th>
<th>Subjects</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php $getcourseslist = find_all("select * from course");
while(#$getcourses = fetch_array($getcourseslist)){
?>
<tr>
<td><?= $getcourses->id; ?></td>
<td> <?= $getcourses->title;?></td>
<td><?= $getcourses->duration/12; ?> Years</td>
<td class="center">
<?php $getsubjects= find_all("select * from subjects where courseid='$getcourses->id'");
while(#$getsubs = fetch_array($getsubjects)){
?>
<?= $getsubs->name; ?><br>
<?php } ?>
</td>
<td class="center">
<?php if($getcourses->status == 1){ ?>
<span class="label label-success">Active</span>
<?php }
else{
?>
<span class="label label-important">Blocked</span>
<?php } ?>
</td>
<td class="center">
<a class="btn btn-success" href="editcourse.php?course=<?= $getcourses->id; ?>">
<i class="halflings-icon white edit"></i>
</a>
<?php if($getcourses->status == 1){ ?>
<a class="btn btn-danger" href="disable.php?course=<?= $getcourses->id; ?>&&action=block&&page=courses">
<i class="halflings-icon white trash"></i>
</a>
<?php }
else{
?>
<a class="btn btn-danger" href="disable.php?course=<?= $getcourses->id; ?>&&action=unblock&&page=courses">
<i class="halflings-icon white trash"></i>
</a>
<?php } ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div><!--/span-->
</div><!--/row-->
// The following code is supposed to show only the record details of a selected title. Currently, it always shows the same detail list of previous page.//
Course Details View
<tr>
<th>ID :</th>
<td><?= $getcourses2->id; ?></td>
</tr>
<tr>
<th>Course Name :</th>
<td><?= $getcourses2->title; ?></td>
</tr>
<tr>
<th>Duration :</th>
<td><?= $getcourses2->duration/12; ?> Years</td>
</tr>
<tr>
<th>Subjects :</th>
<td class="center">
<?php $getsubjects= find_all("select * from subjects where courseid='$getcourses->id'");
while(#$getsubs = fetch_array($getsubjects)){
?>
<?= $getsubs->name; ?><br>
<?php } ?>
</td>
</tr>
<tr>
<th>Monthly Fee :</th>
<td><?= $getcourses2->monthlyfee ; ?></td>
</tr>
<tr>
<th>Examination Fee :</th>
<td><?= $getcourses2->examinationfee; ?></td>
</tr>
<tr>
<th>Addmission Fee :</th>
<td><?= $getcourses2->addmissionfee; ?></td>
</tr>
<tr>
<th>Status :</th>
<td class="center">
<?php if($getcourses2->status == 1){ ?>
<span class="label label-success">Active</span>
<?php }
else{
?>
<span class="label label-important">Blocked</span>
<?php } ?>
</td>
</tr>
<tr>
<th>Actions :</th>
<td class="center">
<a class="btn btn-success" href="editcourse.php?course=<?= $getcourses->id; ?>">
<i class="halflings-icon white edit"></i>
</a>
<?php if($getcourses2->status == 1){ ?>
<a class="btn btn-danger" href="disable.php?course=<?= $getcourses->id; ?>&&action=block&&page=courses">
<i class="halflings-icon white trash"></i>
</a>
<?php }
else{
?>
<a class="btn btn-danger" href="disable.php?course=<?= $getcourses->id; ?>&&action=unblock&&page=courses">
<i class="halflings-icon white trash"></i>
</a>
<?php } ?>
</td>
</tr>
<?php } ?>
</thead>
<tbody>
</table>
enter code here
change the table titles contents to anchor tags
for example as
ID
and then in server side , process the query as
$sort = 'id';
if(isset($_REQUEST['sortby'])&&str_replace(' ','',$_REQUEST['sortby'])!="") $sort = $_REQUEST['sortby'];
$queryText= "select * from table_name where order by ".$sort;
make sure that the value passed by the href is the attribute name in your sql table
this code makes the query to work as sorted default by id.. if a user uses another title, it will switch as needed.
hopes this works..
NOTE: people interested in down voting, please specify the reason
Heres my code: HTML with Server side to get data from the database. How do I get id from the datatables using checkbox option?
<div class="panel-body">
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<h4>
<th></th> <!-- For checkbox column-->
<th>ID Number</th>
<th>Lastname</th>
<th>Firstname</th>
<th>Middlename</th>
<th>Gender</th>
<th>Course</th>
<th>Department</th>
<th>Address</th>
<th>Contact</th>
<th>Birthdate</th>
<th>View</th>
<th>Events</th>
</h4>
</tr>
</thead>
<tbody>
<?php
include 'pgconnect.php';
$mydata = mysqli_query($conn,"SELECT * FROM student") or die(mysql_error());
while($record = mysqli_fetch_assoc($mydata))
{
$id=$record['id'];
?>
<tr>
<td><input type="checkbox"></td>
<td class="id"><?php echo $record['id'] ?></td>
<td class="lname"><?php echo $record['last_name'] ?></td>
<td class="fname"><?php echo $record['first_name'] ?></td>
<td class="mname"><?php echo $record['mid_name'] ?></td>
<td class="sex"><?php echo $record['gender'] ?></td>
<td class="course"><?php echo $record['course'] ?></td>
<td class="dept"><?php echo $record['department'] ?></td>
<td class="add"><?php echo $record['home_address'] ?></td>
<td class="contact"><?php echo $record['contact'] ?></td>
<td class="bdate"><?php echo $record['birthdate'] ?></td>
<td> View Records </td>
<td> <a title='Click here to update record.'href='#edit' data-toggle='modal'><i class="fa fa-pencil fa-fw"></i></a>
<a title='Click here to add record.' href='#'><i class="fa fa-plus fa-fw"></i></a>
<a title='Click here to delete record.' href='delete.php?del=<?php echo $record['id'] ?>'><i class="fa fa-trash-o fa-fw"></i></a></td>
</tr>
<?php
}
?>
</tbody>
</table>
Here's my script:
$(document).ready(function(){
$('#dataTables-example').dataTable();
});
I tried to get the ID but still it didn't works. Hope you can help me.
In your work, you get the ID in table. your should grab it from the checkbox if you use jquery, as in;
<script>
$(document).ready(function(){
document.getElementByID("ckmyid").innerHTML;
});
</script>
Note:add Selector ID in your checkbox, first, say, "ckmyid" so the code will be like this:
<input id="ckmyid" type="checkbox" value="<?php echo $record['id'] ?>">
Based on your structure, you can grab it directly from the link in your view records link if you want to place the ID there,as in;
<td> View Records </td>