I have a system that is outputting a number of images, with a A link next to them to set them as the album cover.
I have multiple albums, and in my database have a field called "is_feature" that is set to 1 if the image is the cover, and 0 if it isnt.
I don't know the best way of selecting the image, i originally outputted something like below;
Set
(image_id is the images id obviously), this function would call the model and set all other photos "is_feature" field to 0, and this photos "is_feature" to 1.
The problem is it is wiping all the other album features as well. I almost need to pass to variables in the A link, the first being the id of the image, the second being the id of the album, then my model function can only set "is_feature" to 0 where album_id = the id of the album passed.
Is there anyway to pass two variables like this? Or am i going about this in totally the wrong way?
You can set the values in the URL as query parameters
<a href="/admin/set_photo?var1=<?= $image_id;?>&var2=<?= $size;?>"
title="Set this photo as the feature photo"> Set </a>
Which you can retrieve in the controller
$image_id = $this->input->get('var1');
$image_size = $this->input->get('var2');
Uh what? You can pass whatever you need.
$data = array(
'title' => 'My Title',
'heading' => 'My Heading',
'message' => 'My Message'
);
$this->load->view('blogview', $data);
Depending upon data type as string or as array, there are 3 ways of passing data (You can use any of them explained below, BASED upon YOUR REQUIREMENT):
Through View
//For data you collected through POST method from FORM, collect them as array
$data=array(
'employee_name' => $this->input->post('emp_name'),
'employee_email' => $this->input->post('emp_email'),
'employee_password' => $this->input->post('emp_password')
);
$this->load-> view(''mynextpage", $data)
Through Controller
redirect('controller_name/index'.$valueofcustomer);
OR
redirect(base_url()."controller_name/index/".$valueofcustomer);
//then in your 'view', you can access value of customer like this:
$v_o_c = $this->uri->segment(3);
echo "your value is " .$v_o_c;
Through Session
$data = array(
'user_name' => $user_name,
'is_logged_in' => true
);
$this->session->set_userdata($data); //set the session
redirect('another_controller/index');
//then access those in another_controller like this:
$in = $this->session->set_userdata('$data');
Note: Session data will available only for redirect and lost on next page request
Related
Hi I am new to php and mysql and jumped right into using the Lithium framework.....I know, bad idea.
BUT basically my question is, how do I go from;
$users = \app\models\Users::findByUrl($this->request->query['url']);
return array(
'users' => $users,
);
in my UsersController to actually echo'ing 1 specific row of data in my users/index.php.html page, according to the query string in my url?
My desired structure is localhost/users/"url", and "url" is a value in my mysql db labeled "url" in my Users table. e.g. localhost/users/Bob. "Bob" is "url" value.
My users/index.php.html page is using foreach ($users as $user) to echo the information like this;
<?=$user->firstname ?>
Snag and Confusion:
When I was using;
$users = \app\models\Users::find('all');
return array(
'users' => $users,
);
in my UsersController, the users/index.php.html page retrieved all the users in my database.
The content displays correctly, BUT I need the data from just the clicked-on user, so this is when I started using; $users = \app\models\Users::findByUrl($this->request->query['url']);
When linking to the users page this is how I do it;
<?php if( $user->avatar ):?>
<a href="/users/<?=$user->url?>">
<img class="img-responsive" src="/img/users/<?=$user->avatar?>" />
</a>
<?php else: ?>
Not sure if I need to add conditions to my controller, add something to my router, change my htaccess, or do something with params.... Pleease help, any direction would be greatly appreciated.
Finders return everything by default. If you want a single object, do:
Users::findFirstByUrl($this->request->query['url'])
...which is short-hand for:
Users::first(['conditions' => ['url' => $this->request->query['url']]]) or
Users::find('first', ['conditions' => ['url' => $this->request->query['url']]]).
Assume I'm in my items controller.
Ok say I am in my view action (the url would be something like /items/view/10012?date=2013-09-30) which lists a list of items that belongs to a client on a given date.
I want to link to add a new item. I would use the htmlhelper like so:
echo $this->Html('action'=>'add');
In my add action I have a form which has fields like client_id and item_date.
When I'm in my view action I know these values as I am viewing the items for a specific client on a specific date. I want to pass these variables to my add action so it will prefill those fields on the form.
If I add a query string in my link ('?' => array('client_id'=>$client_id)) it breaks the add action as it will give an error if the request is not POST. If I use a form->postLink I get another error as the add action's POST data must only be used for adding the record, not passing data to prefill the form.
I basically want to make my link on the view page pass those 2 variables to the add action in the controller so I can define some variables to prefill the form. Is there a way to do this?
Here is my add controller code. It may differ in content a bit from my question above as I have tried to simplify the question a bit but the concept should still apply.
public function add(){
if ($this->request->is('post')) {
$this->Holding->create();
if ($this->Holding->save($this->request->data)) {
$this->Session->setFlash(__('Holding has been saved.'), 'default', array('class' => 'alert alert-success'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to add your holding.'), 'default', array('class' => 'alert alert-danger'));
}
$this->set('accounts', $this->Holding->Account->find('list'));
$sedol_list = $this->Holding->Sedol->find('all', array(
'fields' => array(
'id', 'sedol_description'
),
'recursive' => 0,
'order' => 'description'
)
);
$this->set('sedols', Hash::combine($sedol_list, '{n}.Sedol.id', '{n}.Sedol.sedol_description') );
}
Why not use proper Cake URL parameters?
echo $this->Html->link('Add Item', array(
'action' => 'add',
$client_id,
$item_date
));
This will give you a much nicer URL like:
http://www.example.com/items/add/10012/2013-09-30
And then in your controller, you modify the function to receive those parameters:
public function add($client_id, $item_date) {
// Prefill the form on this page by manually setting the values
// in the request data array. This is what Cake uses to populate
// the form inputs on your page.
if (empty($this->request->data)) {
$this->request->data['Item']['client_id'] = $client_id;
$this->request->data['Item']['item_date'] = $item_date;
} else {
// In here process the form data normally from when the
// user has submitted it themselves...
}
}
I have a small site which allows a user to enter values in a form and then either submit it directly or store the field values in a template to later submit it. To submit the form later, he can load the previously saved template. For that there are three buttons Load Template / Save Template / Submit form.
Because i am using the form validation built-in functionality from Codeigniter i run into problems when i want to populate the form with a template, which had been previously stored.
The form fields are all set up like
$name = array(
'name' => 'name',
'id' => 'name',
'value' => set_value('name', $form_field_values['name'])
);
The variable $form_field_values holds the values from either a loaded template in the case when a template has been loaded or the default values when the form is first loaded.
Initially the form is loaded with the default values. When i click on Load Template the values from the template are not chosen by set_value() because there were the default values in there before. What i want is to replace the values of the form fields with the ones from the template.
Do you have any idea how to do that in a clean approach? What i have done is to introduce a variable to skip the call to set_value() completely like:
$name= array(
'name' => 'name',
'id' => 'name',
'value' => $skip_form_validation ? $form_field_values['name'] : set_value('name', $form_field_values['name'])
);
Where $skip_form_validation is a variable set in the controller, based on what button was pressed. Form validation is skipped for saving/loading a template.
Codeigniter's set_value() function is a simple function which finds value in $_POST if value found then return else returns second argument, you can remove set_value() and write your own code for it. you can write $_POST['field_name'] if you want to populate value of POST data or add whatever value you want to add
Just use like this
$name = array(
'name' => 'name',
'id' => 'name',
'value' => $valueFromYourTemplate
);
You don't need to use set_value() function if you don't want to set POST values in the form
Assuming you retrieve the database fields and pass them to a data array in your controller.
$record = $this->data_model->get_record(array('uid' => $user_id), 'users');
if (!is_null($record)) {
$data['uname'] = $record->username;
$data['loc'] = $record->location;
}
where 'users' is the database table, and the uid is the id field of the table users.
In your form, do something like this
Hope it helps!
So, I'ld like to add a "Like" system on something I'm working on and I have a bit of trouble with it.
So my DB's table for my likes is like this :
========================================
|| *id* | created | user_id | post_id ||
========================================
I'm with with counting the number of likes for a post and all. The problem I have is I want to find if the logged user has already liked the post is is viewing so the "Like" link would become an "Unlike" link. And that's my problem. I can't manage to imagine how to do it.
Any help ?
Look at Model::hasAny() method:
$userHasLikedThisPost = $this->Like->hasAny(array(
'user_id' => $this->Auth->user('id'),
'post_id' => $postId
));
Then simply set a view var and output the corresponding link.
Make a helper method in the model so its more re-usable:
public function hasLike($postId, $userId) {
return $this->hasAny(array(
'user_id' => $userId,
'post_id' => $postId,
));
}
I am trying to build in a "search" box on a results page in my cakephp app. The page uses the cakePHP pagination component to show and "page" results. This is perfect, but I am having difficulties to get the next part to work.
The desired outcome:
A cakephp form (post) with an input box and a couple of select boxes, including a date selector so that I can select between dates. The user should be able to populate these fields and submit
On submit, the user selection should change the cakePHP pagination conditions in the controller
In the view I want the pagination bar to keep record of the user selection, so that when I filter through different pages, it keeps the users search. I understand this can be achieved using $this->passedArgs, hence why I am using post and not get.
The code:
// Form:
<?php
echo $this->Form->create('search', array('class' => false));
echo $this->Form->input('searchFor');
echo $this->Form->input('dateFrom');
echo $this->Form->input('dateTo');
echo $this->Form->end();
?>
// Controller:
if($this->request->is("post")) {
$filters = $this->request->data["search"];
$this->passedArgs["searchFor"] = $filters["searchFor"];
$this->passedArgs["dateFrom"] = $filters["dateFrom"]." 00:00:00";
$this->passedArgs["dateTo"] = $filters["dateTo"]." 00:00:00";
// Assign search parameters:
if($this->passedArgs["searchFor"] != "") {
$conditions["Model.field LIKE"] = "%".$this->passedArgs["searchFor"]."%";
}
$conditions["Model.created >="] = $this->passedArgs["dateFrom"];
$conditions["Model.created <="] = $this->passedArgs["dateTo"];
} else {
$conditions = array("Result.status_id >=" => 12);
}
$this->paginate = array(
'conditions' => $conditions,
'order' => array('Result.created ASC'),
'limit' => 20
);
$this->set("results",$this->paginate("Model");
// The view file:
<?php
$this->Paginator->options(array('url' => $this->passedArgs));
?>
Where I am now:
The initial page loads with all of the results
When I populate the search boxes it does return my results
The problem:
I am convinced the way I am doing it is incorrect as I now need to do 2 checks, a) being if results has been posted and b) check if there is passedArgs available. I am 100% convinced this is not the right way of doing it.
Let's say I have 2 free form fields for search, say name and surname, if I leave surname blank my url would be written as below, and this does not look or appear to be correct. That means I have to assign default values to ensure the items below does not happen, which does not appear to be very dynamic.
http://localhost/site/controller/action/surname:0/name:John/date:0/
On refresh it says the page does not exist because the posted values is not available anylonger.
usually I proceed like this in the controller:
//transform POST into GET
if($this->request->is("post")) {
$url = array('action'=>'index');
$filters = array();
if(isset($this->data['searchFor']) && $this->data['searchFor']){
//maybe clean up user input here??? or urlencode??
$filters['searchFor'] = $this->data['searchFor'];
}
//redirect user to the index page including the selected filters
$this->redirect(array_merge($url,$filters));
}
$conditions = array();
//check filters on passedArgs
if(isset($this->passedArgs["searchFor"])){
$conditions["Model.field LIKE"] = "%".$this->passedArgs["searchFor"]."%";
}
//paginate as normal
$this->paginate = array(
'conditions' => $conditions,
'order' => array('Result.created ASC'),
'limit' => 20
);
The idea is to transform the POST sent by your form into GET. so you wont have problems with the paginator nor the refresh
Hope this helps
What you want can be done a lot more simple and DRY by using this search plugin.
It automates what you want more or less plus it already can do more than your code.
So I suggest you to use the plugin directly or take a look at it how it does the trick. :)