I have a list of records from database, see the attachment. How can i insert a vat no for each record using student id(see leftmost column). Each record has a input field(see rightmost column).
Here is my view:
<?php $attributes = array('id' => 'VATformAdd', 'name' =>
'VATformAdd', 'autocomplete' => 'off'); echo form_open('report/addVATno', $attributes);?>
<fieldset>
<legend>Income By Student Admission</legend>
<table class="">
<thead>
<tr>
<th width="15%">Student ID</th>
<th width="15%">Student Form No</th>
<th width="15%">Course</th>
<th width="20%">Student Name</th>
<th width="15%">Admitted by</th>
<th width="10%">Amount</th>
<th width="10%">VAT Serial No</th>
</tr>
</thead>
<tbody>
<?php
if (isset($addmission_income)) {
foreach ($addmission_income as $addmissionincome) {
?>
<tr>
<td><?php echo $addmissionincome->student_id;?></td>
<td><?php echo $addmissionincome->student_form_no;?></td>
<td><?php echo $addmissionincome->course_name;?></td>
<td><?php echo $addmissionincome->student_full_name;?></td>
<td><?php echo user_profile_name($addmissionincome->student_added_by); ?></td>
<td><?php echo $addmissionincome->student_fee_paid;?></td>
<td><input type="text" name="vatno[<?php echo $addmissionincome->student_id;?>]" class="width-100"/></td>
<?php
}
?>
</tr>
<?php
}
?>
</tbody>
</table>
<p>
<input id="submit" name="submit" type="submit" value="Add VAT Number" />
</p>
</fieldset>
<?php echo form_close();?>
Here is my controller:
function addVATno(){
$studentid = $this->input->post('studentid');
foreach($studentid as $a){
if($val[$a]!=''){
$this->report_mdl->addVATno($a);
}
}
}
Here is Model:
function addVATno($a){
$val = $this->input->post('vatno');
$updatevatsl = array(
'dupVATserial_no' => $val);
$this->db->where('student_id', $a);
$query = $this->db->update('data_student_master', $updatevatsl);
return $query;
}
make your view file with a hidden student_id like:
<input type="hidden" name="student_id[]" value="<?php echo $addmissionincome->student_id;?>"/>
and vatno field:
<input type="text" name="vatno[]" class="width-100"/>
Adding the following to your controller will do everything you need (without needing to put any code in your model):
$student_id = $this->input->post('student_id');
$vatno = $this->input->post('vatno');
for ($i = 0; $i < count($student_id); $i++) {
$sm_data[] = array(
'vatno' => $vatno[$i],
'student_id' => $student_id[$i]
);
}
$this->db->update_batch('data_student_master', $sm_data, 'student_id');
Note: It would be better to move the db call to your model to adhere to MVC conventions.
Your issue is that you aren't pulling the correct values from POST.
You are not submitting the student id on its own anywhere in the form, so I can't see how you would be able to access it from POST.
You are also trying to access 'vatno' from POST to insert it into a DB, but 'vatno' in POST is an array.
Because you are using the student id as the array key in your form, you can access both the student ID and the vat number you want to update from the same array. In your controller, you can do:
function addVATno(){
//vatnos is an array, the key is the student id
$vatnos = $this->input->post('vatno');
foreach($vatnos as $key=>$vatno){
$this->report_mdl->addVATno($key, $vatno);
}
}
Then in your model, you just need:
function addVATno($studentid, $vatno){
$updatevatsl = array(
'dupVATserial_no' => $vatno
);
$this->db->where('student_id', $studentid);
$query = $this->db->update('data_student_master', $updatevatsl);
return $query;
}
Related
I am working on a grading system but whenever I input a grade, it saves a blank instead of the submitted value.
The form is on the table itself and not on a separate page. I know doing it on a separate page will solve my problem but I want to do it on the table itself.
Like this
Here is the code for the table
<tr>
<th>Student Number</th>
<th>Course</th>
<th>Email</th>
<th>Name</th>
<th>Grade</th>
<th>Remark</th>
<th>Add/Edit</th>
<?php
$table = "Users";
$fetch = $database->getReference($table)->getValue();
if($fetch > 0)
{
foreach($fetch as $key => $row)
{
?>
<tr>
<td> <?= $row['Student_number']; ?> </td>
<td> <?= $row['Course']; ?> </td>
<td> <?= $row['Email']; ?> </td>
<td> <?= $row['Name']; ?> </td>
<?php
if(!$row['grade']
{
?>
<td><input type = "text" name = "grade" id = "grade"></input></td>
<td><button type = "submit" name = "update" value = "<?=$key?>"</button></td>
<td>N/A</td>
}
else
{
?>
<td><?=$row[grade];?></td>
<?php
}
}
}
?>
And here is the code for the input
if(isset($_POST[update]))
{
$key = $_POST[update];
$grade = $_POST[grade];
$postGrade = ['grade' => $grade,];
$table = 'Users;
$updateResult = $database->getReference($table)->update($postGrade);
}
I need help finding what's wrong with my code that makes the input a blank instead of the intended submitted value.
I have to delete a record from database using a button but my delete query does not work. Records are entered in database successfully with insertion query. I followed exact tutorial for php code available on YouTube "How to delete records from database with PHP & MySQL" by "kanpurwebD". The code in tutorial works fine but my code still does not delete record. (I have 2 records entered in database).
My code is as follows:
<div class="container">
<div class="row">
<form action='add_record.php' method='get'><button type='submit' name='id' value='submit' class='btn btn-default'>ADD RECORD</button><br />
</form>
<table class="table table-hover table-responsive">
<thead>
<tr>
<th>Topic #</th>
<th>Name</th>
<th>Admin ID</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
echo '<br />';
$query = "SELECT * FROM tb_topic";
$result = $con->query($query);
if(isset($_POST['submitDeleteBtn'])){
$key = $_POST['keyToDelete'];
$check = "Select * from tb_topic where topic_id = '$key'";
if(mysqli_num_rows($con, $check)>0){
$query_delete = mysqli_query($con,"Delete from tb_topic where topic_id = '$key'");
echo 'record deleted';
}
}
while($query_row = mysqli_fetch_array($result)) {?>
<tr>
<td><?php echo $query_row['topic_id'];?></td>
<td><?php echo $query_row['topic_name'];?></td>
<td><?php echo $query_row['aid'];?></td>
<td><input type = 'checkbox' name = 'keyToDelete' value = "<?php echo $query_row['topic_id'];?>" required></td>
<td><input type="submit" name="submitDeleteBtn" class="btn btn-danger"></td>
</tr>
<?php }
?>
</html>
I got it resolved by using following statement:
if(isset($_GET['delete'])) {
$page = filter_input(INPUT_GET, 'delete', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
$sql = "DELETE FROM tb_topic WHERE topic_id = $page";
}
$_GET() was not taking id as int so I tried typecasting it and it worked for me.
I have a site where you can order some tickets. If someone makes an order, I want the order to be send to the database. Also I want to put the specific cart items in to be send to the database. See the model I use:
ORDERS CART ITEMS
id* id
amount order_id*
product_id
quantity
This is the code I use with the form:
<form action="index.php?page=cart" method="post" class="">
<table class=" table__nav">
<thead>
<tr>
<th class="p p--bold p--th th--ticket">Ticket</th>
<th class="p p--bold p--th th--name">Name</th>
<th class="p p--bold p--th th--quantity">Quantity</th>
<th class="p p--bold p--th th--price">Price per ticket</th>
<th class="p p--bold p--th th--itemtotal">Item Total</th>
</tr>
</thead>
<tbody>
<?php
$total = 0;
foreach($_SESSION['cart'] as $ticket) {
$ticketTotal = $ticket['ticket']['price'] * $ticket['quantity'];
$total += $ticketTotal;
?>
<tr>
<td>
<?php echo $ticket['ticket']['eye_cart']; ?>
</td>
<td>
<p class = "p--bold"><?php echo $ticket['ticket']['name']; ?></p>
</td>
<td>
<input class = "p td--quantity" type="number" name="quantity[<?php echo $ticket['ticket']['id'];?>]" value="<?php echo $ticket['quantity'];?>" class="replace" required />
</td>
<td>
<p>€<?php echo $ticket['ticket']['price'];?>,-</p>
</td>
<td>
<p>€<?php echo $ticketTotal;?>,-</p>
</td>
<td>
<td class="remove-item"><button type="submit" class="btn remove-from-cart" name="remove" value="<?php echo $ticket['ticket']['id'];?>">×</button></td>
</td>
</tr>
</tbody>
<?php } ?>
</table>
<div class="table__wrap">
<button type="submit" id="update-cart" class="btn btn--cart" name="action" value="update">
<img width="14" height="14" src="./assets/img/refresh.png" alt="refresh">
</button>
</img>
<p class="table__order--total"><span class="span--bold span--bold-no">Total:</span></p>
<p class="table__order--number"><span class="span--bold span--bold-no">€ <?php echo $total ?>,-</span></p>
</div>
<div class="table__wrap table__wrap--end">
<a class="p p__li p__li--light" href="index.php?page=register">
<button type = "submit" name ="action" value = "details" class="btn btn--big btn--big-2 btn--dark">your details -> </button></a>
</div>
</form>
When the user presses the button to go to "your details" I want to save an order with the total amount + the cart items with the order_id and quantity.
I'm using the MVC model for this but I can't really figure it out.
This is how my controller looks:
if ($_POST['action'] == 'details') {
$data = array(
'amount' => $total,
);
$insertedOrder = $this->orderDAO->insertOrder($data);
$this->set('insertedOrder', $insertedOrder);
if (empty($insertedOrder)) {
$errors = $this->orderDAO->validate($data);
$this->set('errors', $errors);
}
}
if ($_POST['action'] == 'details') {
$dataB= array(
'order_id' => ?,
'product_id' => $_SESSION['cart']['id']['ticket']['id'],
'quantity' => $_SESSION['cart']['quantity'],
);
$insertedCartItem = $this->orderDAO->insertCartItem($data);
$this->set('insertedCartItem', $insertedCartItem);
if (empty($insertedCartItem)) {
$errors = $this->orderDAO->validateB($dataB);
$this->set('errors', $errors);
}
}
And the next code is my DAO:
public function insertOrder($data){
$errors = $this->validate( $data );
if (empty($errors)) {
$sql = "INSERT INTO `orders` (`amount`) VALUES (:amount)";
$stmt = $this->pdo->prepare($sql);
$stmt->bindValue(':amount', $data['amount']);
if ($stmt->execute()) {
return $this->selectOrderById($this->pdo->lastInsertId());
}
}
return false;
}
public function insertCartItem($dataB){
$errors = $this->validateB($dataB);
if (empty($errors)) {
$sql = "INSERT INTO `cart_items` (`order_id` `product_id`,`quantity`) VALUES (:order_id, :product_id, :quantity)";
$stmt = $this->pdo->prepare($sql);
$stmt->bindValue(':order_id', $dataB['order_id']);
$stmt->bindValue(':product_id', $dataB['product_id']);
$stmt->bindValue(':quantity', $dataB['quantity']);
if ($stmt->execute()) {
return $this->selectCartItemById($this->pdo->lastInsertId());
}
}
return false;
}
The main problem I have is that I don't know how I can send data from $_SESSION variables to the database. I run into problems in the controller. As you can see I'm sending data with the $data and $dataB, but I think I'm going wrong there.
I'm currently working on codeigniter. I want to display a value that is not been duplicated or overwrite a duplicated value from MySQL database into the datatable of PHP foreach loop.
Here is the picture my 2 tables in database:
Table "employees" & table "times"
Here is the relationship of my 2 tables:
Relation of table "employees" & table "times"
Here is my controller (home.php):
public function goPresent() { // attendance present page
$this->load->model('Model_attendance');
$query = $this->Model_attendance->getEmployees();
$data['EMPLOYEES'] = null;
if ($query) {
$data['EMPLOYEES'] = $query;
}
$this->load->view('imports/header');
$this->load->view('imports/menu');
$this->load->view('attend', $data);
}
Here is my model (model_attendance.php):
class Model_attendance extends CI_Model {
public $userID;
public $username;
public $password;
public $totime;
public $absence_type;
public $date;
public $hours;
public function getEmployees() {
$this->db->select("*");
$this->db->from('times');
$this->db->join('employees', 'employees.empnum = times.userID');
$query = $this->db->get();
return $query->result();
}
}
Here is my view (attend.php):
<table class="table table-striped responsive-utilities jambo_table" id="myTable">
<thead>
<tr class="headings">
<th>Employee No.</th>
<th>Username</th>
<th>Name</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
foreach($EMPLOYEES as $employee){?>
<tr>
<td><?php echo $employee->empnum; ?></td>
<td><?php echo $employee->username; ?></td>
<td><?php echo $employee->name; ?> <?php echo $employee->lastname; ?></td>
<td><?php
if ($employee->hasClockOut==1){
echo '<a class="label label-danger">Inactive</a>';
}else {
echo '<a class="label label-success">Active</a>';
}
?></td>
</tr>
<?php } ?>
</tbody>
</table>
on which basis you want unique values, just add this line in your query....
$this->db->group_by("column name which needs to be unique");
<?php
$customers = $this->db->group_by('customer_id')->get_where('registered_table' , array(
'chat_group_id' => $group_id , 'year' => $reg_year
))->result_array();
foreach($customers as $row):?>.....
the example code above will find duplicated customer_id and avoids printing out duplicated values as you intended. Here i used group_by('customer-id')
I have a page that contains an ordering form, on this form it lists the vendor information and then each of the products for the vendor underneath and in front of the product is an input field that allows the user to input the quantity of each product that they want.
Upon submitting the information goes to a confirmation page where I need to be able to show the order information. On the form on the order page, I have a hidden field that contains the vendor id. and the vendor id is put once for each vendor. What I need to be able to do is not only echo out the quantity but also echo out the vendor id specific for each order. My code is below. The first block is the order page and then the block below that will be the confirm page.
As it stands right now underneath every quantity it displays all the vendor ids as opposed to just the one I need.
<?php defined('C5_EXECUTE') or die("Access Denied.");?>
<div class="ccm-ui">
<?php
$db= Loader::db(); //This loads the database helper.
Loader::model('user'); //This loads the user Model.
$user = new User();
$userInfo = UserInfo::getByID($user->getUserID()); //This gets the user info for the current user.
$userCostCenter = $userInfo->getAttribute('cost_center'); //This sets a variable equal to the attribute Cost Center for the current user.
//The if statement below checks if the user is an admin and then displays the info accordingly.
if ($userCostCenter === "Admin") {
?>
<form name="SelectCostCenter" action="/adminorder" method="POST">
<select name="CostCenter">
<option value="unitedilluminating">United Illumination</option>
<option value="clp">CL&P</option>
</select>
<input type="submit" value="Continue">
<button style="float:right;" type="button" class="btn btn-primary"></button>
</form>
<?php
} elseif ($userCostCenter === "United Illuminating") {
?>
<form name="OrderForm" action="/confirm" method="POST">
<?php
$query = 'SELECT * FROM Vendors WHERE costCenterID = 1';
$productQuery = 'SELECT * FROM Products WHERE costCenterID = 1';
$results = $db->getAll($query);
$productResults = $db->getAll($productQuery);?>
<table class="table">
<thead>
<tr>
<th>Quantity/Product</th>
<th>Category</th>
<th>Vendor</th>
<th>Address</th>
</tr>
<?php
foreach ($results as $vendor) {
?>
<tr class="category">
<td></td>
<td><?php echo $vendor['Category']; ?></td>
<td><?php echo $vendor['Vendor']; ?></td>
<td><?php echo $vendor['Address']; ?></td>
</tr>
<?php foreach ($productResults as $product) { ?>
<tr class="product">
<td colspan="4"><span class="name"><input type="text" name="quantities[]" size="1" /><?php echo $product['Product'];?></span></td>
</tr>
<?php } ?>
<td><input type="hidden" name="vendor[]" value="<?php echo $vendor['vendorID']; ?>"/></td>
<?php
}?>
</table>
<input type="submit" value="Checkout"<button style="float:right;" type="button" class="btn btn-primary"></button>
</form>
</div><?php
}
else {
?>
<form name="OrderForm" action="/confirm" method="POST">
<?php $query = 'SELECT * FROM Vendors Where costCenterID = 2';
$productquery = 'SELECT * FROM Products WHERE costCenterID = 2';
$results = $db->getAll($query);
$productresults = $db->getAll($productquery);?>
<table class="table">
<thead>
<tr>
<th>Quantity/Product</th>
<th>Category</th>
<th>Vendor</th>
<th>Address</th>
</tr>
<?php
foreach ($results as $vendor) {
?>
<tr class="category">
<td></td>
<td><?php echo $vendor['Category'];?></td>
<td><?php echo $vendor['Vendor'];?> </td>
<td><?php echo $vendor['Address'];?></td>
</tr>
<?php
foreach ($productresults as $product){
?>
<tr class="product">
<td colspan="4"><span class="name"><input type="text" name="quantities[<?php echo $vendor['vendorID']; ?>]" size="1" /><?php echo $product['Product'];?></span></td>
<td><input type="hidden" name="vendor[]" value="<?php echo $vendor['vendorID']; ?>"/></td>
</tr>
<?php
}
?>
<?php
}?>
</table>
<input type="submit" value="Checkout"<button style="float:right;" type="button" class="btn btn-primary"></button>
</form>
</div><?php
}
?>
This is the confirm page below.
<?php defined('C5_EXECUTE') or die("Access Denied.");
$db= Loader::db();
$quantity = $_POST['quantities'];
$vendor = $_POST['vendor'];
$minimumorder = 25;
foreach($quantity as $num){
if ($num >= $minimumorder){
echo "$num";
echo "</br>";
foreach($vendor as $vendors){
echo "$vendors";
echo "</br>";
}
}
}
?>
I appreciate any help anyone can give. This has had me stumped for a few days actually.
you might want to rearrange your array, and do something like:
$i = 0;
foreach ($productresults as $product) {
echo '<input name="product['.$i.'][quantity]" />';
echo '<input name="product['.$i.'][vendor_id]" value="'.$vendor['vendorID'].'" type="hidden" />';
++$i;
}
The resulting array in $_POST would have the quantities & their vendor separated into their own arrays.
In your code $vendor['vendorID'] seems the key of your $_POST['quantities'] so in your confirm page you could use:
foreach($quantity as $vendorid=>$num){
if ($num >= $minimumorder){
echo "$num";
echo "</br>";
echo "$vendorid";
}
}