how to display contents from another table (codeigniter)? - php

I want to do crud on my page and I have to get content from another table and display it in a select tag. my plan was to make an update page wherein it will get the data from another table to display (in this case the category_name, cat_id was the foreign key at the menu table).
I already tried to join because that's what other forums have said but I think I was wrong.
help would really be appreciated.
this is my model:
function inputMenuToUpdate($menu_id){
$this->db->select('cat_id, category.category_id, category.category_name AS category_name');
$this->db->from('menu');
$this->db->join('category', 'menu.cat_id = category.category_id');
$this->db->where('menu_id',$menu_id);
return $query->row();
}
this is my controller:
public function inputToUpdate($menu_id){
$data['row'] = $this->adminMenuModel->inputMenuToUpdate($menu_id);
$this->load->view('adminEdit', $data);
}
this is my view:
<tbody>
<!-- $result is from the $data in CrudController-->
<?php foreach($result as $row){?>
<tr>
<td><?php echo $row->menu_name; ?></td>
<td><?php echo $row->category_name; ?></td>
<td><?php echo $row->price; ?></td>
<td><?php echo $row->description; ?> </td>
<td>
<i class="fa fa-pencil"></i>
</td>
</tr>
<?php } ?>
</tbody>
and this is my update page:
<div class="container" style="width: 40rem;">
<form method="POST" action="<?php echo site_url('adminMenuController/update'); ?> / <?php echo $row->category_id; ?>">
<button type="button" class="btn btn-link"> Go Back </button>
<h1> UPDATE MENU </h1>
<div class="border-bottom"></div>
<br>
<br>
<label for="upMName">New Drink Name</label>
<input type="text" name="mn" class="form-control" id="upMName" value="<?php echo $row->menu_name ?>"></input>
<!-- <label for="cat"> Category </label>
<select id="cat" class="form-control">
<option> <?php echo $row->category_name; ?> </option>
</select> -->
<label for="upPrice">New Price</label>
<input type="number" name="price" class="form-control" id="upPrice" value="<?php echo $row->price ?>"> </input>
<label for="updesc">New Description </label>
<input type="text" name="desc" class="form-control" id="updesc" value="<?php echo $row->description ?>"> </input>
<br>
<button type="submit" class="btn btn-info"> Update </button>
<button type="reset" class="btn btn-danger"> Cancel </button>
</form>
</div>

Related

Only one radio button value is inserted in php

I have a dynamic radio button that inserts a list of student attendance data
but only one student data is inserted. What am I doing wrong
When only the first student gets inserted. The rest of the students do not get inserted into the database
<div class="table">
<table class="table table-sm table-bordered mx-auto">
<tr>
<th>Student Name</th>
<th>Attendence Status</th>
</tr>
<tbody>
<?php
while ($row=mysqli_fetch_array($select)) {
$student_id=$row['student_id'];
$student_name=$row['student_lastname'].' '.$row['student_midname'].' '.$row['student_firstname'];
$link='student_images/'.$row['student_img'];
?>
<tr>
<td class="px-md-2" ><?php echo '<img src="'.$link.'" class="profile-user-img img-fluid img-circle" alt="Student Picture" />' ?>
<p class="profile-username lead"><?php echo $student_name; ?></p>
<p class="text-muted "><?php echo "Date:".' '.date("d/m/Y"); ?></p>
</td>
<td class="mx-auto" style="width: 40%;">
<div class="radio icheck-square">
<input type="radio" value="1" id="state" name="<?php echo $student_id; ?>" />
<label for="present">Present</label>
</div>
<div class="radio icheck-square">
<input type="radio" value="0" id="state" name="<?php echo $student_id; ?>" />
<label for="absent">Absent</label>
</div></td>
</tr>
<input type="hidden" name="student_id" id="student_id" value="<?php echo $student_id ?>">
<input type="hidden" name="date" id="date" value="<?php echo date("Y/m/d"); ?>">
<?php
}
?>
</tbody>
</table>
<div class="row">
<div class="col">
<button class="btn btn-primary" onclick="AddOnly();">Add Attendance Only</button>
or
<button class="btn btn-info" onclick="AddSend();">Add Attendance & Alert Parents</button>
</div>
</div>
</div>
</div>
The PHP code is that processes the data is below
$state=$_POST['state'],
$student_id=$_POST['student_id'];
$date=$_POST['date'];
$insert=mysqli_query($conn,"INSERT INTO `aza_attendance`(`attendance_id`, `student_id`, `atten_date`, `state`) VALUES (NULL, '$student_id', '$date', '$state')") or die(mysqli_error($conn));
if ($insert) {
if ($state==1) {
echo $student_id. 'was at school on'.' '.$date;
}else if($state==0){
echo $student_id. 'was at <strong>NOT</strong> school on'.' '.$date;
}
}
i think is not good to use a variable in name like you use here name="" i think this is better just put them in one div
<div class="radio icheck-square">
<input type="radio" value="1" id="state" name="state" />
<label for="present">Present</label>
<input type="radio" value="0" id="state" name="state" />
<label for="absent">Absent</label>
</div>

