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.
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>
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'));
I have good command over core PHP, and I have just started learning CodeIgniter. I have created some pages according to the CodeIgniter's Tutorial. But I am stuck in this tutorial: http://www.codeigniter.com/user_guide/tutorial/news_section.html
Following is the code:
/application/config/routes.php :
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['users/(:any)'] = 'users/view/$1';
$route['users'] = 'users';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
/application/models/Users_model.php :
class Users_model extends CI_Model
{ public function __construct()
{ $this->load->database();
}
public function get_users($username = FALSE)
{ if ($username === FALSE)
{ $query = $this->db->get('users');
return $query->result_array();
}
$query = $this->db->get_where('users', array('username' => $username));
return $query->row_array();
}
}
/application/controllers/Users.php :
class Users extends CI_Controller
{ public function __construct()
{ parent::__construct();
$this->load->model('users_model');
$this->load->helper('url_helper');
}
public function index()
{ $data['users'] = $this->users_model->get_users();
$data['title'] = 'List of Users';
$this->load->view('templates/header', $data);
$this->load->view('users/index', $data);
$this->load->view('templates/footer');
}
public function view($username = NULL)
{ $data['user'] = $this->users_model->get_users($username);
if (empty($data['user']))
{ show_404();
}
$data['title'] = $data['user']['display_name'];
$this->load->view('templates/header', $data);
$this->load->view('users/view', $data);
$this->load->view('templates/footer');
}
}
/application/views/users/index.php :
<div class='main'>
<table border='1'>
<tr>
<th>Name</th>
<th>Email Address</th>
<th>Username</th>
</tr>
<?php foreach ($users as $user) { ?>
<tr>
<td><?php echo $user['display_name'] ?></td>
<td><?php echo $user['email'] ?></td>
<td>#<?php echo $user['username'] ?></td>
</tr>
<?php } ?>
</table>
</div>
/application/views/users/view.php :
<h2><?php $user['display_name'] ?></h2>
<p>#<?php $user['username'] ?></p>
MySQL 'users' table's structure :
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
email VARCHAR(255),
username VARCHAR(255),
display_name VARCHAR(50)
);
(You can replace .... (4 dots) with your desired domain name OR localhost in the below code.)
Output of ..../index.php/users page :
<html>
<head>
<title>List of Users</title>
</head>
<body>
<h1>List of Users</h1>
<div class='main'>
<table border='1'>
<tr>
<th>Name</th>
<th>Email Address</th>
<th>Username</th>
</tr>
<tr>
<td>Nikunj Bhatt</td>
<td>nikunj#example.com</td>
<td>#NikunjBhatt</td>
</tr>
</table>
</div>
</body>
</html>
This output is proper, but when I click on the username, it is redirecting to ..../index.php/users/NikunjBhatt page and this page is showing the error "This webpage is not available" "ERR_CONNECTION_REFUSED" in Google Chrome.
So, where is the problem? What I am missing?
Try to write code as below:-
public function view($username = ""){
// do your stuff here
}
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')