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>
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>';
}
I am using Codeigniter 3 and trying CRUD operation. I have created the basic crud operation and am showing the data in a table however I have linked a paragraph tag in the controller below the table to the form controller, If I want to enter another data
The issue is when I click on the link to enter another data it redirect me the original form in controller but when I enter the data and submit it, The data is shown below the table in the paragraph tag.
I am not able understand why this is happening as the controller is the same
I had faced a similar issue before when redirecting in controller.I had redirected the page after submission to show_form() controller which was basically redirecting the page to $this->load->view('learn/view_form');
in which I have kept a condition that if No data is present click to enter. Now when it redirects to show_form() controller it goes into else condition even if the data is present
CONTROLLER
<?php
defined('BASEPATH') OR exit("No direct script access allowed");
class Learning extends CI_Controller{
public function __construct(){
parent::__construct();
$this ->load->helper("url");
$this->load->model("tatti_test");
$this->load->database();
$this->load->helper();
}
//functions should be passed here
//creating a function
function start_learn() {
//this varible
$this->load->view('learn/start_learn');
}
function start_crud(){
$this->load->view('learn/form');
}
function show_form(){
$this->load->view("learn/view_form");
}
function insert_form(){
$name = $this->input->post("u_name");
$email = $this->input->post("u_email");
$mobile = $this->input->post("u_mobile");
//File Uploading
$config['upload_path']="./assets/images/";
$config["allowed_types"]="gif|jpg|png";
$config['encrypt_name']=true;
$this->load->library("upload",$config);
if(!$this->upload->do_upload("u_file")){
$file='noimage.png';
}
else {
$filework = $this->upload->data();
$file =$filework['file_name'];
}
$data = array(
"name"=>$name,"email"=>$email,"mobile"=>$mobile,"file_name"=>$file
);
$this->tatti_test->insert_tatti($data);
redirect("learning/view_form");
}
function view_form(){
$data['returned_data']=$this->tatti_test->show_form();
$this->load->view("learn/view_form",$data);
}
function delete_entry(){
$id=$this->uri->segment(3);
$data=$this->tatti_test->for_unlink($id);
$filepath="./assets/images/".$data['file_name'];
unlink($filepath);
$this->tatti_test->delete_entry($id);
redirect('learning/view_form');
}
function time_to_update(){
$id=$this->uri->segment(3);
$data['fetched_update_entry']=$this->tatti_test->update_entry($id);
$this->load->view("learn/update.php",$data); //bus associative array hi leta hai
}
function up_db(){
$name =$this->input->post('up_name');
$email = $this->input->post('up_email');
$mobile = $this->input->post('up_mobile');
$file = $this->input->post('up_file');
$id = $this->input->post('up_id');
//File Uploading
$config['upload_path']="./assets/images/";
$config["allowed_types"]="gif|jpg|png";
$config['encrypt_name']=true;
$this->load->library("upload",$config);
if(!$this->upload->do_upload("up_file")){
$data= $this->tatti_test->remove_prev($id);
$file=$data['file_name'];
}
else {
$data= $this->tatti_test->remove_prev($id);
$path="./assets/images/".$data['file_name'];
unlink($path);
$filework = $this->upload->data();
$file =$filework['file_name'];
}
$data = array(
"name"=>$name,"email"=>$email,"mobile"=>$mobile,"file_name"=>$file
);
$this->tatti_test->up_nw($data,$id);
redirect('learning/view_form');
}
} /*this accesses command from main ci controller */
?>
VIEW
<?php $this->load->view("common/header.php");
if ($returned_data != 0){ ?>
<table border='1'>
<tr>
<th>Sr No</th>
<th>Name</th>
<th>Password</th>
<th>Mobile</th>
<th>Email</th>
<th>Final Name</th>
<th>Delete</th>
<th>View</th>
</tr>
<?php $i=0; foreach ($returned_data as $key=>$d){
?>
<tr>
<td>
<?php echo ++$i; ?>
</td>
<td>
<?php echo $d['name'];?>
</td>
<td>
<?php echo $d['mobile'];?>
</td>
<td>
<?php echo $d['email'];?>
</td>
<td>
<?php echo $d['file_name'];?>
</td>
<td>
<img src="<?php echo base_url().'/assets/images/'.$d['file_name'];?>" width="100px" ; height="100px" />
</td>
<td>Edit</td>
<td>Delete</td>
</tr>
</table>
<p>Add another entry
<?php echo anchor("learning/start_crud"," here "); ?>
</p>
<?php } ?>
<?php } else { ?>
<p>No data to show please click
<?php echo anchor("learning/start_crud"," here "); ?>to enter</p>
<?php } ?>
<?php $this->load->view("common/footer.php");
MODEL
<?php
class Tatti_test extends CI_Model{
function insert_tatti($insert_data){
$this->db->insert("f_form",$insert_data);
}
function show_form(){
$query = $this->db->get("f_form");
$response=[];
if ($query->num_rows() > 0){
$response = $query->result_array();
}
else {
$response = 0;
}
return $response;
}
function for_unlink($id){
$this->db->where("id",$id);
$query = $this->db->get("f_form");
$response=[];
foreach ($query->result_array() as $rows){
return $response = $rows;
}
}
function delete_entry($id){
$this->db->where("id",$id);
$this->db->delete("f_form");
}
function update_entry($id){
$this->db->where("id",$id);
$query = $this->db->get("f_form");
$response = [];
if($query->num_rows() > 0 ){
foreach($query->result_array() as $rows);
$response = $rows;
}
return $response;
}
function up_nw($introduced_data,$id){
$this->db->set($introduced_data);
$this->db->where('id',$id);
$this->db->update('f_form');
}
function remove_prev($id){
$this->db->where('id',$id);
$query = $this->db->get('f_form');
$response = [];
foreach($query->result_array() as $rows){
$response=$rows;
}
return $response;
}
}
?>
This is how the data is showing when clicked on the link below table
enter image description here
You're html formatting is messed up. You should have the closing </table> outside your foreach loop or premature <table> closure.
Also moved the Add another entry link outside the foreach loop. So it only appears once, and your document format not messed up.
You can use this fixed view instead:
<?php $this->load->view("common/header.php");
if ($returned_data != 0){ ?>
<table border='1'>
<tr>
<th>Sr No</th>
<th>Name</th>
<th>Password</th>
<th>Mobile</th>
<th>Email</th>
<th>Final Name</th>
<th>Delete</th>
<th>View</th>
</tr>
<?php $i=0; foreach ($returned_data as $key=>$d){
?>
<tr>
<td>
<?php echo ++$i; ?>
</td>
<td>
<?php echo $d['name'];?>
</td>
<td>
<?php echo $d['mobile'];?>
</td>
<td>
<?php echo $d['email'];?>
</td>
<td>
<?php echo $d['file_name'];?>
</td>
<td>
<img src="<?php echo base_url().'/assets/images/'.$d['file_name'];?>" width="100px" ; height="100px" />
</td>
<td>Edit</td>
<td>Delete</td>
</tr>
<?php } ?>
</table>
<p>Add another entry
<?php echo anchor("learning/start_crud"," here "); ?>
</p>
<?php } else { ?>
<p>No data to show please click
<?php echo anchor("learning/start_crud"," here "); ?>to enter</p>
<?php } ?>
<?php $this->load->view("common/footer.php");
I realize that you are trying to do CURD,
first of all, try this to improve your code and fill the missing library of codeigniter: https://github.com/avenirer/CodeIgniter-MY_Model
For your code:
No data passed to the view in show_form(),
You should check the form submission and the conditions to load the view,
simple thing to do is follow the best practice using ready scripts,
Hope this will be useful,
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'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.
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']);