Codeigniter click the radio button and open modify model which displays data from the database

In my code there is a table that displays search results. When a user clicks on the radio button and then clicks on the modify item button, it should open up the modal with information about the item gotten from the database based on item id. Currently the modal opens up with information about all the items on the result page, not just the item selected using the radio button I don't know why it's not taking the value of the radio button and displaying everything.
Here's my code:
Controller:
class Search extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
$this->load->model('searchModel');
$this->load->model('itemModal');
}
public function index(){
$this->load->view('base');
$this->load->view('search');
}
public function displayItem(){
//modify item button is clicked
if(isset($post['#modifyItem'])){
//radio button is checked
if(isset($post['id'])){
//value from the radio button
$id=$this->input->post("id");
$data['results'] = $this->itemModal->get_item_by_id($id);
//open modal with the results
$this->load->view('searchResult/#modifyItem',$data);
}
}
}}
Model:
<?php
class ItemModal extends CI_Model {
function __construct(){
parent::__construct();
}
function get_item_by_id($id){
$this->db->select('*');
$this->db->where('inventoryID =',$id);
// Execute the query.
$query = $this->db->get('inventory');
// Return the results.
return $query->result_array();
}
}
View:
<body>
<h1><center>Item List</center></h1>
<hr>
<div class="container">
<form method="post" action="<?php echo site_url('itemView/viewItems'); ?>">
<table>
<tr>
<th><center><input type="radio" name="id"></center></th>
<th>Inventory ID</th>
<th>Master Code</th>
<th><center>Item Name</center></th>
<th>Color Name</th>
<th><center>Location</center></th>
<th><center>Checkout Allowed</center></th>
</tr>
<?php foreach($results as $rows):?>
<tr>
<td><input type="radio" name="id" value="<?php echo $rows['inventoryID'] ?>" <?php echo set_radio('id', '$rows[inventoryID]'); ?>></td>
<td><?php echo $rows['inventoryID'] ?></td>
<td><?php echo $rows['masterCode'] ?></td>
<td><?php echo $rows['itemName'] ?></td>
<td><?php echo $rows['colorName'] ?></td>
<td><?php echo $rows['location'] ?></td>
<td><input type="checkbox" <?php if($rows['checkoutAllowed'] == 'Yes') echo " checked='checked' "; ?>></td>
</tr>
<?php endforeach; ?>
</table>
</form><br><br>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modifyItem" data-title="Modify an Item" onclick="<?php echo site_url("Search/displayItem"); ?>">Modify an Item</button>
<!-- Modify an Item Modal -->
<div id="modifyItem" class="modal fade">
<div class="modal-dialog">
<form action="<?php echo site_url("Search/updateItem"); ?>" method='POST'>
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Modify an Item</h4>
</div>
<div class="modal-body">
<form role="form">
<?php foreach($results as $rows):?>
<table>
<tr>
<td><input type="text" name="rfid" value="<?php echo $rows['inventoryID']?>"/></td>
<td><input type="text" name="itemCode" placeholder="Item Code"/></td>
<td><input type="text" name="masterCode" value="<?php echo $rows['masterCode']?>"/></td>
</tr>
<tr>
<td><input type="text" name="itemName" value="<?php echo $rows['itemName']?>" /></td>
<td><input type="text" name="colorCode" placeholder="Color Code" /></td>
<td><input type="text" name="colorName" placeholder="Color Name" /></td>
</tr>
<tr>
<td><input type="text" name="location" placeholder="Location" /></td>
<td><input type="text" name="makelocation" placeholder="Location Made"/></td>
<td><input type="text" name="itemCategory" placeholder="Item Category" /></td>
</tr>
<tr>
<td><input type="text" name="materialDescription" placeholder="Material Description" /></td>
<td><input type="text" name="supplier" placeholder="Supplier/Vendor" /></td>
<td><input type="text" name="checkoutAllowed" placeholder="Checkout Allowed" /></td>
</tr>
</table>
<div class="row personal-info">
<div class="col-sm-4">
<div class="form-group">
<textarea name="itemDescription" placeholder="Insert information regarding the weather this item is suitable for and where it is used"></textarea>
<textarea name="Comments" placeholder="Additional Coments on the Item"></textarea>
</div>
</div>
</div>
<?php endforeach; ?>
</form>
</div>
<div class="modal-footer" style="text-align:center;">
<input type="submit" class="btn btn-primary" name="modifyItem" value="Modify Item">
</div>
</div>
</form>
</div>
</div>
<!-- Modify an Item Modal -->
</div></body>
Why you load two time foreach its not good way when multiple record on database for load all data its take to more time use display record pagination library of codei-gniter and for edit make dynamic model code using ajax
your function in controller 'displayItem' write code for modify item and make edit form html
<table>
<tr>
<th><center><input type="radio" name="id"></center></th>
<th>Inventory ID</th>
<th>Master Code</th>
<th><center>Item Name</center></th>
<th>Color Name</th>
<th><center>Location</center></th>
<th><center>Checkout Allowed</center></th>
</tr>
<?php foreach($results as $rows):?>
<tr>
<td><input type="radio" name="id" value="<?php echo $rows['inventoryID'] ?>" <?php echo set_radio('id', '$rows[inventoryID]'); ?>></td>
<td><?php echo $rows['inventoryID'] ?></td>
<td><?php echo $rows['masterCode'] ?></td>
<td><?php echo $rows['itemName'] ?></td>
<td><?php echo $rows['colorName'] ?></td>
<td><?php echo $rows['location'] ?></td>
<td><input type="checkbox" <?php if($rows['checkoutAllowed'] == 'Yes') echo " checked='checked' "; ?>></td>
</tr>
<?php endforeach; ?>
</table>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modifyItem" data-title="Modify an Item" onclick="updateItem();">Modify an Item</button>
<!--Bootstrap model for edit start-->
<div id="myModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content" id="model_data">
//append form data here
</div>
</div>
</div>
<!--Bootstrap model for edit end-->
<script>
function updateItem()
{
var CHECKBOXIDS = PASS_CHECKBOX_CHECKEDVALUE;
$('#model_data').html('');
$.ajax({
url: "<?php echo site_url('Search/displayItem');?>",
type: "POST",
dataType: "html",
data: {'<?php echo $this->security->get_csrf_token_name(); ?>': '<?php echo $this->security->get_csrf_hash(); ?>', 'checkids': CHECKBOXIDS, },
catch : false,
success: function (data) {
$('#model_data').append(data);
}
});
}

