Codeigniter: Create a dynamic ID - php

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!

Related

how to insert dynamic row data form into mysql in php

I want to get the attendance from the student list into my database.
I have my newattinsert.php file here:
<div class="form-group input-group">
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
<input type="date" name="attdate" id="attdate" data-date="" data-date-format="DD MMMM YYYY" placeholder="MM/DD/YYYY" name="attdate" id="attdate" class="form-control" min="1990-01-01" max="2030-12-31" required/>
</div>
<?php
if (! empty($_POST['classn'])) {
$class = $_POST['classn'];
//run the store proc
$sql = "CALL addattlist('".$class."')";
$result = mysqli_query($conn,$sql);
if (! empty($result)) {?>
<div class="row" style="width: 140%;">
<table id="myTable" class="table table-bordered table-striped">
<thead>
<th>Student Name</th>
<th>Class Name</th>
<th>Status</th>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($result)){
?>
<tr>
<td><input type="hidden" name="name" value="<?php echo $row[1] ?>"><?php echo $row['studentname']; ?></td>
<td><input type="hidden" name="classn" value="<?php echo $row[2] ?>"><?php echo $row['classname']; ?></td>
<td>
<input type="radio" id="absent" name="status" value="Absent">
<label for="status">Absent</label>
<input type="radio" id="present" name="status" value="Present">
<label for="status">Present</label>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<?php
}
}
?>
<div class="group" href="attendanceinsert.php" style="padding-left: 10px; padding-right: 5px;">
<button type="submit" name="submit" class="btn btn-success"><span class="glyphicon glyphicon-ok-sign"></span> Submit Data</button> OR <button type="reset" class="btn btn-warning"> <span class="glyphicon glyphicon-refresh"></span> Reset The Form</button>
</div>
and my attedanceinsert.php file here:
<?php
include('connection.php');
if(isset($_POST['submit'])){
$name= $_POST['. $row[1] .'];
$classn= $_POST['. $row[2] .'];
$status= $_POST['status'];
$attdate= $_POST['attdate'];
$sql = "CALL attendanceregister('".$_POST['name']."', '".$_POST['classn']."','".$_POST['status']."','".$_POST['attdate']."')";
$result = mysqli_query($conn,$sql);
if($result){
$_SESSION['success'] = 'Attedance Recorded!';
header('location: allatt.php');
}
else{
$_SESSION['error'] = 'Something went wrong!';
}
}
?>
the problem is, I only get the data of the last student in the list only and the radio button only allows me to choose one choice only. If I click on the radio button on the student no1, the data that get into my database is the data from the student no2. Any idea how to resolve this? I'm really desperate as this is the only problem that I'm stuck at right now.
ps: this is the stored procedure that I use
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `addattlist`(IN `class` VARCHAR(20))
NO SQL
BEGIN
SELECT studentid, student_name, classname
FROM student
WHERE classname = class;
END$$
DELIMITER ;
EDIT: I still can't figure out the solution. I'm open to any changes or anything that will make it work.

how to display contents from another table (codeigniter)?

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>

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);
}
});
}

I can't log in as admin

i have created a login system where i can login as a admin, or as a user (employer). If i'm logged in as a admin, on the next page i can update, delete or create a new employer, and if i'm logged in as a employer a didn't have that options. This works fine, but today when i'm logged in as admin a don't have that permission and i got a message: You can't create a new user (this is the message that i have posted in else statement).
This is my homepage.php script. Any help?
<?php
require_once("connection.php");
$user_id = $_SESSION['userid'];
$role = $_SESSION['role'];
$sql = "SELECT * FROM login";
$stmt = $conn->query($sql);
?>
<html>
<head>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<nav class="navbar navbar-default">
<div class="navbar-header">
<a class="navbar-brand" href="#"><?php echo $role ?></a>
</div>
<div class="container">
<?php if($role == 'admin') { ?>
<button type="button" class="btn btn-default navbar-btn">New Employee</button>
<?php } else { }?>
<button type="button" class="btn btn-default navbar-btn">Employee</button>
<button type="button" class="btn btn-default navbar-btn">Sign Out</button>
</div>
</nav>
<div class="box">
<div class="col-sm-4 col-md-6 col-md-offset-2">
<table class="table table-hover table-striped table bordered" border="1">
<thead>
<tr>
<th style="width:250px">UserName</th>
<th style="width:250px">User Password</th>
<th style="width:250px">User Role</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach($stmt as $row){ ?>
<tr>
<td><?php echo $row['user_name'] ?></td>
<td>*******</td>
<td><?php echo $row['user_role'] ?></td>
<?php if($role == "admin"){ ?>
<td>
<form action="delete.php" name="deleteForm" method="post">
<input type="hidden" name="delete_id" value="<?php echo $row['user_id'] ?>"/>
<button type="submit" name="delete_user" class="btn btn-info">Delete</button>
</form>
</td>
<td>
<form action="update.php" name="updForm" method="post">
<input type="hidden" name="update_id" value="<?php echo $row['user_id'] ?>"/>
<button type="submit" name="update_user" class="btn btn-info">Update</button>
</form>
</td>
<?php } ?>
</tr>
<?php } ?>
<?php if($role == "admin"){ ?>
<form action="add_user.php" method="POST" name="add_user">
<tr>
<td><input type="text" name="username" class="form-control"/></td>
<td><input type="text" name="password" class="form-control"/></td>
<td>
<select name="select_role" class="form-control">
<option value="admin">Admin</option>
<option value="emp">Employee</option>
</selet>
</td>
<td>
<button type="submit" name="sub_user" class="btn btn-danger">Add User</button>
</td>
</tr>
<?php } else{ ?>
<tr>
You can't add a new user!
</tr>
<?php } ?>
</form>
</tbody>
</table>
</div>
</div>
</body>
</html>
where is session_start();
function because without this function how can you get the session variables so please first put the session_start() at top of the file and then check.

Validate radio button selected in each group

Here is the code. The "surveys" are generated elsewhere. I need validation that a radio button is clicked in each group of answers. The number of answers and answer groups will be variable.
<div id="main">
<div class="container">
<!-- display a table of surveys -->
<h2><?php echo $survey['title']; ?></h2>
<form class="survey" name="take_survey" action="./take_survey" method="post">
<table class="table table-hover">
<input type="hidden" name="survey" value="<?php echo $survey['id'] ?>" />
<tr>
<th>Question</th>
<th>Response</th>
</tr>
<?php foreach ($questions as $question) : ?>
<tr>
<td><?php echo $question['question']; ?></td>
<td></td>
</tr>
<?php foreach ($answers as $answer) : ?>
<?php if ($answer['question_id'] == $question['id']) : ?>
<tr>
<td> <?php echo $answer['answer']; ?></td>
<td><input type="radio" name="question_<?php echo $question['id']?>" value="<?php echo $answer['id'] ?>"></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
<?php endforeach; ?>
</tr>
</table>
<div class="eventButtons">
<input class="btn btn-default" type="submit" name="submit" id="submit" value="Save">
<input class="btn btn-default" type="reset" name="reset" id="reset" value="Clear" class="btn">
</div>
</form>
</div>
</div>

Categories