Cakephp parameters not passing - cakephp

I've spent a bunch of time trying to figure this problem out and had zero luck. I have tried routing with no success. I'll put the logic first then the code.
Users are in /pra/Fields/view/1 looking at a list of fields where fields.template_id=1
Users click add new field /pra/Fields/add_new/1 they are brought to a form where they enter the information they want to
about the new field they create.
when a user clicks on add/submit, the new field is saved to the database
The user is taken back to /pra/Fields/view/1 so they can see the new field added to the template
currently the first 3 steps are happening, however when it comes to redirecting back to /pra/Fields/view/1 The user is being redirected to /pra/Fields/view/
here is the code for the add_new function
function add_new($id=null){
//allows users to add another field to an existing template
$this->set('title_for_layout', 'Create Fields');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.png');
$this->layout='home_layout';
$name=$this->Field->field('template_id', array('template_id'=>$id));
//if the field data saves
$this->set('name',$name);
if(($this->Field->save($this->data)))
{
//user is redirected to fields/view/$name
$this->redirect(array('controller'=>'Fields', 'action'=>'view',$name));
}
//sets the $id variable
$this->set('id',$id);
here is the add_new view
<table id="formatform">
</br></br>
<?php
$options = array('Money'=>'Money','Text'=>'Text','Description'=>'Description');
?>
<?php echo $this->Form->create('Field', array('action'=>'add_new')); ?>
<?php echo $this->Form->input('id',array('type'=>'hidden')); ?>
<tr>
<td></td>
<td><?php echo $this->Form->input('template_id',array('type'=>'hidden','default' => $id)); ?></td>
</tr>
<tr>
<td align='center'>Field Name:</td>
<td align='left'><?php echo $this->Form->input('name', array('label'=>false)); ?></td>
</tr>
<tr>
<td align='center'>Default Value:</td>
<td align='left'><?php echo $this->Form->input('default_value', array('label'=>false)); ?></td>
</tr>
<tr>
<td align='center'>Field Type:</td>
<td align='left'><?php echo $this->Form->input('field_type', array('label'=>false,'type'=>'select','options'=>$options)); ?></td>
</tr>
<tr>
<td align='center'>Description:</td>
<td align='left'><?php echo $this->Form->input('description', array('label'=>false)); ?></td>
<td><?php echo $this->Form->input('active', array('type'=>'hidden','default'=>true)); ?></td>
</tr>
<td></td> <td align = "left"><?php echo $this->Form->end('Submit');?></td>
and here is the view function code
function view($name){
//lists information about fields that correspond to the
//template they have clicked 'view' on
$this->set('title_for_layout', 'Field Details');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.png');
$this->layout='home_layout';
//sets $conditions where template_id=$name and field.active=true
$conditions=array(
"AND"=>array(
'template_id'=> $name,
'Field.active'=>true));
//finds all fields where 'conditions'=$conditions
$fields = $this->Template->Field->find('all',array(
'conditions' => $conditions));
//sets all the variables
$this->set('conditions', $conditions);
$this->set('field', $fields);
$this->set('field', $this->paginate('Field', $conditions));
}

It might happen that you have an issue here:
function add_new($id=null){
//allows users to add another field to an existing template
$this->set('title_for_layout', 'Create Fields');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.png');
$this->layout='home_layout';
Check if $name is not empty
$name = $this->Field->field('template_id', array('template_id'=>$id));
if(!empty($name)){
$this->set('name',$name);
if(($this->Field->save($this->data)))
{
//user is redirected to fields/view/$name
$this->redirect(array('controller'=>'Fields', 'action'=>'view',$name));
}
}
the rest of your code
//sets the $id variable
$this->set('id',$id);

function add_new($id=null){
//allows users to add another field to an existing template
$this->set('title_for_layout', 'Create Fields');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.png');
$this->layout='home_layout';
$this->set('id',$id);
if(($this->Field->save($this->data)))
{
$id = $this->data['Field']['template_id'];
$this->set('id',$id);
$this->redirect(array('controller'=>'Fields', 'action'=>'view',$id));
}
}

Related

Displaying data from database based on specific values Codeigniter 3

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

cakephp, link to retrieve status variable

Total n00b in CakePhp
Im trying to link to the view retrieved data from variable $status, defined in the controller, given the simple condition "done" or "pending".
Given the model:
<?php
class Task extends AppModel {
var $name = 'Task';
var $validate = array(
'title' => array(
'rule' => 'notEmpty',
'message' => 'Title of a task cannot be empty'
)
);
}?>
The Task Controller:
function index($status=null) {
if($status == 'done')
$tasks = $this->Task->find('all', array('conditions' => array('Task.done' => '1')));
else if($status == 'pending')
$tasks = $this->Task->find('all', array('conditions' => array('Task.done' => '0')));
else
$tasks = $this->set('Tasks', $this->Task->find('all'));
$this->set('tasks', $tasks);
$this->set('status', $status);
}
And finally The View:
<h2>Tasks</h2>
<?php if(empty($Tasks)): ?>
There are no tasks in this list
<?php else: ?>
<table>
<tr>
<th>Title</th>
<th>Status</th>
<th>Created</th>
<th>Modified</th>
<th>Actions</th>
</tr>
<?php foreach ($Tasks as $Task): ?>
<tr>
<td>
<?php echo $Task['Task']['title'] ?>
</td>
<td>
<?php
if($Task['Task']['done']) echo "Done";
else echo "Pending";
?>
</td>
<td>
<?php echo $Task['Task']['created'] ?>
</td>
<td>
<?php echo $Task['Task']['modified'] ?>
</td>
<td>
<?php echo $this->Html->link('Edit', array('action'=>'edit', $Task['Task']['id'])); ?>
<?php echo $this->Html->link('Delete', array('action'=>'delete', $Task['Task']['id']), null, 'Are you sure you want to delete this task?'); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
<?php echo $this->Html->link('List Done Tasks', array('action'=>'index','done')); ?><br />
<?php echo $this->Html->link('List Pending Tasks', array('action'=>'index', 'pending')); ?><br />
Clicking the "Done Task" returns an empty list (There are no tasks in this list). Can anyone see what is wrong on the link? Thanks in advance!
You are mixing up the casing in the variable name $Tasks. In your example you are assigning the array to $tasks, but then in your view, your are trying to check a variable names $Tasks, which will turn up empty because it has never been properly set.
Change this line:
$this->set('tasks', $tasks);
To:
$this->set('Tasks', $tasks);
As mentioned by McWayWeb above to case of the variable is incorrect.
As you are setting lower case vars to use in your view i would make the change there. Change all counts of Task in the view to task.
I would also consider setting all viewVars at the same time using the below
$this->set(compact('tasks','status));

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.

Cakephp: pass parameter or data to another page

Our group create a database for patron to attend an event (booking) which has lots of entities.
We have three tables: patron, booking, booking_patron (join table).
We want to create 2 pages in the view (add.ctp and add2.ctp) of booking_patron.
Page add.ctp has a drop down box which lists booking ID and a submit button.
Page add2.ctp should appear the booking ID that we chose before (in add.ctp). Also in add2.ctp, we did make a function that user can create a patron and assign that patron to the booking ID.
BookingsPatronsController.php
public function add() {
$bookings = $this->BookingsPatron->Booking->find('list');
$this->set(compact('bookings', 'patrons'));
}
public function add2($booking_id = null) {
$patrons = $this->BookingsPatron->Patron->find('list');
$bookings = $this->BookingsPatron->Booking->find('list');
$this->set(compact('bookings', 'patrons'));
if ($this->request->is('post')) {
$this->BookingsPatron->Patron->create();
if ($this->BookingsPatron->Patron->save($this->request->data)) {
$this->Session->setFlash("Sign In Successful");
$this->BookingsPatron->create();
$this->Session->setFlash(__('Well.... done'));
$this->redirect(array('action' => 'add3'));
} else {
$this->Session->setFlash(__('We can not add your information. Please, try again.'));
}
}
}
The problem is: we don't know how to get the booking ID from add.ctp to add2.ctp.
At the moment, we use a drop down box that list booking ID in add2.ctp to choose the booking; we want to change that to a static text field that appears the booking ID from add.ctp.
We also have a function that can create a new patron in add2.ctp. We want to assign the new patron to the chosen booking ID from add.ctp
add.ctp
<?php echo $this->Form->input('Booking.booking_id');?>
<?php echo $this->Html->link(__('Create'), array('action' => 'add3')); ?>
add2.ctp
<?php echo $this->Form->create('BookingsPatron'); ?>
<fieldset>
<?php echo $this->Form->input('Booking.booking_id');?>
<table cellpadding="3" cellspacing="3">
<tr>
<td class="heading">Name: </td>
<td><?php echo $this->Form->input('Patron.patrons_name', array('label' => '', 'div' => 'formLabel'));?></td>
<td></td><td></td>
<td class="heading">E-mail: </td>
<td><?php echo $this->Form->input('Patron.patrons_email', array('label' => '', 'div' => 'formLabel'));?></td>
</tr>
<tr>
<td class="heading">Age: </td>
<td><?php echo $this->Form->input('Patron.patrons_age', array('label' => '', 'div' => 'formLabel'));?></td>
<td></td><td></td>
<td class="heading">Company: </td>
<td><?php echo $this->Form->input('Patron.patrons_company', array('label' => '', 'div' => 'formLabel'));?></td>
</tr>
<tr>
<td class="heading">Postcode: </td>
<td><?php echo $this->Form->input('Patron.patrons_postcode', array('label' => '', 'div' => 'formLabel'));?></td>
<td></td><td></td>
<td class="heading">Gender: </td>
<td><?php echo $this->Form->input('Patron.patrons_gender', array('label' => '', 'div' => 'formLabel', 'type' => 'select', 'options' => array('Male' => 'Male', 'Female' => 'Female')));?></td>
</tr>
<tr>
<td colspan="2">PH: 1300 7 34726</td>
<td colspan="3"></td>
<td><?php echo $this->Form->submit('Submit', array('class' => 'classSubmitButton', 'title' => 'Sbumit')); ?></td>
</tr>
</table>
</fieldset>
This should work
public function add() {
if ($this->request->is('post')) {
$this->Session->write('booking_id', $this->request->data['Booking']['booking_id']);
$this->redirect(array('action' => 'add2')); // Line added
}
$bookings = $this->BookingsPatron->Booking->find('list');
$this->set(compact('bookings', 'patrons'));
}
And to retreive the ID use this in your add2() function
$booking_id = $this->Session->read('booking_id');
Update
edit add.ctp -- This needs to be a form, not just a link, otherwise you don't submit the booking ID
<?php
echo $this->Form->create('Booking');
echo $this->Form->input('Booking.booking_id');
echo $this->Form->end(__('Create'));
?>

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