I am trying to use the count function , this is what it looks like :
$sql = "SELECT COUNT(Live) as c FROM tapplicant WHERE CompletedDate >= CURDATE() ";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo $row['c'] ;
How do i conver this using the cakePHP way ? , i have tryed :
$count = $this->Article->find('count',
array('conditions' => array('Tapplicant.Live')));
Then to view the value of $count :
<?php echo $count ?>
I have tryed :
$this->Tapplicant = array(
'c' => 'COUNT(*)',
);
$options = array(
'fields' => array(
'Tapplicant.c',
),
);
$data = $this->find('all', $options);
$this->set('data', $data );
Basically im just trying to count the value of tapplicant.Live , there are 5 records in it.
You almost have it. In your condition, the value array should have 2 fields. 1st the column name, 2nd the value of the condition.
Considering you've binded correctly the Tapplicant table to your Article model :
$count = $this->Article->find('count', array('conditions' => array('Live >=' => 'CURDATE()' )));
Or just this, if you want to count all lines :
$count = $this->Article->find('count');
You need to set the conditions array like this:-
$count = $this->Article->find(
'count',
array(
'conditions' => array(
'Live >=' => 'CURDATE()'
)
)
);
Related
I have a taxonomy called "fachbereiche". First I load the taxonomies of the current page:
<?php $term_list = wp_get_post_terms($post->ID, 'fachbereiche', array("fields" => "all", 'parent' => '0'));
foreach($term_list as $thisslug)
{
$output = $thisslug->slug;
echo $output;
?>
The current page has the taxonomy slugs: "bauelemente" and "baumarkt". The echo $output returns bauelementebaumarkt.
Now I want to find all posts of a custom post type "marken" with the same taxonomies as we got above ("bauelemente" and "baumarkt"), so I load the following query:
<?php
$loop = new WP_Query(
array(
'post_type' => 'marken',
'post_status'=>'publish',
'posts_per_page'=>-1,
'orderby'=> 'title',
'order'=>'ASC',
'tax_query' => array(
array(
'taxonomy' => 'fachbereiche',
'field' => 'slug',
'terms' => array($output)
),
),
)
);
}
?>
The query returns only the posts with the taxonomy for "baumarkt". I think because the variable $output returns bauelementebaumarkt. I think that you have to seperate "bauelemente" and "baumarkt". Please have in mind that there can be more than 2 terms or just 1.
Your $output should be an array instead of string, so add this before foreach:
$output = array();
Then inside foreach you should do this:
$output[] = $thisslug->slug;
And finally in tax_query it should be like this:
'terms' => $output,
I think I have my dumber-than-usual head on today.
I am trying to parse text from a textbox in a form to generate an array that I can pass to the find method in cakePHP.
I have used php regular expressions to generate an array of the terms entered (including "phrases included in speech marks" as single item)
This may look like this:
array(
(int) 0 => 'test',
(int) 1 => 'this out',
(int) 2 => 'now')
In the cakePHP cookbook it says that I have to get an array that looks like this (using above example):
array(
array('ProjectDocumentSectionVariable.variable_description LIKE' => '%test%'),
array('ProjectDocumentSectionVariable.variable_description LIKE' => '%this out%'),
array('ProjectDocumentSectionVariable.variable_description LIKE' => '%now%'))
Try as I might, I end up with arrays that do NOT look like an array of arrays.
Typically I am ending up with this horrible looking thing:
array(
(int) 0 => array(
'ProjectDocumentSectionVariable.variable_description LIKE' => '%test%'
),
(int) 1 => array(
'ProjectDocumentSectionVariable.variable_description LIKE' => '%this out%'
),
(int) 2 => array(
'ProjectDocumentSectionVariable.variable_description LIKE' => '%now%'
))
The code for the above is:
$i = 0;
foreach($matches[1] as $match) :
$searcharray['ProjectDocumentSectionVariable.variable_description LIKE'] = "%" . $match . "%";
array_push($array_full,$searcharray);
$i++;
endforeach;
THis must be a common requirement, so I am hoping one of you guys could put me on the straight an narrow...
Thanks
If you're trying to build a search query (not clear what your goal is), your conditions should all be in the same array within an OR or AND - something like this:
$searchResults = $this->find('all', array(
'conditions' => array(
'OR' => array(
'ProjectDocumentSectionVariable.variable_description LIKE' => '%test%',
'ProjectDocumentSectionVariable.variable_description LIKE' => '%this out%',
'ProjectDocumentSectionVariable.variable_description LIKE' => '%now%'
)
)
));
If you want to build it out in a foreach() loop, you can do it something like this:
$conditions = array('OR' => array());
$matches = array('test', 'this out', 'now');
foreach($matches as $match) {
array_push($conditions['OR'], array('variable_description LIKE' => '%' . %match . '%'));
}
$searchResults = $this->find('all', array(
'conditions' => $conditions
));
its as simple as this:
$array= array();
foreach ( $colors as $c):
$uniques[] = $c;
endforeach;
not having the [] will treat it as a regular variable. This will append it to the array
I have a query in CakePHP as following :
lnguserID = 10;
$result = $this->Mymodel->find('all', array(
'fields' => array('Mymodel.intPhoneID'),
'conditions' => array('Mymodel.intUserid'=> $lnguserID)
));
When I debug the result like this: echo debug($result);
I get :
array(
(int) 0 => array(
'Mymodel' => array(
'intPhoneID' => (int) 3975
)
)
)
How can I access directly to this id : 3975 from the resulted array? Something like :
result['Mymodel']['intPhoneID'];
I want to use it in an other query.
echo $result[0]['Mymodel']['intPhoneID'];
Also - you don't need to echo a debug. It's just:
debug($result[0]['Mymodel']['intPhoneID']);
I assume this is in the Controller - if so, you can then access it in the View by "'set'ing" it:
// Controller
$this->set('result', $result);
// View
debug($result);
if this is your object:
array(
(int) 0 => array(
'Mymodel' => array(
'intPhoneID' => (int) 3975
)
)
)
you need to access the array by 0 -> Mymodel -> intPhoneID , so use:
result[0]['Mymodel']['intPhoneID'];
I've set a simple search engine on my CakePHP project which looks like that :
<?php
echo $this->Form->create("Post", array(
"action" => "search",
"id" => "searchForm"
));
echo $this->Form->input("keyword", array(
"label" => "",
"type" => "search",
"placeholder" => "Recherche..."
));
echo $this->Form->end();
?>
Here is the controller :
function search() {
$keyword = $this->request->data;
$keyword = $keyword["Post"]["keyword"];
$cond = array("OR" => array(
"Post.title LIKE '%$keyword%'",
"Post.description LIKE '%$keyword%'"
));
$posts = $this->Post->find("all", array("conditions" => $cond));
$this->set(compact("posts", "keyword"));
}
And it works great. The only problem is when I want to paginate the results. I simply add :
$posts = $this->paginate();
And here is the problem. When I add this, CakePHP give me all the posts and not only the ones that match the keyword.
So, if you would have a solution, it would be nice :)
According to the CakePHP book you should be able to do
$this->paginate('Post', array(
'OR' => array(
'Post.title LIKE' => "%$keyword%",
'Post.description LIKE' => "%$keyword%"
)
));
Or you can do it like this ( from the cakephp site ).
public function list_recipes() {
$this->paginate = array(
'conditions' => array('Recipe.title LIKE' => 'a%'),
'limit' => 10
);
$data = $this->paginate('Recipe');
$this->set(compact('data'));
);
Source:
http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html
Paginate does its own lookup on the data I believe. The find you are calling previously has no effect on the paginate.
Try this:
$this->paginate = array( 'conditions' => $cond, ));
you can use Session for storing conditions
the first, when you submit form you store conditions into Session
and then(paginate) you can read conditions from Session
example i want to search the products:
Products/search.ctp
<?php
echo $this->Form->create('Product');
echo $this->Form->input('keyword');
echo $this->Form->end(__('Search'));
?>
ProductsController.php
<?php
class ProductsController extends AppController{
public function search() {
if ($this->request->is('post')) {
$keyword = $this->request->data['Product']['keyword'];
$this->paginate = array(
'fields' => array('Product.name', 'Product.price', 'Product.created'),
'order' => array('Product.created' => 'DESC', 'Product.price' => 'DESC'),
'limit' => 30,
'conditions' => array('Product.name LIKE' => '%' . $keyword . '%')
);
// store array $this->paginate into Session
$this->Session->write('paginate', $this->paginate);
}
$this->paginate = $this->Session->read('paginate');
$this->set('products', $this->paginate('Product'));
}
}
I have a start and an end date in my database and a $date variable from a form field. I am now trying to query all the rows where $date is either = start/end date in the db, or ANY date between those two.
It's kind of the opposite of what is described in the docs of how daysAsSql works. I can't figure out how to get it to work. The following line does not work as a find condition in the controller:
'? BETWEEN ? AND ?' => array($date, 'Item.date_start', 'Item.date_end'),
Any help is greatly appreciated. This is driving me crazy.
Here is the complete Query and corresponding SQL:
$conditions = array(
'conditions' => array(
'and' => array(
'? BETWEEN ? AND ?' => array($date, 'Item.date_start', 'Item.date_end'),
'Item.title LIKE' => "%$title%",
'Item.status_id =' => '1'
)));
$this->set('items', $this->Item->find('all', $conditions));
WHERE (('2012-10-06' BETWEEN 'Item.date_start' AND 'Item.date_end') AND (`Item`.`title` LIKE '%%') AND (`Item`.`status_id` = 1))
$conditions = array(
'conditions' => array(
'and' => array(
array('Item.date_start <= ' => $date,
'Item.date_end >= ' => $date
),
'Item.title LIKE' => "%$title%",
'Item.status_id =' => '1'
)));
Try the above code and ask if it not worked for you.
Edit:
As per #Aryan request, if we have to find users registered between 1 month:
$start_date = '2013-05-26'; //should be in YYYY-MM-DD format
$this->User->find('all', array('conditions' => array('User.reg_date BETWEEN '.$start_date.' AND DATE_ADD('.$start_date.', INTERVAL 30 DAY)')));
Here is CakePHP BETWEEN query example.
I'm defining my arrays as variables, and then using those variables in my CakePHP find function call:
// just return these two fields
$fields = array('uri', 'page_views');
// use this "between" range
$conditions = array('Event.date BETWEEN ? and ?' => array($start_date, $end_date));
// run the "select between" query
$results = $this->Event->find('all',
array('fields'=>$fields,
'conditions'=>$conditions));
Ref from
General Example For CakePHP 2.x Query
$_condition = array("TABLENAME.id" => $id, "TABLENAME.user_id" => array_unique($_array), 'date(TABLENAME.created_at) BETWEEN ? AND ?' => array($start_date, $end_date));
$result_array = $this->TABLENAME->find("all", array(
'fields' => array("TABLENAME.id", "TABLENAME.user_id", 'TABLENAME.created_at'),
"conditions" => $_condition,
"group" => array("TABLENAME.id"), //fields to GROUP BY
'joins' => array(
array(
'alias' => 'T2',
'table' => 'TABLENAME2',
'type' => 'LEFT',
'conditions' => array('TABLENAME.t_id = TABLENAME2.t_id')
),
array(
'alias' => 'T3',
'table' => 'TABLENAME3',
'type' => 'LEFT',
'conditions' => array(
'IF(
TABLENAME.t3_id > 0,
T2.f_id = T3.f_id,
TABLENAME.ff_id = T2.ff_id
)'
)
),
),
'recursive' => 0
)
);
This is more efficient and understandable IN BETWEEN query in cakephp 2.x
$testing_log_device_site_name = $testingLogData['TestingLogDevice']['Siteid'];
$conditions = array('TestingLogDevice.dateee BETWEEN ? and ?' => array($start_date, $end_date));
$results = $this->TestingLogDevice->find('all',
array(
'fields'=>array('dateee','timeee','Siteid'),
'conditions'=>array($conditions, 'TestingLogDevice.Siteid'=>$testing_log_device_site_name)
)
);
pr($results);
$data=$this->post->find('all')->where([ 'id'=>$id,
'price between'=>$price1,'and'=>$price2])->toArray();
This query works as following:
select * from post where id=$id and price between $price1 and $price2;
" -- 'price between'=>$price1 --" become "price between $price1"
Use this
$today = new DateTime( date('Y-m-d'));
$fiveYearsBack = $today->sub(new DateInterval('P5Y'));