value echoing only last value in the array in codeigniter

I have an array with values from a table. When var_dumped it shows also the expected values in the array. But when it is echoed, value in the last array is echoed. How can I echo all the values that are present in the array. My view is as below:
<div class="col-md-12">
<div class="col-md-6">
<div class="form-group">
<label>Appoinment Date</label>
<?php $attributes = array("name" => "Subscriptions add");
echo form_open("Admin/show_appoinment", $attributes);?>
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div> <input type="hidden" name="chair" value="1">
<input type="date" name="n1" id="n1" class="form-control" value="<?php echo set_value('n1'); ?>" onChange="copy();">
</div>
</div> </div>
<div class="col-md-6 " style="margin-top:25px;">
<input type="submit" class="btn btn-sm btn-info ">
</div>
<?php echo form_close(); ?>
</div>
<?php $temp_array=[]; foreach($list as $row){array_push($temp_array, $row['time'], $row['op_no']); } ?>
<?php $attributes = array("name" => "Subscriptions add");
echo form_open("Admin/Add_Appointment", $attributes);?>
<?php $check=9.30; if(in_array($check, $temp_array)){
?>
<tbody id="table1">
<tr>
<td class="bg-danger"><input type="text" name="time" class="form-control" id="Dept_Id" value="9.30" readonly></td>
<td class="bg-danger"><input type="text" class="form-control" value=" <?php echo $row['op_no']; ?> " readonly></td>
<td class="bg-danger"><input type="text" class="form-control" value="<?php echo $row['p_name'];?>" readonly> </td>
<td class="bg-danger"><input type="text" class="form-control" value="<?php echo $row['name'];?>" readonly> </td>
<td class="bg-danger"><input type="button" value="Booked" class="btn btn-sm btn-danger glyphicon col-sm-offset-0">
<input type="button" value="Cancel" name="s1" class="btn btn-sm btn-warning glyphicon col-sm-offset-0"></td>
<?php } else{?>
<td class="bg-success">
<input type="hidden" name="chair" value="1">
<input type="hidden" name="date" id="n2" value="<?php echo set_value('n1');?>"/>
<input type="text" class="form-control" class="form-control" name="time" id="Dept_Id" value="9.30" readonly></td>
<td class="bg-success"><input type="text" class="form-control" name="op_no" id="op_no" value="" ></td>
<td class="bg-success"><select name="p_name" class="form-control" type="text" id="p_name"/>
<option></option>
<?php
foreach($patient->result() as $row)
{
echo '<option value="'.$row->patient_id.'">'.$row->p_name.'</option>';
}
?>
</select></td>
<td class="bg-success"><select class="form-control" name="dr_name" type="text" id="dr_name"/>
<option></option>
<?php
foreach($chair->result() as $row)
{
echo '<option value="'.$row->user_id.'">'.$row->name.'</option>';
}
?></select></td>
<td class="bg-success">
<input type="submit" value="Booking" class="btn btn-sm btn-info glyphicon col-sm-offset-0"></td>
<?php } ?>
</tr>
<?php echo form_close(); ?>
My Controller:
public function show_appoinment()
{
$date=$this->input->post('n1');
$chair=$this->input->post('chair');
$data['list']=$this->Appointment_model->get_appointment_by_date($date,$chair);
$data['chai'] =$this->Appointment_model->getdoctors_chair();
$data['chair'] =$this->Appointment_model->getdoctors_chair1($date);
$data['chairs'] =$this->Appointment_model->getdoctors_chair2($date);
$data['patient'] =$this->Appointment_model->getpatient();
$count['alert_count'] = $this->Stock_model->get_stock_for_alert_msg();
$this->load->view('Admin/header', $count);
if($data['list']){ //var_dump($data['list']); die();
$this->load->view('Admin/time_slot_view',$data);
}
else{
$this->load->view('Admin/time_slot_view2',$data);
}
$this->load->view('Admin/footer');
}
My issue is: if I have two rows fetched from db in the array, when I try to echo these data one in the last row echoes repeatedly. How could I solve this..Please help.

