Displaying data from database based on specific values Codeigniter 3 - database

I don't know how to explain this but i hope you can get the glimpse about my problem.
I have list of kode_mtk, if i click the button Input it will direct to view list of student who takes that subject.
I want to display that list of student based on kode_mtk and kelas. i tried to put $kode_mtk and $kelas in model and controller but it doesn't works.
I hope you can guide me well since i'm newbie in codeigniter.
This is my controller :
public function display(){
$data['nama'] = $this->session->userdata('username');
$kode_mtk = $this->input->post('kode_mtk');
$kelas = $this->input->post('kelas');
$data['hasil'] = $this->mtk_model->hasil();
$this->load->view('templates/header_tutor',$data);
$this->load->view('tutor/daftar_mtk_np', $data);
$this->load->view('templates/footer_tutor');
}
This is my model :
function hasil () {
$data = $this->session->userdata('username');
$kode_mtk = $this->input->post('kode_mtk');
$kelas = $this->input->post('kelas');
$this->db->select('
tb_kelas.idtutor,
tb_kelas.nim,
tb_kelas.nama,
tb_kelas.masa,
tb_kelas.kelas,
tb_kelas.idtutorial,
tb_mtk.kode_mtk,
tb_mtk.kode_program_studi,
prodi.nama_program_studi
');
$this->db->from('tb_kelas');
$this->db->join('tb_mtk', 'tb_mtk.idtutorial = tb_kelas.idtutorial');
$this->db->join('prodi', 'prodi.kode_program_studi = tb_mtk.kode_program_studi');
$this->db->where('tb_kelas.idtutor',$data);
$this->db->where('tb_kelas.masa',20172);
$this->db->where('tb_mtk.kode_mtk',$kode_mtk);
$this->db->where('tb_kelas.kelas',$kelas);
$query = $this->db->get();
return $query->result();
}
And here is my view
<div class="box-body table-responsive no-padding">
<div class="form-group">
<table class="table table-hover">
<tr>
<th>No</th>
<th>NIM</th>
<th>NAMA</th>
<th>KELAS</th>
<th>KODE MTK</th>
<th>PRODI</th>
<th>N.AKHIR</th>
<th>AKSI</th>
</tr>
<?php
if(isset($hasil)>0){$i=1; foreach($hasil as $row) {
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $row->nim; ?></td>
<td><?php echo $row->nama; ?></td>
<td><?php echo $row->kelas; ?></td>
<td><?php echo $row->kode_mtk; ?></td>
<td><?php echo $row->nama_program_studi; ?></td>
<td><input type="text" class="form-control na" id="na<?php echo $i; ?>" name="na" readonly /></td>
<td>Input Nilai<td>
</tr>
<?php $i++;}}?>
</form>
</table>
</div>
</div>

So, i found the mistakes. I changed $this->input->post to $this->input->get and put this in model. So in controller i just load $data['hasil'] = $this->mtk_model->hasil();
Thanks for your comments all :)

Related

i have connected two tables(Login and addresstable) and now i want to view data from Addresstable in My View in cake php

this is my controller function and I want to pass all data in my address_view
but the only Id is Passed When I die $xyz in my address_view.ctp
public function view_address($id = null)
{
$this->loadModel('Addresstable');
$data = $this->Addresstable->findAllByUserId($id);
$this->set('xyz',$id);
// pr($xyz);die;
$xyz=$this->data;
}
and this Is My view_address.ctp
<html>
<head>
</head>
<body>
<table>
<tr>
<td><?php pr($xyz);die; echo $xyz['Addresstable']['user_id']; ?></td>
</tr>
<tr>
<td>House No :<?php echo $xyz['Addresstable']['houseno']; ?></td>
</tr>
<tr>
<td>Block :<?php echo $xyz['Addresstable']['block']; ?></td>
</tr>
<tr>
<td>Street :<?php echo $xyz['Addresstable']['street']; ?></td>
</tr>
<tr>
<td> City :<?php echo $xyz['Addresstable']['city']; ?></td>
</tr>
<tr>
<td><td> State :<?php echo $xyz['Addresstable']['state']; ?></td></td>
</tr>
</table>
Error Message: Missing Controller Error: AddresstablesController could not be found. Error: Create the class AddresstablesController
below in file: app\Controller\AddresstablesController.php
you set data array wrong way,
Try this
public function view_address($id = null)
{
$this->loadModel('Addresstable');
$data = $this->Addresstable->findAllByUserId($id);
$xyz=$data;
$this->set(compact('xyz','id'));
}

