Codeigniter DATABASE Trouble - php

The thing is when i use this code
in my model :
public function get_all_subject_tasks(){
$this->db->select('*');
$this->db->from('task');
$this->db->where("user_id",$this->session->userdata('user_id'));
$this->db->order_by("task_id", "desc");
$query_result=$this->db->get();
$result=$query_result->result();
return $result;
}
in my controller:
public function subjects($t,$action=""){
$data=array();
$data['subject_tasks']=$this->Schoolmodel->get_all_subject_tasks($t);
if($action=='asyn'){
$this->load->view('theme/task',$data);
}else{
$this->load->view('theme/include/header');
$this->load->view('theme/include/school_sidebar');
$this->load->view('theme/task',$data);
$this->load->view('theme/include/footer');
}
}
in my php page:
<div class="panel-body">
<table id="teacher_table" class="table table-striped table-bordered table-condensed">
<th>Name</th><th><?php get_phrase('teacher_email') ?></th><th width="110"><?php get_phrase('action') ?></th>
<?php foreach($subject_tasks as $list){ ?>
<tr>
<td class="task_name"><?php echo $list->task_name ?></td>
<td class="task_desc"><?php echo $list->task_desc ?></td>
</tr>
<?php } ?>
</table>
</div>
I get all of the tasks that are in the database, without any subject filter.
So my question is
How do i make the page echo the tasks based on which subject they are in?
Also here is how i have setup my database structure
subject_id
task_id
task_name
task_desc
user_id
Where subject_id is the id of the subject where the task is inserted in.

<?php
// your model
public function getTask($subject_id)
{
$result = $this->db
->where("subject_id", $subject_id) // !!!
->where("user_id", $this->session->userdata('user_id')) // !!!
->order_by("task_id", "desc")
->get('tasks');
return $result->num_rows() > 0 ?
$result->result() : false;
}
// your class
class School extends CI_Controller {
public function __construct() {
parent::__construct();
if($this->session->userdata('logged_in')==FALSE){
redirect('User');
}
$this->load->database();
$this->load->model('Schoolmodel');
$this->load->library('form_validation');
}
public function subjects($subject_id,$action=""){
$data=array();
$data['subject_tasks'] = $this->Schoolmodel->getTask($subject_id);
if($action=='asyn'){
$this->load->view('theme/task',$data);
}else{
$this->load->view('theme/include/header');
$this->load->view('theme/include/school_sidebar');
$this->load->view('theme/task',$data);
$this->load->view('theme/include/footer');
}
}
}
?>
// your view
<div class="panel-body">
<table id="teacher_table" class="table table-striped table-bordered table-condensed">
<th>Name</th><th><?php get_phrase('teacher_email') ?></th><th width="110"><?php get_phrase('action') ?></th>
<?php if(false !== $subject_tasks) { foreach($subject_tasks as $list){ ?>
<tr>
<td class="task_name"><?php echo $list->task_name ?></td>
<td class="task_desc"><?php echo $list->task_desc ?></td>
</tr>
<?php } } ?>
</table>
</div>

This is not going to work:
$this->db->where("subject_id",$subject_id AND "user_id",$this->session->userdata('user_id'));
From Active Record documentations: Multiple calls to where will result in AND:
$this->db->where("subject_id", $subject_id);
$this->db->where("user_id", $this->session->userdata('user_id'));

Related

To get a link from database

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>

Codeigniter: How to avoid displaying duplicated values or overwrite a duplicated value in PHP foreach loop in a table?

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')

Passing row variables to a function in Codeigniter