Displaying information from a specific id from a while loop on a different page

I created an admin function to be able to display/add/edit products that are on my site. I am having no issues with adding and fetching/displaying what products are on my site.
Being able to see the current product I am trying to edit is where I am having difficulties. I am displaying the products that are in my database by fetching all of them through a while loop. I added an edit link that goes to editproducts.php.
The problem I am having is I am not sure how to get the product's data that I clicked on to display and fetch just that product_id's data.
The way I display all of the products
<table class="tableproduct">
<tr>
<th class="thproduct">Product ID</th>
<th class="thproduct">Product Name</th>
<th class="thproduct">Price</th>
<th class="thproduct">Category</th>
<th class="thproduct">Description</th>
<th class="thproduct"></th>
<th class="thproduct"></th>
</tr>
<?php
if(isset($_POST['product_id']) && is_numeric($_POST['product_id'])) {
mysqli_query($con, "DELETE FROM products WHERE product_id = ". $_POST['product_id'] ."")
or die("Could not DELETE: " . mysqli_error($con));
"Your product was successfully deleted.";
} else {Session::flash('addproduct', 'Your product was successfully deleted.');
}
if( $result ){
while($row = mysqli_fetch_assoc($result)) :
?>
<form method="POST" action="adminproducts.php">
<tr>
<td class="tdproduct"><?php echo $row['product_id']; ?> </td>
<td class="tdproduct"><?php echo $row['name']; ?> </td>
<td class="tdproduct"><?php echo $row['price']; ?> </td>
<td class="tdproduct"><?php echo $row['category']; ?> </td>
<td class="tdproduct"><?php echo $row['description']; ?> </td>
<td class="tdproduct"><a href='editproducts.php?product_id=<?php echo $row['product_id']; ?>'>EDIT</a></td>
<input type="hidden" name="product_id" value="<? echo $row['product_id']; ?>"/>
<td class="tdproduct"><input name="delete" type="submit" value="DELETE "/></td>
</tr>
</form>
<?php
endwhile;
}
?>
</table>
The page where I want to edit the product.
I tried the GET function and pulling the product_id. This does nothing. The name and price that I am trying to echo at the bottom of the code does not display anything other than the echoed string '$'.
<?php
$_GET['product_id'];
?>
<form action="" method="post">
<div class="field">
<label for="product_id">Product ID</label>
<input type="text" name="product_id" class="inputbar">
</div>
<div class="field">
<label for="name">Product Name</label>
<input type="text" class="inputbar" name="name">
</div>
<div class="field">
<label for="price">Price</label>
<input type="text" class="inputbar" name="price">
</div>
<div class="field">
<label for="category">Category</label>
<input type="text" class="inputbar" name="category">
</div>
<div class="field">
<label for="description">Description</label>
<input type="text" class="inputbar" name="description">
</div>
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
<label for="signinButton">
<input type="submit" id="signinButton" value="Update">
</label>
</form>
<p><?php echo "<a href='./viewProduct.php?view_product=$id'>" . $product['name'] . "</a>"; ?></p>
<p> <?php echo "$" . $product['price']; ?> </p>
Is there something I'm not doing correctly or over-looking?

