I am developing an attendance management codeigniter app and I am using switchery switch plugin to update attendance status. The process will be:
System display the list of student with attendance status in checkbox with switchery plugin.
Using check or uncheck the checkbox with switchery plugin, if the student is present or absent or simply user change the status.
System sends the form using AJAX, if the student present 1 or absent 0, with student ID and attendance date.
And simply system will change the attendance status of student.
NOTE* THE VALUE OF $date $month $year $program_id $batch_id and $section_id is loaded before in controller function parameter.
And this my app's preview. I need to this form working using AJAX.
And this is my code for the view:
<!-- student attendence status update form -->
<form method="post" action="<?php echo base_url(); ?>index.php?admin/student_attendance">
<!-- start datatable -->
<table class="table table-hover dataTable table-striped width-full" id="departmentTableTools">
<thead>
<tr>
<th class="text-center"><?php echo get_phrase('rool'); ?></th>
<th class="text-center"><?php echo get_phrase('name'); ?></th>
<th class="text-center"><?php echo get_phrase('status'); ?></th>
</tr>
</thead>
<tbody>
<?php
foreach($students as $row):?>
<tr>
<td class="text-center"><?php echo $row['roll_no'];?></td>
<td class="text-center"><?php echo $row['name'];?></td>
<td class="text-center">
<input type="checkbox" value="1" name="status_<?php echo $row['student_id'];?>" data-plugin="switchery" <?php if($status == 1) echo 'checked'; ?>/>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
<input type="hidden" name="date" value="<?php echo $full_date;?>" />
</form>
<!-- end the attendance status update form -->
And this my controller code:
if($_POST) // if the update request is send
{
foreach ($students as $row)
{
$this->crud_model->update_student_attendance($students['student_id'], $this->input->post('status_' . $row['student_id']), $this->input->post('date'));
}
$this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
redirect(base_url() . 'index.php?admin/student_attendance/'.$date.'/'.$month.'/'.$year.'/'.$program_id.'/'.$batch_id , 'refresh');
}
And this is my model code to update and db values:
function update_student_attendance($student_id, $attendance_status, $full_date){
$this->db->where('student_id' , $student_id);
$this->db->where('date' , $full_date);
$this->db->update('attendance' , array('status' => $attendance_status));
}
Related
I've created a notification script and I have a page called notificationCenter.php where the admin user can view ALL the notifications. It will display which users have read which notifications, when it was posted and who by. Everything works fine with it, the only issue is when I go onto the notificationCenter.php page to view all notifications, the table doesn't display the results. However I run an if statement to check if there are any results and I don't get any errors. Here's my code, I'm sure I've done something stupid but I can't see what it is, I'd really appreciate any help!.
//Get User ID
$id = $_SESSION['user_id'];
//Select * Notifications
$notQuery = $serviceConn->query("SELECT * FROM db759709251.notifications WHERE `not_viewedby` NOT LIKE '%$id%' ");
<h4>Nofication Center</h4>
<?php if($notQuery->rowCount()) { ?>
<table>
<thead>
<tr>
<th scope="col">Status</th>
<th scope="col">User</th>
<th scope="col">Notification</th>
<th scope="col">Date</th>
<th scope="col">Viewed By</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<?php
while ($row = $notQuery->fetch()) {
$notid = $row['not_id'];
$notUser = $row['not_user'];
$notMsg = $row['not_msg'];
$notStatus = $row['not_status'];
$notDate = $row['not_date'];
$notViewedby = $row['not_viewedby'];
?>
<tr>
<td style="background-color: <?php echo $statusColour; ?>" data-label="Status"><?php echo $notStatus; ?></td>
<td data-label="User"><?php echo $notUser; ?></td>
<td data-label="Notification"><?php echo $notMsg; ?></td>
<td data-label="Date"><?php echo $notDate; ?></td>
<td data-label="Viewed By"><?php echo $notViewedby; ?></td>
<td data-label="">
<form action="" method="POST">
<input type="hidden" name="notificationID" value="<?php echo $notid; ?>" >
<input type="hidden" name="notificationBy" value="<?php echo $notViewedby; ?>">
<input type="submit" name="read" value="Dismiss!">
</form>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php } else { ?>
<p>You currently have no notifications, please check back later.</p>
<?php } ?>
Now obviously there is a lot of HTML in the middle for styling and layouts, but I didn't think it was important enough as it shouldn't have any effect on the output of the table. But as you can see the if statement SHOULD print that there are no notifications and to check back later, and it doesn't so in theory there should be records to display.
This is my controller, that i use to send the row to the model:
function excluir(){
$codcliente = $this->input->post('codcliente');
$this->load->model("Cliente_class");
$this->Cliente_class->excluirCliente($codcliente);
redirect('cliente/busca');
}
my model:
function excluirCliente($codcliente){
$this->db->delete('cliente', array('codcliente' => $codcliente));
}
my table (view):
<form action="#" method="POST" id="form">
<div style="float: left;">
<input type="button" onclick="cadastro();" value="Novo cliente" name="botao" class="btn btn-primary">
</div>
<div class="dataTable_wrapper">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Código</th>
<th>Nome</th>
<th>Cidade</th>
<th>Telefone</th>
<th> </th>
</tr>
<tbody>
<?php
foreach ($records as $row){ ?>
<tr>
<td><input name="codcliente" id="codcliente" value="<?php echo $row->codcliente ?>"></td>
<td><?php echo $row->nome ?></td>
<td><?php echo $row->cidade ?></td>
<td><?php echo ($row->telefone == null) ? "-" : $row->telefone ?></td>
<td><center><input type="button" onclick="excluir();" value="Delete"></td>
</tr>
<?php } ?>
</tbody>
</form>
</thead>
</table>
and my JS:
function excluir(){
document.forms['form'].action = "<?php base_url();?>excluir";
document.forms['form'].submit();
}
its working fine, but the code is deleting my last row in relation with the table on html. i used the order by too see whats beeing deleted, and it works the same...
i dont know what's wrong
Your problem is that you are using the same form name name="codcliente" for all your rows and then submitting the whole form when you delete. The server sees the last value for codcliente which will be your last row and hence deletes it.
To fix, I suggest you use unique form names for each row for all your row fields. Eg. Instead of name="codcliente" use name="codcliente_<?php echo $row->id ?>". And then when you post to the server to delete, use a data-name=<?php echo $row->codcliente ?> on the delete button and in the onClick handler use $(this).data('name') to get the unique name of the field and post that to the server. I suggest you use ids instead of names as well.
I want to fetch some data from one table in the same page when a row of other table is clicked.
Here is the code for of what I am trying to do and I am stuck:
<?php
$db_table="area";
$db_query='select areaid,area_name from ' . $db_table;
$query_result=mysqli_query($dbc,$db_query) or die('error querying db');?>
<div id="area1" >
<h3>Select ony two area from Category </h3>
<form>
<fieldset style="float: left; width: 25%">
<legend>Project Area</legend>
<table border=1>
<tr>
<th> </th>
<th> Area Name </th>
</tr>
<?php
while($row=mysqli_fetch_array($query_result)){
?>
<tr id="<?php echo $row['areaid'];?>" class="area" onlick="showfaculty()">
<td> <input class="selectarea" type="radio" name = "proarea" value="<?php echo $row['areaid'];?>"></td>
<td> <?php echo $row['area_name']; ?></td>
</tr>
<?php }
echo '</table></br></br>
</fieldset>
<fieldset id="avaguide" style="width: 25%">
<legend>Available Guide</legend>
<table border=1>
<tr>
<th> </th>
<th> Available Guides </th>
</tr>';
$db_table2="facultytable";
$db_query='select facultyID,facultName from ' . $db_table2.' where project_count<2';
$query_result=mysqli_query($dbc,$db_query) or die('error querying db');
while($row=mysqli_fetch_array($query_result)){
?>
<tr id="<?php echo $row['facultyID'];?>" class="area">
<td> <input class="selectguide" type="radio" name = "areaguide" value="<?php echo $row['facultyID'];?>"></td>
<td> <?php echo $row['facultName']; ?></td>
</tr>
<?php }
echo '</table></fieldset>
</form>
</div>;`
Now my code is working fine it shows both area and faculty table. But it displays full faculty table. What I want to do is when click the row of area table some function should be called which get the all the faculty id of the selected area id from the table faculty_area(a_id, f_id) and faculty table shows only the name of faculty with fetched faculty id. An example of that function is below.
function showFaculty($aeraid){
$db_table2="faculty_area";
$db_query='select f_id from ' . $db_table2.' where a_id = '.$areaid;
$query_result=mysqli_query($dbc,$db_query) or die('error querying db');
while($row=mysqli_fetch_array($query_result)){ some code that can be used to display faculty table with feteched f_id}
Please tell me how can I do it.
In my opinion the best way to do this task is using AJAX or you can build one page application with JS and RESTful service on PHP. I like AngularJS as frontend.
I am trying to delete -- using checkboxes -- an HTML table row/s populated by SQL data using PHP. I am new to this, so I would appreciate any improvements to my code. (especially SQL-injection, which I read quite a few times while researching)
Part of HTML with a table - inv_main.php
...
<div class="mid">
<h1>Inventory</h1>
<div class="content">
<!-- buttons -->
<div class="inv_btn">
Add
<!-- button to press to delete ticked checkboxes -->
<form action="inventory.php" method="post">
<input name="delete" type="submit" id="delete" value="Delete"/>
</form>
</div>
<!-- TABLE -->
<div id="inv_tbl">
<table class="table table-striped" id="tblSearch">
<thead>
<tr>
<th>Product Name</th>
<th>Supplier Name</th>
<th>Category</th>
<th>Unit Price</th>
<th>Retail Price</th>
<th>Est. profit per unit</th>
</tr>
</thead>
<tbody>
<?php foreach($allInfo as $info): ?>
<tr>
<td> <?= $info['product'] ?> </td>
<td> <?= $info["supplier_name"] ?> </td>
<td> <?= $info["category"] ?> </td>
<td>$ <?= $info["unit_price"] ?> </td>
<td>$ <?= $info["retail_price"] ?> </td>
<td>$ <?= number_format(($info["unit_price"] - $info["retail_price"]), 2) ?></td>
<td><input type="checkbox" name="checkbox[<?= $info['product'] ?>"] id="checkbox[]" value="<?= $info['product'] ?>" method="post"/></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</div>
</div>
</div>
...
PHP - inventory.php
<?php
/**
* inventory.php
* configures the inventory page.
*/
// configuration
require("../includes/config.php");
require("../includes/render_dash.php");
// if user reached page via GET...
if($_SERVER["REQUEST_METHOD"] == "GET")
{
// get all the info from the inventory database
$allInfo = [];
$getInfo = query("SELECT * FROM inventory WHERE id = ?", $_SESSION["id"] );
foreach($getInfo as $info)
{
$allInfo[] = [
"product" => $info["product"],
"unit_price" => number_format($info["unit_price"], 2),
"retail_price" => number_format($info["retail_price"], 2),
"supplier_name" => $info["supplier_name"],
"category" => $info["category"],
];
}
// render HTML
render("inv_main.php", ["title" => "Inventory", "allInfo" => $allInfo ] );
}
Note the 'form' right after the comment <!-- button to press to delete ticked checkboxes --> and the last table data inside the tbody tag.
I already saw similar questions but I still can't figure it out. If it helps, my PRIMARY KEYS in the inventory database are the user's id and product. The id is used for the $_SESSION, so using that would delete all the inventory data for, say user id #30 so I'm trying to reach the product, which is a string.
So I'm creating a database for my website and I want to create an admin section that allows you to add or delete from the table. Here is a snapshot of what I want to achieve...
In my php file I have if($_POST['delete_category']) which correctly gets the delete button clicks, but then I'm not sure how to distinguish which delete button was actually clicked. I'm sure this is a very simple solution, but I'm stuck. Thanks!
You can discern which button is submitted by this following markup (based on your example fetched results from DB):
<?php
if(isset($_POST['delete_category'])) {
$id = $_POST['delete_category']; // the value="1" or value="3" goes in here
echo $id;
}
?>
<form method="POST" action="">
<table border="1">
<tr>
<th>ID</th><th>Name</th><th>Delete</th>
</tr>
<tr>
<td>1</td>
<td>Senior Pictures</td>
<td><button typpe="submit" name="delete_category" value="1">Delete</button></td>
<!-- each delete button has the same name, but different values -->
</tr>
<tr>
<td>3</td>
<td>Engagements</td>
<td><button typpe="submit" name="delete_category" value="3">Delete</button></td>
</tr>
</table>
</form>
If I had to guess this makes sense on the fetching: (Note: Just a sample!)
<form method="POST" action="">
<table border="1">
<tr>
<th>ID</th><th>Name</th><th>Delete</th>
</tr>
<?php while($row = $results->fetch_assoc()): ?> <!-- assuming mysqli() -->
<?php while($row = mysql_fetch_assoc($result)): ?> <!-- assuming on mysql (bleh) -->
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td>
<button typpe="submit" name="delete_category" value="<?php echo $row['id']; ?>">Delete</button>
</td>
</tr>
<?php endwhile; ?>
</table>
</form>