I am using codeigniter framework.I have a array of id that is to be saved in service_id column in my table. I need to store a array of id in my table. my table structure is as given below.
CREATE TABLE IF NOT EXISTS `addservice` (
`employee_id` int(10) NOT NULL,
`service_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I wrote a controller for this like:
class service extends CI_Controller
{
public function services()
{
$id = $this->session->userdata('employee');
$id_array = json_encode($this->input->post("services"));
$data_to_store=array('employee_id'=>$id,'service_id'=>$id_array));
$this->add_service_model->save($data_to_store);
$data['addservice'] = $this->add_service_model->get_addservices();
$data['main_content'] = 'admin/service_limitation/newview';
$this->load->view('includes/template', $data);
}
}
I used json_encode function. I dont get any error but i am getting a white screen. I wrote a view file where i post my id as check box.In this view file i am posting the service_id as a array and post them to my controller. My view file are:
<table class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th class="header">Service id</th>
<th class="yellow header headerSortDown">Service name </th>
<th class="green header">Service catogary</th>
<th class="red header">Service tax</th>
<th class="red header">Service length</th>
<th class="red header">Service price</th>
<th class="red header">Actions</th>
</tr>
</thead>
<tbody>
<?php
foreach($service as $row)
{
echo '<tr>';
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['service_name'].'</td>';
echo '<td>'.$row['category'].'</td>';
echo '<td>'.$row['service_tax'].'</td>';
echo '<td>'.$row['service_length'].'</td>';
echo '<td>'.$row['service_price'].'</td>';
echo '<td class="crud-actions">
<input type="checkbox" value="'.$row['id'].'" name="services[]"/>
</td>';
echo '</tr>';
}
?>
</tbody>
</table>
my model file
class mymodel extends CI_Model{
public function get_addservices()
{
$this->db->select('*');
$this->db->from('addservice');
$query = $this->db->get();
return $query->result_array();
}
public function save($data)
{
$insert = $this->db->insert('addservice', $data);
return $insert;
}
}
here in this file, i used a function save to save data to my table
Remove json_encode from input post and use for loop to get value of array
Controller
public function services()
$id = $this->session->userdata('employee');
$id_array = $this->input->post("services");
for ($i = 0; $i < count($id_array); $i++) {// add for loop here
if(isset($id_array[$i])&& $id_array[$i]!=""){
$data_to_store = array('employee_id' => $id, 'service_id' => $id_array[$i]);
$this->add_service_model->save($data_to_store);
}
}
$data['addservice'] = $this->add_service_model->get_addservices();
$data['main_content'] = 'admin/service_limitation/newview';
$this->load->view('includes/template', $data);
}
Related
What I'm trying to do is to have one datatable probably as a template and passing different datas on different pages.
Example functions.php
function force() {
global $DBconn;
$q = "SELECT * FROM table";
$res = $DBconn->query($q);
return $this->result;
}
function force_1() {
global $DBconn;
$q = "SELECT * FROM table_1";
$res = $DBconn->query($q);
return $this->result;
}
function table() {
echo '
<table class="sortable">
<thead>
<tr>
<th>
Title
</th>
</tr>
</thead>
<tbody>';
foreach ($this->result as $key) {
echo ' <tr>
<td>
$key->name
</td>
</tr>';
}
echo '</tbody>
</table>';
}
return true;
}
Then in the page.php
$table = table();
$smarty->assign("table", $table);
and page.tpl
<div> {$table} </div>
Currently nothing showed on page, not even an error or something. So:
Is this the way that should be done? With using the returned result in either function and then pass it to the smarty template?
I don't know how to use the smarty template (I never used it), but the $table variable holds what the function table() returns (in your case, the boolean value true). It should return a string, like this:
function force() {
// corrected the function force return value, assuming that you are using PDO connection
global $DBconn;
$q = "SELECT * FROM table";
$res = $DBconn->query($q);
return $res->fetchAll(PDO::FETCH_ASSOC);
}
function table() {
$arr = force();
return
'
<table class="sortable">
<thead>
<tr>
<th>
Title
</th>
</tr>
</thead>
<tbody>';
foreach ($arr as $row) {
echo " <tr>
<td>
$row['name']
</td>
</tr>";
}
echo '</tbody>
</table>';
}
You should be also careful with quotes when using variables inside a string.
EDIT:
If you are using mysqli class
function force() {
global $DBconn;
$q = "SELECT * FROM table";
return $DBconn->query($q);
}
function table() {
$result = force();
return
'
<table class="sortable">
<thead>
<tr>
<th>
Title
</th>
</tr>
</thead>
<tbody>';
//while ($row = $result->fetch_assoc()) {
while ($row = mysql_fetch_assoc($result)) {
echo " <tr>
<td>
$row['name']
</td>
</tr>";
}
echo '</tbody>
</table>';
}
Hi Im currently using codeigniter, Below i have attached the complete code. As i run this Im getting data from database but i want URL field to be link.How to do that.
This is my controller:
public function ajaxsearch()
{
if(is_null($this->input->get('id')))
{
$this->load->view('input_view');
}
else
{
$this->load->model('Infomodel');
$data['Infotable']=$this->Infomodel->Infotable($this->input->get('id'));
$this->load->view('output_view',$data);
}
}
Below is the model:
class Infomodel extends CI_Model
{
function Infotable($search)
{
$query = $this
->db
->select('*')
->from('Info_table')
->like('NAME',$search)
->or_like('URL',$search)
->get();
if($query->num_rows()>0)
{
return $query->result();
}
else
{
return null;
}
}
}
and two views: one is input_view
And another is output_view:
if(!empty($Infotable ))
{
$output = '';
$outputdata = '';
$outputtail ='';
$output .= '<div class="container">
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>Url</th>
<th>Tags</th>
</tr>
</thead>
<tbody>
';
foreach ($Infotable as $objects)
{
$outputdata .= '
<tr>
<td >'.$objects->id.'</td>
<td >'.$objects->name.'</td>
<td>'.$objects->url.'</td>
<td>'.$objects->tags.'</td>
</tr>
';
// echo $outputdata;
}
$outputtail .= '
</tbody>
</table>
</div>
</div> ';
echo $output;
echo $outputdata;
echo $outputtail;
}
else
{
echo 'Data Not Found';
}
Thanks in advance for the help.
if you mean that you want the url to be clickable by the user you can simply change this line:
<td>'.$objects->url.'</td>
to something like this:
<td>'.$objects->url.'</td>
This will create a link in the table cell that will send you to the link
Try using tag inside the table data.
<td> <a heref="'.$objects->url.'">'.$objects->url.'</a></td>
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 need to insert employee id and service id into a new table addservice.i wrote a function add in my controller page where i get my employee id from the session and service id from the service table from the uri.
In controller
public function add()
{
$this->load->library('session');
$id = $this->uri->segment(4);
$data_to_store = array('employee_id'=>$taxdetails['total'],
'service_id'=>$id);
$this->selected_model->store_employee($data_to_store);
$data['main_content'] = 'admin/selected/list';
$this->load->view('includes/template', $data);
}
in model:
this is my model file where wrote the code to insert the employee id and service id.
public function store_employee($data)
{
$insert = $this->db->insert('addservice', $data);
return $insert;
}
in view page
<div class="container top">
<ul class="breadcrumb">
<li>
<a href="http://localhost/elfanto/elfanto_billing/admin/addservice">
Admin </a>
<span class="divider">/</span>
</li>
<li class="active">
Service </li>
</ul>
<div class="row">
<div class="span12 columns">
<div >
<?php
$attributes = array('class' => 'form-inline reset-margin', 'id' => 'myform');
$options_manufacture = array(0 => "all");
foreach ($category as $row)
{
$options_manufacture[$row['id']] = $row['name'];
}
//save the columns names in a array that we will use as filter
$options_products = array();
foreach ($service as $array) {
foreach ($array as $key => $value) {
$options_products[$key] = $key;
}
break;
}
?>
</div>
<table class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th class="header">Service id</th>
<th class="yellow header headerSortDown">Service name </th>
<th class="green header">Service catogary</th>
<th class="red header">Service tax</th>
<th class="red header">Service length</th>
<th class="red header">Service price</th>
<th class="red header">Actions</th>
</tr>
</thead>
<tbody>
<?php
foreach($service as $row)
{
echo '<tr>';
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['service_name'].'</td>';
echo '<td>'.$row['category'].'</td>';
echo '<td>'.$row['service_tax'].'</td>';
echo '<td>'.$row['service_length'].'</td>';
echo '<td>'.$row['service_price'].'</td>';
echo '<td class="crud-actions">
Add service
</td>';
echo '</tr>';
}
?>
</tbody>
</table>
Continue as chair renter
<?php echo '<div class="pagination">'.$this->pagination->create_links().'</div>'; ?>
</div>
</div>
this code doesnt work for. i am not able to resolve my problem can someone correct my code.
In your url try adding index.php after projectname http://localhost/elfanto/index.php/elfanto_billing/admin/addservice and the link does not match the functions.
Example
http://localhost/project/index.php/controller/add/
First I would recommend auto load the session library to get
To get any set session userdata you need to do some thing like but do not know how you set your sessions.
$this->session->userdata('total')
Controller Function
public function add() {
$this->selected_model->store_employee();
$data['main_content'] = 'admin/selected/list';
$this->load->view('includes/template', $data);
}
Model Function
public function store_employee()
{
$data = array(
'employee_id' => $this->session->userdata('total'),
'service_id' => $this->uri->segment(4)
);
$this->db->insert('addservice', $data);
}
if session set in array()
$taxdetails = $this->session->userdata('some_array_name');
$taxdetails['total']
Function example 2
public function store_employee()
{
$taxdetails = $this->session->userdata('some_array_name');
$data = array(
'employee_id' => $taxdetails['total'],
'service_id' => $this->uri->segment(4)
);
$this->db->insert('addservice', $data);
}
I'm trying to learn codeigniter, the first thing i did is to make simple add edit delete function...
I'm having trouble calling the name of input button to the controller to delete an entire row of table..
model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Delete_model extends CI_Model{
public function delete($data){
$this->db->delete('record_tbl', $data);
}
}
controller
class Delete_controller extends CI_Controller {
public function index()
{
$this->delete();
}
public function delete()
{
$data = array();
$this->load->model('fetch');
$query = $this->fetch->getData();
if ($query)
{
$data['results'] = $query;
}
$this->load->view('delete', $data);
}
public function delete_data(){
$this->load->model('delete_model');
$where = $this->input->get('id');
$data['id'] = $where;
$this->delete_model->delete($data);
redirect('');
}
}
view
<table border="1" width="100%">
<tr>
<th>Name</th>
<th>Address</th>
<th>Phone Number</th>
<th>Status</th>
<th></th>
</tr>
<?php foreach($results as $row) { ?>
<tr>
<td><?=$row->name?></td>
<td><?=$row->addr?></td>
<td><?=$row->pnum?></td>
<td><?=$row->status?></td>
<?php echo form_open('delete_controller/delete_data') ?>
<td style="text-align: center"><input name="<?=$row->id?>" type="submit" value="delete"></input>
</form>
</td>
</tr>
<?php } ?>
</table>
Your form will be doing a POST rather than a GET request. So rather than doing
$where = $this->input->get('id');
You want to do
$where = $this->input->post('id');
Also why don't you pass the value of $row->id to your controller so that you know which input field to target. So in your view do
<?php echo form_open('delete_controller/delete_data/' . $row->id) ?>
Then in your controller
public function delete_data($id){
$this->load->model('delete_model');
$where = $this->input->post($id);
//...
It's going to be $this->input->post ('id') like #Pattle says and you are using the "name" attribute of the form for the ID. What I usually do is use a hidden field (see form_hidden () helper in CI documentation) with the name "id" and the value you want ($row->id).
<?php echo form_hidden ('id', $row->id); ?>
Instead of $this->input->get('id'); use $this->input->post('id');.
And to simplify things up: I probably recommend using <button></button> as control buttons like delete functionality (it also works on edit/update FYI).
Coz you can just add attribute to a button value = $id and name=delete. And it would be like an <input type='submit'> just by adding an attribute of type='submit'
So
VIEW
<table border="1" width="100%">
<tr>
<th>Name</th>
<th>Address</th>
<th>Phone Number</th>
<th>Status</th>
<th></th>
</tr>
<?php foreach($results as $row) { ?>
<tr>
<td><?=$row->name?></td>
<td><?=$row->addr?></td>
<td><?=$row->pnum?></td>
<td><?=$row->status?></td>
<?php echo form_open('delete_controller/delete_data') ?>
<td style="text-align: center">
<?php
echo "<button type='submit' name='delete' value='" . $row->id . "'>" ;
echo "DELETE" ;
echo "</button>" ;
?>
</form>
</td>
</tr>
<?php } ?>
</table>
Then it would be very simple to fetch the id of the row you want to delete. Just do this in your controller $id = $_POST['delete']; or $id = $this->input->post('delete');