Codeigniter: Create a dynamic ID

I just want to ask on how can I create a dynamic ID in the fields that I have loaded.
Here is my view:
<div id="content">
<form action="" method="POST">
<div class="modal-body">
<button type="button" class="close" data-dismiss="modal" onclick="location.reload();"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
</div>
<div id="div1">
<fieldset>
<legend><?php // echo $tablename; ?></legend>
<table id="table_main">
<tr>
<th>Column Name:</th>
<th>Type:</th>
<th>Length/Values:</th>
</tr>
<?php foreach($updatemaintenance1 as $field) { ?>
<tr>
<td><input type="text" value="<?php echo $field->name; ?>" style="width: 80%"/></td>
<td>
<select style="width: 80%">
<option><?php echo $field->type; ?></option>
<option>INT</option>
<option>VARCHAR</option>
<option>TEXT</option>
<option>DATE</option>
</select>
</td>
<td><input type="text" value="<?php echo $field->max_length; ?>" style="width: 100%"/></td>
</tr>
<?php } ?>
<tr>
<?php //foreach ($tablename as $row1) { ?>
<td><input type="text" id="ninja79" name="ninja79" value="<?php echo $table_name1; ?>"/></td>
<?php// } ?>
</tr>
</table>
<div class="modal-footer">
<button type="button" class="btn_editinventorytype btn-info btn-sm">Update</button>
<button type="button" class="btn_deleteinventorytype btn-danger btn-sm">Delete</button>
</div>
</fieldset>
</div>
</form>
</div>
As you can see I have a foreach loop where I placed all the values from my database. What I want is to generate an ID on the textboxes and the selectbox. And I have this code in the model where I can modify the loaded fields.
Here is my model:
public function modify_table($modify){
$fields = array($modify[''] = array('name' => $modify[''],
'type' => $modify[''],
'constraint' => $modify['']),
);
$this->dbforge->modify_column($modify['ninja79'], $fields);
}
This codes are not as simple as 'SELECT * FROM 'tablename' I mean I'm talking about modifying tables in the database, so please could you help me? All I want is to have a dynamic ID in my model and in my view. I have no idea on how to code that. This is confusing so PLEASE I need your help. Thank you in advance!

Categories