I'm trying create a function where in when you click on a person's name, a list of trainings he/she has attended will show up on another page. My problem right now is how to pass the user_id from each row so that I can use it for my query.
Below is my code so far
Controller:
public function traineeRater()
{
if(!$this->session->userdata('loginuser'))
{
redirect(base_url());
}
if($this->session->userdata('level') != 'Rater')
{
$this->session->sess_destroy();
redirect(base_url());
}
$data['trainee'] = $this->rater_model->traineeList();
$this->load->view('rater/trainee_rater', $data);
}
public function trainingAttended()
{
if(!$this->session->userdata('loginuser'))
{
redirect(base_url());
}
if($this->session->userdata('level') != 'Rater')
{
$this->session->sess_destroy();
redirect(base_url());
}
$data['attended'] = $this->rater_model->getAttended(//some variable here?);
$this->load->view('rater/attended', $data);
}
Model:
public function traineeList()
{
$query = $this->db->query("SELECT dept_name, position, user_id,
CONCAT(fname, ' ', mname, ' ', lname) AS name
FROM users, department
WHERE users.dept_id = department.dept_id
");
return $query->result();
}
public function getAttended($id)
{
$query = $this->db->query("SELECT training_title, immediate_rater,
CONCAT(fname, ' ', mname, ' ', lname) AS name
FROM users, training_details, coach
WHERE users.user_id = coach.user_id
AND training_details.dept_id = coach.dept_id
AND users.user_id = '".$id."'");
return $query->result();
}
View(traineeList):
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Name</th>
<th>Department</th>
<th>Position</th>
</tr>
</thead>
<tbody>
<?php foreach ($trainee as $row)
{
?>
<tr class = "odd gradeX">
<td><?=$row->name;?></td>
<td><?=$row->dept_name;?></td>
<td><?=$row->position;?></td>
</tr>
<?php
}
?>
</tbody>
</table>
View(trainingAttended):
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Training Title</th>
<th>Immediate Rater</th>
<th>Coach</th>
</tr>
</thead>
<tbody>
<?php
foreach ($attended as $row)
{
?>
<tr class = "odd gradeX">
<td><?=$row->training_title;?></td>
<td><?=$row->immediate_rater;?></td>
<td><?=$row->name;?></td>
</tr>
<?php
}
?>
</tbody>
</table>
Try like this,
<td><?=$row->name;?></td>
In href make sure you add controller/method/parameters
In controller
public function trainingAttended($id)
^// capture id here
{
if(!$this->session->userdata('loginuser'))
{
redirect(base_url());
}
if($this->session->userdata('level') != 'Rater')
{
$this->session->sess_destroy();
redirect(base_url());
}
$data['attended'] = $this->rater_model->getAttended($id);
^// send here
$this->load->view('rater/attended', $data);
}
on your view where you click on a person's name
Name of person
Then on you're Controller:
public function trainingAttended()
{
if(!$this->session->userdata('loginuser'))
{
redirect(base_url());
}
if($this->session->userdata('level') != 'Rater')
{
$this->session->sess_destroy();
redirect(base_url());
}
$id = $this->uri->segment('3');
$data['attended'] = $this->rater_model->getAttended($id);
$this->load->view('rater/attended', $data);
}
""
Hope it works.

Button in view doesn't call method in controller

i have a problem : I have a view that needs to call a method in my controller EmployeeController to go to the page to add an employee. But it don't work.. also with other views it doe
This is the view:
<body>
<form class="form-horizontal" id='employeeform' id='employeeform' action="<?php echo base_url();?>EmployeeController/addEmployee" method="post">
<div class="container">
<div class="row">
<div class="col-lg-12 col-sm-12">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>#</th>
<th>ID</th>
<th>Naam werknemer</th>
<th>Datum aanwerving</th>
<th>Salaris</th>
</tr>
</thead>
<tbody>
<?php for ($i = 0; $i < count($employeelist); ++$i) { ?>
<tr>
<td><?php echo ($i+1); ?></td>
<td><?php echo $employeelist[$i]->employee_no; ?></td>
<td><?php echo $employeelist[$i]->employee_name; ?></td>
<td><?php echo $employeelist[$i]->hired_date; ?></td>
<td><?php echo $employeelist[$i]->salary; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<input id="btn_add" name="btn_add" type="submit" class="btn btn-primary" value="Add employee" />
</div>
</div>
</div>
</form>
</body>
THIS IS THE CONTROLLER:
class EmployeeController extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->helper('form');
$this->load->helper('url');
$this->load->database();
$this->load->library('form_validation');
//load the employee model
$this->load->model('employee_model');
}
//index function
function index() {
$employeelist = $this->employee_model->get_employee_list();
$data['employeelist'] = $employeelist;
$this->load->view('employee/employee_add', $data);
}
function createEmployee() {
$this->form_validation->set_rules('employee_no', 'Employee No', 'trim|required|numeric');
$this->form_validation->set_rules('employee_name', 'Employee Name', 'trim|required|xss_clean|callback_alpha_only_space');
$this->form_validation->set_rules('hired_date', 'Hired Date', 'required');
$this->form_validation->set_rules('salary', 'Salary', 'required|numeric');
if ($this->form_validation->run() == FALSE) {
//fail validation
$this->load->view('employee/employee_add');
} else {
//pass validation
$data = array(
'employee_no' => $this->input->post('employeeno'),
'employee_name' => $this->input->post('employeename'),
'hired_date' => #date('Y-m-d', #strtotime($this->input->post('hireddate'))),
'salary' => $this->input->post('salary'),
);
//insert the form data into database
$this->db->insert('tbl_employee', $data);
//display success message
$employeeresult = $this->employee_model->get_employee_list();
$data['employeelist'] = $employeeresult;
//load the department_view
$this->load->view('employee/employee_view', $data);
redirect('employee/employee_view');
}
}
function addEmployee() {
$this->load->view('employee/employee_add');
//set validation rules
}
}
?>
THE ERROR I GET IS :
The requested URL /BoMaTec_afstudeerproject/CodeIgniter-3.0.1/EmployeeController/createEmployee was not found on this server.
Seems like you don't have any router for that url, or the router is wrong. As per the error, it's trying to load a folder named EmployeeController and within that folder, another folder named createEmployee. Check your .htaccess file and your routers in the framework you are using.

how to paginate a table in zend framework?

I'm using zend framework and want to paginate the table of contents based on the data received from the MySQL table. I want to show only 10 rows in the html table.
here is the .phtml code i have..
<div class="panel-body">
<?php
if(isset($this->ErrorMessage))
echo "<div class='alert alert-danger'>" . $this->ErrorMessage . "</div>";
if(isset($this->success))
echo "<div class='alert alert-success'>" . $this->success . "</div>";
echo $this->form;
?>
<!-- / .table -->
<div class="table-responsive">
<table class="table table-striped table-hover dataTable table-bordered no-footer" role="grid">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php if(isset($this->results)) { ?>
<?php foreach($this->results as $result) { ?>
<tr>
<td><?php echo $result->id; ?></td>
<td><?php echo $result->name; ?></td>
</tr>
<?php } ?>
<?php } ?>
</tbody>
</table>
</div>
</div>
so how can i add the pagination...
You don't state whether you want to paginate on the client or server side. If you want to paginate on the client side, the easiest way is to just use JQuery DataTables:
https://www.datatables.net/
Example:
$(document).ready(function() {
$('.dataTable').DataTable();
} );
First you will need to use Paginator class. Let' say that you already have a factory class which passess an instance of tablegateway adapter to your modeltable class. Let's create a function to retreive all the data
public function fetchAll()
{
$dbSelect = new DbSelect($select, $this->tableGateway->getAdapter(), $this->tableGateway->getResultSetPrototype());
return new Paginator($dbselect);
}
In your controller you will do something simillar to this.
public function indexAction()
{
$this->view->setTemplate("application/index/index");
$paginator = $this->getServiceLocator()->get("myModelTable")->fetchAll();
$paginator->setCurrentPageNumber((int) $this->params("page", 1));
$paginator->setItemCountPerPage(10);
$this->view->paginator = $paginator;
return $this->view;
}
In your view part put this somewhere (usually at the bottom of the file)
echo $this->paginationControl($this->paginator, 'sliding', 'application/pagination.phtml', ['route' => 'application']);

Categories