I am trying to apply filters to employees to show them just on the timesheet table. If other code is required to help you help me, I'll start posting it out. The code is filter.ctp
http://webdesign4.georgianc.on.ca/~100141468/comp2084/todo/employees
Problem: http://webdesign4.georgianc.on.ca/~100141468/comp2084/todo/timesheets/filter/19
<table>
<tr>
<th>ID</th><th>name</th><th>Hours</th><th>Employees</th><th>Edit</th>
</tr>
<? foreach($timesheets as $row): ?>
<tr><td>
<?=$row['Timesheet']['id']?>
</td><td>
<?=$row['Timesheet']['hours']?>
</td><td>
<?=$row['employee']['name']?>
</td><td>
Edit
</td></tr>
<? endforeach; ?>
</table>
<?
class TimesheetsController extends AppController {
var $name = 'Timesheets';
var $scaffold;
var $helpers = array('Number');
function add() {
//check if user loaded form for first time or had already loaded & is now posting
if ($this->request->is('post')) {
//save the data to the db
if ($this->Timesheet->save($this->request->data)) {
//Build message & add to session
$this->Session->setFlash('Timesheet has been saved');
//take user back to the index page
$this->redirect(array('action'=>'index'));
}
//else save did not work
else {
$this->Session->setFlash('Unable to add Timesheet');
}
}
//populate the Employee list
$this->loadModel('Employee');
$this->set('Employees',$this->Employee->find('list',array('order'=> array('Employee.name'))));
$this->set(compact('Employees'));
//populate the Employee list
$this->loadModel('Client');
$this->set('Clients',$this->Client->find('list',array('order'=> array('Client.name'))));
$this->set(compact('Clients'));
}
//create function to filter Timesheet list by selected Employee
function filter($Employee_id) {
$this->set('Timesheets',$this->Timesheet->findAllByEmployeeId($Employee_id));
}
//create function to filter Timesheet list by selected Employee
function filter2($Client_id) {
$this->set('Timesheets',$this->Timesheet->findAllByClientId($Client_id));
}
}
?>
You may need to set the variable $timesheets on your controller using $this->set('timesheets', $timesheets)
Related
I have a problem with my shopping cart system using cakephp + bootstrap modal. The problem is that when I click/select one of my images, it will add to the cart but it will always display the last item/data in my database. Even though I'v selected the first item, it will still display the last item/data in my database. Please help me out of this.
CartsController.php
class CartsController extends AppController {
public $uses = array('Sidedish','Cart');
public function add() {
$this->autoRender = false;
if ($this->request->is('post')) {
$this->Cart->addProduct($this->request->data['Cart']['product_id']);
}
echo $this->Cart->getCount();
}
public function view() {
$carts = $this->Cart->readProduct();
$side_dishes = array();
if (null!=$carts) {
foreach ($carts as $productId => $count) {
$side_dish = $this->Sidedish->read(null,$productId);
$side_dish['Sidedish']['count'] = $count;
$side_dishes[]=$side_dish;
}
}
$this->set(compact('side_dishes'));
print_r($carts);
print_r($side_dishes);
}}
views/Orders.ctp -> this is where I will need to click the item to display in the cart.
<div class="col-sm-12">
<?php echo $this->Form->create('Cart',array('id'=>'add-form','url'=>array('controller'=>'carts','action'=>'add')));?>
<ul class="list-inline">
<?php foreach ($side_dish as $sidedish):?>
<li>
<?php echo $this->Form->hidden('product_id',array('value'=>$sidedish['Sidedish']['sidedish_id'])); ?>
<?php
echo $this->Form->submit(//$sidedish['Sidedish']['sidedish_id'],
'',array(
'name'=>'submit',
'style'=>'height:130px;width:200px;'
. 'background-repeat:no-repeat;'
. 'border:none;'
. 'background-image:url(/webapp' .$sidedish['Sidedish']['sidedish_img']. ')'));
?>
<h5 class="text-center"><?php echo $sidedish['Sidedish']['sidedish_name'];?></h5>
<h5 class="text-center">$<?php echo $sidedish['Sidedish']['sidedish_price'];?></h5>
</li>
<?php endforeach;?>
</ul>
<?php echo $this->Form->end();?>
</div>
Some people told me that the problem is the foreach loop in my views/orders.ctp and some people told me that the problem is in the controller. I'm not sure where is the problem here. please need help guys.
Can you please show your code for this function $this->Cart->addProduct()?
<?php
App::uses('AppModel', 'Model');
App::uses('CakeSession', 'Model/Datasource');
class Cart extends AppModel {
public $useTable = false;
/*
* add a product to cart
*/
public function addProduct($productId) {
$allProducts = $this->readProduct();
if (null!=$allProducts) {
if (array_key_exists($productId, $allProducts)) {
$allProducts[$productId]++;
} else {
$allProducts[$productId] = 1;
}
} else {
$allProducts[$productId] = 1;
}
$this->saveProduct($allProducts);
}
/*
* get total count of products
*/
public function getCount() {
$allProducts = $this->readProduct();
if (count($allProducts)<1) {
return 0;
}
$count = 0;
foreach ($allProducts as $product) {
$count=$count+$product;
}
return $count;
}
/*
* save data to session
*/
public function saveProduct($data) {
return CakeSession::write('cart',$data);
}
/*
* read cart data from session
*/
public function readProduct() {
return CakeSession::read('cart');
}
}
I am having a problem. Please let me know if there is a solution. I am new to codeigniter therefore, sorry in advance if there is a silly one!
I am trying to fetch data from a database. Table name is fw_main_cat which have fields (cat_id, cat_parent_id, cat_level, cat_title, cat_menu_order and cat_status). cat_id is unique.
I want a dropdown menu (in view) to take all the data of a column cat_title through cat_level (which are int). So, how can I do?
Here is my code which I have tried so far.
This is Model :
public function cat_level_one($cat_level)
{
$sql = "Select * from fw_main_cat Where cat_level=? ";
$result = $this->db->query($sql, $cat_level);
if($result->num_rows() > 0)
{
return $result->result_array();
}
else { return false;
}
}
This is Controller :
public function getcategory()
{
if ($this->session->userdata('session_status'))
{
$cat_level_one = $this->admin_cat_model->cat_level_one($cat_level);
$data['cat_level_one'] = $this->admin_cat_model->cat_level_one($cat_level);
$this->session->set_userdata('cat_level', $_POST['cat_level']);
$this->laod_view('admin_view/admin_cat/view_add_category', $data);
}else {
redirect ('admin_view/admin_cat/view_category');
}
}
This is View (dropdown menu):
<li class="full-row">
<select name = 'cat_level_one' id = 'cat_level_one'>
<option value="<?php if(isset ($cat_level_one) && $cat_level_one != ''){ foreach ($cat_level_one as $cat_one){ echo $cat_one->cat_id; } } ?>"
selected="selected">------------Select Category------------</option>
Thanks! for the consideration.
Updated code -
// in model
function cat_level_one($cat_level)
{
$this->db->where('cat_level',$cat_level);
$q = $this->db->get('fw_main_cat');
$data = $q->result_array();
return $data;
}
//controller function
public function getcategory()
{
**if ($this->session->userdata('session_status'))
{
$data['cat_level_one'] = $this->admin_cat_model->cat_level_one($cat_level);**
//Where this post data comes from??
$this->session->set_userdata('cat_level', $_POST['cat_level']);
$this->laod_view('admin_view/admin_cat/view_add_category', $data);
}else {
redirect ('admin_view/admin_cat/view_category');
}
}
//in view
<li class="full-row">
<select name = 'cat_level_one' id = 'cat_level_one'>
<option value="">-- Select Category --</option>
<?php foreach($cat_level_one as $cat_one){ ?>
<option value="<?php echo $cat_one->cat_id; ?>"><?php echo $cat_one->cat_title; ?></option>
<?php } ?>
</select>
</li>
I'm trying to build a small blog with CodeIgniter, and for some reason I put form database entries 0 instead of strings. I tried to put it through the controller manually and it works. What's the problem in a form?
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class site_model extends CI_Model {
var $title = '';
var $content = '';
var $date = '';
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function get_records()
{
$query = $this->db->get('posts');
return $query->result();
}
function add_records($data) {
$this->db->insert('posts', $data);
return;
}
function updata_records($data) {
$this->db->where('postID', $id);
$this->db->updata('posts', $data);
}
function delede_roe() {
$this->db->where('postID', $this->uri->segment(3));
$this->db->delete('posts');
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
the form page:
<div class="grid_11">
<h2>About</h2>
<?php echo form_open('site/creata'); ?>
<label for="title"> title </label>
<input type="text" name="title" id="title">
<label for="content"> content </label>
<input type="text" name="content" id="content">
<br>
<input type="submit" value="send">
<?php echo form_close(); ?>
</div>
the controler:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class site extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$this->load->view('home');
}
function add_post() {
$this->load->view('add_post');
}
function creata() {
$data = array (
'title' => $this->input->post('title'),
'post' => $this->input->post('content')
);
$this->load->model('site_model');
$this->site_model->add_records($data);
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
Your controller function should look something like this:
function creata() {
if( $this->input->post(null) ){ //enter if the form is submitted
$data = $this->input->post(null, true); // get all the data of the post
print_r( $data );die; // see the post data
$this->load->model('site_model');
$this->site_model->add_records($data);
$this->session->set_flashdata('msg', 'A msg to show to the user about the insertion');
redirect('site/creata', 'refresh');
}else{ //load the form
$this->load->view('home');
}
}
You form looks okay to me tho. You model may contain one extra line to view the query.
function add_records($data) {
$this->db->insert('posts', $data);
$this->db->last_query(); die; //check the query it's executing.
return;
}
I've been runing on my issue all night long tried many method to update an entry of User model and I didn't figure it out.
I tried with saveField, updateAll and findBy then save but all i've got is new entry on my DB or no update.
Here is the code, Hope you'll help
<?php
public function email_verification($token) {
$this->User->create();
$user = $this->User->findByEmailToken($token);
$this->User->set(array('email_token' => 'valid'), $user);
$this->User->save($user, false);
}
?>
Thanks in advance
Ahum:
$this->User->set(array('email_token' => 'valid'), $user);
$this->User->save($user, false);
The save() call saves your record with your original data..... since you pass in $user!
Replace the call to $this->user->set() with:
$user['User']['email_token'] => 'valid';
Try code below:
<?php
public function email_verification($token) {
$user = $this->User->findByEmailToken($token);
$user['User']['email_token'] = 'valid';
if ($this->User->save($user)) {
....
....
} else {
debug($this->User->validationErrors);
}
}
?>
I'm learning cakephp from here codes are from the older version of cake so some of the code needs to be updated I have controller which pass data to a view or a layout here is the controller:
BlogController.php
<?php
class BlogController extends AppController {
var $name = 'Blog';
var $uses = array('Blog');
// Used when indexing the page (http://yourdomain.com/blog/)
function index($view = null) {
// What to do if a view is set
if (isset($view))
{
//problem is here
$this->set('article', $this->Blog->find("id = $view"));
$this->render('article');
}
else
{
$this->set('articles', $this->Blog->find('all'));
}
}
}
?>
the problem is this line $this->set('article', $this->Blog->find("id = $view"));
if I replace the line with $this->set('article', $this->Blog->find('first')) it will show me the first item always and nothing will go wrong how can I correct this line so that I can use id?
the layout is article.ctp as follow
<div id="article">
<h1><?= $article['Blog']['title'] ?></h1>
<p class="date"><?= $article['Blog']['date'] ?></p>
<p class="intro"><?= $article['Blog']['introtext'] ?></p>
<p class="main"><?= $article['Blog']['maintext'] ?></p>
</div>
here is the error that I will get by clicking one of items:
Notice (8): Undefined index: id = 2 [CORE\Cake\Model\Model.php, line 2666]
You probably want this:
$this->set('article', $this->Blog->find('first', array('conditions' => array('Blog.id' => $view))));
See: Retrieving Your Data - CakePHP Manual