cakephp making a front page - cakephp

Im trying to make a front page for a website using cake.
I want to display a random advertisment (adverts listed in advertisements) table. A user has many adverts.
How to i go about doing this with cakephp?

You can randomly retrive your advertisements from mysql
$this->loadModel('Advertisement');
$advertisments = $this->Advertisement->find('all',
'order' => 'rand()');
and use $advertisments as an array in your frontpage.

Just get all Adverts belonging to your user with $data = $this->find('all');
Then use array_rand() to get a random entry out of your $data array.
Further reading:
http://php.net/manual/de/function.array-rand.php

Related

How to create a lookup in CakePHP

I'm in the process of converting a 10 year old PHP application. After my boss hired a php consultant, he has set up a CakePHP application environment and we are learning as we go. (fun, I know). Also, I come from a javascript/sharepoint background and have not had a lot of php experience.
As a test, I created a basic address table with these fields: firstname, lastname, state, phonenumber. I've been using justice league members as names and other test data to populate my table. Baked it just fine, default bootstrap pages are working.
I decided I wanted to add a dropdown field called current status, and for now just to keep it simple I wanted the choices: alive, dead.I created the column in my address table.
I created a second table called statuses and pointed the status column in my first table to the status table, using the status id as the foriegn key.
Baked my new table and rebaked my old one.
The status drop down does not give my choices of dead or alive, If I click in the field I get an up or down arrow, and based on which one you click, it either increments or decrements by 1. So the first time I click it inserts a 0. If I go up or down, it adds or takes away one.
I'm not sure what I'm doing wrong, I'm guessing there is some additional code I need to add to the MVC?
ok, if this works, then a lot is working :-). Now to the following: set in the Status Model a query like this:
public function getStatus()
{
$opt = $this->Status->find('list', array(...));
return $opt;
}
Then get the list over to the Adress Controller like this:
$this->loadModel('Status');
$opt => $this->Status->getStatus();
$this->set('opt', $opt);
Now you are able to access the $opt in the view file.
Just delete this line in the view:
$opts = array('0' => __('dead'), '1' => __('alive'));
And it should work.
Keep it simple. Ad to your table this row (only to understand how it works): 'status' as typ "tinyint(1)". Then set this in your view file:
$opts = array('0' => __('dead'), '1' => __('alive'));
When you create the inputfield, do it like that:
echo $this->Form->input('Address.status', array('options' => $opts, 'label'
=> __('Status')));
This should work.

how to post values in cakephp 2.x and show the filtered records in another page

I have designed page in cakephp contains form which input filed name 'id' and submit button. I want to display the data filtered by the 'id' in view page. Please give me an example with code for this.
So you wanna search into a database for this ID and return back all stored data to the view?
First of all,you have to retrieve your data. See here: http://book.cakephp.org/2.0/en/models/retrieving-your-data.html
Secondly, you have to use
$this->set('var_name', $var_name_containing_found_data);
and finally you can handle your data into the view by manipulating as you wish the $var_name variable.
Put this action in your controller:
public function your_action($id = null) {//your action
if ($this->request->is('post')) {
$search = $this->YourModel->find('all', array('conditions' => array('id' => $this->request->data['id'])));//assuming id is submitted like you said $_POST['id']
$this->set('search', $search);
}
}
You can access the search variable in your view (your_view.ctp) like this $this->search
assuming your cakephp version is 2.x

CakePHP hasMany checkbox

I have two tables: Ingredients and Customers. The relationship between them is that Customers hasMany Ingredients. By default when doing the cakebake using the console, the only way to change them is by assigning an ingredient to the customer in the Ingredients page. However, I want to have in Customers page a checkbox list of Ingredients that can be assigned. Is it possible to do this? If yes, how?
edit:
What I have done until now is that I add this code to my add.ctp:
echo $this->Form->input('Ingredient',
array('label'=>'',
'type'=>'select',
'multiple'=>'checkbox',
'options'=>$ingredients));
However, it gives me "Undefined variable: ingredients" error when I tried to open the add view.
You want and need a HABTM relationship. Different customers can access and use the same ingredients. Look at the docs here yours would be very similar to different Posts using same Tags.
If you are getting this error:
"Undefined variable: ingredients"
It sounds like you haven't declared this variable in your controller, and set it so that the view can use it. Without knowing your code, you would probably need do do something like this (please note I am guessing what your application structure looks like and have not tested this code).
CustomersController.php
// The controller action for your view
public function view() {
// Get the ID and name of all your ingredients
$ingredients = $this->Ingredient->find('all', array(
'fields' => array('id', 'name'),
'order' => 'name',
'recursive' => -1
));
// We will use this array to store all the HTML select options
$ingredientOptions = array();
// Loop through all the ingredients and add them to the select
// options in a format that is suitable for CakePHP to use in
// the view to build your HTML select menu.
foreach ($ingredients as $i) {
$ingredient = $i['Ingredient'];
$ingredientOptions[$ingredient['id']] = $ingredient['name'];
}
// Make the variable available to the view
$this->set('ingredients', $ingredientOptions);
}

How to retrieve the cakephp used query in find or read

Usually we check the find operation by checking the sql-dump. But it is not possible if query operation done through ajax. How can we check any find operation whick cakePhp done in backgoround and Any way to print it in pr()?
If you give $this->layout='ajax' in the controller code, then it would not show the query.So if you want to view the query , just set the layout to your normal layout (only for testing) and check the query and then revert the code.
So for finding queries you have to follow these steps
1- Now go to cake folder and find this path cake\libs\model\datasources\dbo\dbo_mysql.php
2- find the function _execute. This function is responsible for all the queries what cakephp execute from its functions.
3- debug the $sql variable like
function _execute($sql) {
echo $sql;
return mysql_query($sql, $this->connection);
}
4- die the code after the cakephp background retrieval functions. like
$referral = $this->find('first', array(
'conditions' => array('Referral.id' => $id)
));
die();
5- If this is ajax call for that you can view the queries in firebug.
Note: The last displayed query will be your desire query before die

CakePHP Containable on Assoicated Model

I have a setup where there are Books Users and Logs. Each Log has a Book and a User and I am trying to retrieve a list of all the books regardless of the user, but then also retrieve the Logs associated with that book (recursive is set to 1), but then only retrieving the logs of the current logged in user.
Hopefully that's clear. I tried using Containable and like so:
$this->Book->contain('Log.user_id = 2');
But unfortunately this leaves out Books for which User 2 has no logs for. Am I going about this correctly and I'm just not using containable properly, or am I doing this all wrong.
Any help is appreciated
$this->Book->Behaviors->attach('Containable');
$this->Book->find('all', array(
'contain' => array(
'Log.user_id' => 2
)
)
);

Resources