Delete multiple rows by ticking Checkbox

How do I delete multiple rows by checking a checkbox?
I have a table of records, and I'd like to to delete multiple rows at once.
This is my my deleteselected function:
UPDATED
public function order() {
if(!empty($this->data)) {
//If at lesst 1 check box is selected
if (isset($this->data) && !empty($this->data['order_id'])) {
$selectedReferences = $this->data['order_id'];
$flagReferenceAdded = false;
foreach ($selectedReferences as $singleReference) {
//NEW DELETE
$this->Order->id = $singleReference;
$this->Order->delete();
//NEW DELETE
}
//After delete
$this->Session->setFlash(
__('Your All record Are deleted.')
);
return $this->redirect(array(''));
}
else{
echo "Select at least 1 ORDER.";
}
}
and in my view.
<?php
echo $this->Form->create('Order',array('url' => array('controller' => 'admin', 'action' => 'order'))); ?>
<table class="table table-bordered table-hover">
<th>Name</th>
<th>Email</th>
<th>phone</th>
<th>view</th>
<th>delete</th>
<thead>
</tr>
</thead>
<tbody>
<?php foreach ($order as $orders): ?>
<tr>
<td><?php echo $orders['Order']['varfullname']; ?></td>
<td><?php echo $orders['Order']['varemailid']; ?></td>
<td><?php echo $orders['Order']['varphone']; ?></td>
<td>
<?php echo $this->Html->link('',
array('controller' => 'orders', 'action' => 'view', $orders['Order']['id']), array('title'=>'VIEW','class' => 'fa fa-search'));?>
</td>
<td><input type="checkbox" name="order_id[]" value ="<?php echo $orders['Order']['id'];?>" hiddenField = "false">
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</br>
<?
echo $this->Form->end('Deleteselected'); ?>
</div>
</div>
</br>
</div>
<!-- /.row -->
after using below answer my delete selected working fine
Use in ctp
//in ctp Along with EACH record use Checkbox integration . Dont use Checkbox code u used.
<input type="checkbox" name="order_id[]" value ="<?php echo $referenceSingle['Order']['id'];?>" hiddenField = "false">
//In controller
if(!empty($this->data)) {
//If at lesst 1 check box is selected
if (isset($this->data) && !empty($this->data['order_id'])) {
$selectedReferences = $this->data['order_id'];
$flagReferenceAdded = false;
foreach ($selectedReferences as $singleReference) {
//NEW DELETE
$this->Order->id = $singleReference;
$this->Order->delete();
//NEW DELETE
}
//After delete
$this->Session->setFlash(
__('Your All record Are deleted.')
);
return $this->redirect(array(''));
}
else{
echo "Select at least 1 ORDER.";
}
}
UPDATE
Use Above Code in your Index method itself ,Dont create deleteAll method.
OR
Make <form action TO deleteAll
LIKE
echo $this->Form->create('searchCourse',array('url' => array('controller' => 'Courses', 'action' => 'index')));
UPDATE
For proper Workflow refer :
ALWAYS do paperwork and Algorithm ,Before Coding SIR.

Convert SQL query / loop into PDO

I am trying to get the PDO results displayed in a table via a loop. However presently It is just displaying a white page. Any suggestions to what is missing? or why the page is blank?
<?php
try { $conn = new PDO("pgsql:host=localhost port=5432 dbname=riverflies_gis", "opengeo", "opengeo");
echo "PDO connection object created";
echo "<br>";
}
catch(PDOException $e) {
echo $e->getMessage();
}
$sql = "SELECT admin_name, adminuser_id, group_id FROM adminuser_tbl";
$stmt = $conn->prepare($sql);
$stmt->execute();
$data = $stmt->fetchAll();
?>
<table border='1' cellpadding='10'>
<tr>
<th>ID</th>
<th>adminuser</th>
<th>group_id</th>
<th></th>
</tr>
<?php foreach ($data as $row): ?>
<tr>
<td><?=$row['adminuser_id']?></td>
<td><?=$row['admin_name']?></td>
<td><?=$row['group_id']?></td>
</tr>
<?php endforeach ?>
</table>
Please never mix database operations with HTML output.
PDO is especially good for it. Get your data first:
$sql = "SELECT admin_name, adminuser_id, group_id FROM adminuser_tbl";
$stmt = $conn->prepare($sql);
$stmt->execute();
$data = $stmt->fetchAll();
and then output it in a template:
<table border='1' cellpadding='10'>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th></th>
<th></th>
</tr>
<? foreach ($data as $row): ?>
<tr>
<td><?=$row['id']?></td>
<td><?=$row['firstname']?></td>
and so on
</tr>
<? endforeach ?>
</table>
see how nice and clean your output code can be

Implementing pagination in cakephp

I wish to implement pagination in my search results in the view page. Currently in the controller, I limit it to 10 results to view. How do I implement pagination?
In my controller,
var $name = 'Searches';
var $components = array('Auth');
var $uses = array('User','Passion');
$users = $this->User->find('all',array('limit'=>10,'conditions'=>$final_conditions,'fields'=>array('User.*')));
In my view page,
<?php foreach ($search_fields as $user): ?>
<tr>
<?php //debug($search_fields);?>
<td><?php echo $user['User']['firstName']; ?></td>
<td><?php echo $user['User']['lastName']; ?></td>
<td><?php echo $user['User']['email']; ?></td>
<td><?php echo $user['User']['displayName']; ?></td>
<td><?php echo $user['User']['gender']; ?></td>
</tr>
<?php endforeach; ?>
Controller
$this->paginate = array(
'conditions' => $final_conditions,
'limit' => 10
);
$users = $this->paginate('User');
In the view you just use the same as on http://book.cakephp.org/view/166/Pagination-in-Views

Returning extra fields with generatetreelist

I don't know if I'm going the right way with the tree behavior but I'm trying to build a comment system for a blog. I would like to have an indentation of 5 level depth.
The generatetreelist method looks like it would be the fastest way to accomplish this but it doesn't look like you can add fields to the query. Am I right ? Is there a way to modify the method ?
Thanks
This is how I do it for categories. You could modify accordingly.
In controller:
$categories = $this->Category->generatetreelist(null,null,null,"|-- ");
$categories_array = array();
foreach($categories as $k => $v)
{
$categories_array[$k] = $this->Category->find('first', array('conditions' => array('Category.id' => $k)));
$categories_array[$k]["Category"]["path"] = $v;
}
$this->set(compact('categories','categories_array'));
In view:
<table>
<thead>
<tr>
<th><?php __('Id');?></th>
<th><?php __('Name');?></th>
<th><?php __('Status');?></th>
<th><?php __('Action');?></th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
foreach ($categories_array AS $categoryId => $category):
$class = 'even';
if ($i++ % 2 == 0) {
$class = 'odd';
}?>
<tr class="<?php echo $class;?>">
<td><?php echo $category['Category']['id']; ?></td>
<td><?php echo $category["Category"]["path"];?></td>
<?php if ($category['Category']['status'] == '1'){
$published = 'Active';
}else {
$published = 'Inactive';
} ?>
<td><?php echo $published;?></td>
<td>
<?php echo $html->link($html->image("icons/16_icon_view.png"), array('action' => 'view', $category['Category']['id']), array('title'=>'View','escape' => false));?>
<?php echo $html->link($html->image("icons/16_icon_edit.png"), array('action' => 'edit', $category['Category']['id']), array('title'=>'Edit','escape' => false));?>
<?php echo $html->link($html->image("icons/16_icon_delete.png"), array('action' => 'delete', $category['Category']['id']), array('class'=>'delete_trigger','rel'=>'#error','title'=>'Delete','escape' => false));?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
If it was me, I would just do this in the result set. As you get a numeric array in the first dimension of the results you could use that when outputting the data to indent or add a class to your comments as you needed.

Resources