I am trying to calculate distance between two geographical locations. one location is entered by user.
User entered location is calculated by model and is stored in the variable.
$ulatitude=$userLocation['Location']['latitude'];
$ulongitude=$userLocation['Location']['longitude'];
I have used below code to calculate the distance.
$this->User->virtualFields['lowestDistance'] = 'round(3959 * acos(cos(radians($ulatitude)) * cos(radians(Profile.latitude )) * cos(radians(Profile.longitude) - radians($ulongitude)) + sin(radians($ulatitude)) * sin(radians(Profile.latitude)))) ';
$order = "User.lowestDistance";
$options = array(
'fields' => '
User.*, Profile.*,
round(3959 * acos(cos(radians($ulatitude)) * cos(radians(Profile.latitude )) * cos(radians(Profile.longitude) - radians($ulongitude)) + sin(radians($ulatitude)) * sin(radians(Profile.latitude)))) AS lowestDistance,
',
'order' => array($order =>'asc'),
);
Above code throws error : unknown column $ulatitude.
Is there any other way of doing this ?
Single quotes are not interpreted
Consider:
<?php
$foo = "A VARIABLE";
echo 'foo is $foo' . "\n";
echo "foo is $foo" . "\n";
This would output:
-> php example.php
foo is $foo
foo is A VARIABLE
In the question everything is in single quotes, they are literal strings.
Fields should be an array
Also note that this:
'fields' => 'User.*, Profile.*, ...
Is normally an array, which makes more sense when there's multiple values:
'fields' => [
'User.*',
'Profile.*',
...
]
But since you're declaring all fields plus a virtual field, it'd be included in a find call anyway - just remove the fields from the find parameters:
$this->User->virtualFields['lowestDistance'] = "round(...";
$options = [
//'fields' => [], Not necessary
'order' => [$order =>'asc']
];
$withLowestDistance = $this->User->find('all', $options);
Related
I can't understand the proposed solutions in other questions or resolve the php error Uninitialized string offset: 0 .
I used this code to create acronyme from a string
$article = 'the Central intelligence agency';
$words = array('the', 'of', 'to' );
$pattern = '/\b(?:' . join('|', $words) . ')\b/i';
$entityWords = preg_replace($pattern, '', $article);
$words = explode(" ", $entityWords);
var_dump($words);
$entityAcro = "";
foreach ($words as $letter) {
-> $entityAcro .= $letter[0];
}
echo strtoupper( $entityAcro );
I get this result :
Notice: Uninitialized string offset: 0 ....line (->)
Notice: Uninitialized string offset: 0 ....line (->)
CIA
I don't understand this solution : "you must know if it exist (isset)"
I manage multiple organizations and I can't know before how many word there are inside string.
Issue resolved with this solution:
foreach ($words as $letter) {
if(!empty($letter[0])){
$entityAcro .= $letter[0];
}
}
I had to verify the existence of each word $letter[0] in the array before showing the result of the foreach condition
How can I concatenate two fields inside a select box using cakephp 2.5 and Hash:combine ?
Today I have name:
$banks = Hash::combine($banks, '{n}.Bank.id', '{n}.Bank.name');
I need name and identifier, I tried this:
$banks = Hash::combine($banks, '{n}.Bank.id', '{n}.Bank.name {n}.Bank.identifier');
But it return NULL.
How can I have : name - identifier ?
Also try to concatenate the two fields at the model but could not add an hiffen between name and identifier.
You can provide array’s for both $keyPath and $valuePath. If you do this, the first value will be used as a format string, for values extracted by the other paths:
$result = Hash::combine(
$a,
'{n}.User.id',
array('%s: %s', '{n}.User.Data.user', '{n}.User.Data.name'),
'{n}.User.group_id'
);
/* $result now looks like:
Array
(
[1] => Array
(
[2] => mariano.iglesias: Mariano Iglesias
)
[2] => Array
(
[14] => phpnut: Larry E. Masters
)
)
*/
$result = Hash::combine(
$a,
array('%s: %s', '{n}.User.Data.user', '{n}.User.Data.name'),
'{n}.User.id'
);
/* $result now looks like:
Array
(
[mariano.iglesias: Mariano Iglesias] => 2
[phpnut: Larry E. Masters] => 14
)
*/
See CookBook > Hash for mode details.
Below cakephp query returns an empty array result but if I use only $max_results it works.
I want to search for a record from two limits (0,10).
This doesn't work.
$from = 0;
$max_results = 10;
$this->Model->find('all',array(
'conditions'=>$condition,
"order"=>'Model.id DESC',
'limit'=>"$from,$max_results"
));
You need to use offset options-
$from = 0;
$max_results = 10;
$this->Model->find('all',array(
'conditions'=>$condition,
"order"=>'Model.id DESC',
'limit'=>"$max_results",
'offset' => "$from"
));
Hi all I have a dropdown box on a view. I have a find statement that returns a list of templates, when I debug the find, it prints out the correct list of template.id and template.name however when I submit the form it carries across the number it is in the list for example if I select the 5th template in a list it saves the template_id = 5 NOT the actual template id number.
//Retrieve Account Id of current User
$accountid=$this->Auth->user('account_id');
//conditions for template arrays
$conditions=
array('AND' => array(
array('Template.account_id' => $accountid),
array('Template.active' => 1)
));
//An array of all Templates belonging to the current User's Account
$templates=$this->Template->find('list', array(
'conditions' => $conditions));
when I debug $templates I get this print out
array(
(int) 1 => 'Consulting- Bus',
(int) 2 => 'Consulting- Pers',
(int) 7 => 'ClientEdge',
(int) 8 => '11111',
(int) 9 => 'testSUn',
(int) 10 => 'Test Bruce Review',
(int) 11 => 'Test Bruce 3 = Final'
when I select for example 'Test Bruce Review' and hit submit and debug the value it prints out '6', the 6th item in the dropdown box when I want it printing out 10 when I debug it.
here is the snippet from the form relating to this dropdown box
<tr><td><?php echo "Template: "?></td>
<td><?php echo $this->Form->input('template_id', array('label'=>false,'type'=>'select','options'=>$templates));?></td></tr>
how can I remedy this issue?
I had an issue with merging my arrays that caused the death of my template.id
I can't make element caching work:
echo $this->element('categorytree', array(
'cache' => array(
'key'=>'categorytree-cache','time' => '+1 hour'
)
))
I checked core.php:
// In development mode, caches should expire quickly.
$duration = '+999 days';
/*if (Configure::read('debug') >= 1) {
$duration = '+10 seconds';
}*/
And commented as you see - to prevent short caching in debug mode...
But still when I refresh page the SQL query that cached element does by requestAction() is shown... So no caching... why?
I may be wrong here, but I think the correct call would be:
echo $this->element('categorytree', array(), array(
'cache' => array(
'key'=>'categorytree-cache','time' => '+1 hour'
)
))
Notice the second argument is element params while the third is element options (what you need here).
You can see the function signature here.