Cake php Text Helper Issue
In view.ctp
$userName = 'Jusnitdustinwq';
echo $this->Text->truncate( $userName, 8, array('ending' => '...', 'exact' => false));
In document of cake php truncate it is written as if 'exact' is 'false', then $userName will not be cut mid-word, but here no word or $username is displaying, instead only ... is displaying here for above example
How to correct it ?
Try this:
echo $this->Text->truncate($userName , 8, array('ending' => '...'));
Or
echo $this->Text->truncate($userName , 8, array('ending' => '...', 'exact' => true));
problem cause by exact param, because $userName is not a collection of words separated by space and exact => true works on that type of input.
If you try like following, will see the fact:
$userName = 'Ju snit dustinwq';
echo $this->Text->truncate($userName , 8, array('ending' => '...', 'exact' => false));
It's working as intended. In your example, if you set 'exact'=>false, it tries to find a space somewhere at/before 8 characters to truncate there, but there are none. So, the only way it can keep your string below 8 characters and not cut off a word, is by removing all the text and just using "...".
Instead, try this:
$userName = 'Jusnitdustinwq';
if(strpos($userName, ' ')) {
echo $this->Text->truncate( $userName, 8, array('exact' => false));
} else {
echo $this->Text->truncate( $userName, 8);
}
Notice, you don't need to specify 'ending' unless you want to change it to something OTHER than the default, which is '...'; The same goes withk 'exact', which has the default of true.
Related
$params = array(
'fields' => array(
'OutCountry,Sum(TotalCalls),Sum(ConnectedCalls),sum(Duration),(Sum(ConnectedCalls)/Sum(TotalCalls))*100,sum(Duration)/Sum(ConnectedCalls),
sum(SaleAmount) as SaleAmount'),
'table'=> 'Nextone_cdr_reports.'.$tabname ,
'conditions'=>array(
'InDate' => date("Y-m-d",strtotime("-1 days")),'not'=>array('OutCountry'=>null)),
'group' => 'OutCountry',
'order' => $orderby
);
$rs = $this->find('all',$params);
I want to make it print in Model only.How Can I do so?
To print your query in cakephp 2.x Please follow these step
1)set the debug variable to 2 in app/config/config.php
2) <?php echo $this->element('sql_dump');?>
at the end of your layout. This should actually be commented out in your default cake layout.
Do you want to print the result of this query?
If yes then you can print the result of this query using below code in your model file.
echo "<pre>";
print_r($rs);
exit();
I have an array of hashes (AoH) which looks like this:
$VAR1 = [
{
'Unit' => 'M',
'Size' => '321',
'User' => 'test'
}
{
'Unit' => 'M'
'Size' => '0.24'
'User' => 'test1'
}
...
];
How do I write my AoH to a CSV file with separators, to get the following result:
test;321M
test1;0.24M
I've already tried this code:
my $csv = Text::CSV->new ( { sep_char => ';' } );
$csv->print( $fh1, \#homefoldersize );
But I get
HASH(0x....)
in my CSV file.
Pretty fundamentally - CSV is an array based data structure - it's a vaguely enhanced version of join. But the thing you need for this job is print_hr from Text::CSV.
First you need to set your header order:
$csv->column_names (#names); # Set column names for getline_hr ()
Then you can use
$csv -> print_hr ( *STDOUT, $hashref );
E.g.
$csv -> column_names ( qw ( User Size Unit ) );
foreach my $hashref ( #homefoldersize ) {
$csv -> print_hr ( *STDOUT, $hashref );
}
As you want to concatenate a couple of your columns though, that's slightly harder - you'll need to transform the data first, because otherwise Size and Unit are separate columns.
foreach my $hashref ( #homefoldersize ) {
$hashref -> {Size} .= $hashref -> {Unit};
delete $hashref -> {Unit};
}
Additionally -as another poster notes - you'll need to set sep_char to change the delimiter to ;.
As an alternative to that - you could probably use a hash slice:
#values = #hash{#keys};
But print_hr does pretty much the same thing.
All that is necessary is
printf "%s;%s%s\n", #{$_}{qw/ User Size Unit /} for #homefoldersize;
Try using the example for Text::CSV posted here: http://search.cpan.org/~makamaka/Text-CSV-1.33/lib/Text/CSV.pm
You will need to set sep_char = ';' to make it semicolon-separated.
I am trying to extract the contents of a file and store it in a variable. My code looks like this.
define ssh_user (
$name = undef,
$role = undef,
$password = undef,
$usergroup = undef, ) {
user { $name:
home => "/home/${name}/",
ensure => present,
}->
exec { "generate keys ${name}":
user => $name,
command => '/bin/echo -e "\n\n\n" | ssh-keygen -t rsa',
path => "/usr/bin/",
}->
file { "/home/${name}/.ssh/auhorized_keys":
ensure => file,
mode => 700,
source => "/home/${name}/.ssh/id_rsa.pub",
}
##node_user { $name:
require => File["/home/${name}/.ssh/auhorized_keys"],
key => file('/home/${name}/.ssh/id_rsa.pub'), ## line causing error
role => "ssh::users::${name}::role",
}
}
I get the following error,
Error: Could not find any files from /home/${name}/.ssh/id_rsa.pub at /etc/puppet/manifests/sshd_setup.pp:90 on node puppet.colo.seagate.com
Error: Could not find any files from /home/${name}/.ssh/id_rsa.pub at /etc/puppet/manifests/sshd_setup.pp:90 on node puppet.colo.seagate.com
I am creating files for a set users and storing the contents of the file in a varaible.
After storing the file contents in a variable I am using it to create an exported resource.
I tried using "require" and "->" for ensuring the ordering between my resources; but it turns out the error may be because of the path to my file (it contains a variable name).
You're using single quotes instead of double quotes for that line, I'm guessing that's why the variable isn't parsed correctly?
ie. it should be
key => file("/home/${name}/.ssh/id_rsa.pub"), ## line causing error
instead
I'm trying to make a query to a database in Joomla 2.5. I have a db named 'example', and I'm trying to get certain value named 'value' (very original) for a user whose id is 949:
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$user = 949;
$db->setQuery( 'SELECT value FROM example WHERE user_id = ' . $user );
$result = $db->loadObjectList();
echo $result;
However, I'm just getting 'Array' as result (the expected value is a decimal, e.g. 4.5).
Could someone please tell me what am I doing wrong?
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$user = 949;
$db->setQuery( "SELECT value FROM example WHERE user_id = '" . $user."'" );
$result = $db->loadObjectList();
echo $result;
try this one
$db->loadObjectList() returns an array of objects, which echo can't display. If you want to just return one value from one row, user $db->loadResult() instead.
I am trying to replace multiple urls in a pagination element with a new url.
The replacement works fine when there is one occurrence of the url i.e. prev and next, but it breaks on breaks on the Pagination Numbers. It brings the first and last number links together. How do I get the preg_replace function realize that there are multiple occurrences of the links that need replacing?
<?php
$pattern = '/\/(.+)\/page:(\d)/S';
$replacement = $uurl.'page:$2';
echo preg_replace($pattern, $replacement,$paginator->prev('<< '.__('previous', true), array('url' => $paginator->params['pass']), null, array('class'=>'disabled'))).' | ';
echo preg_replace($pattern, $replacement,$paginator->numbers());
echo preg_replace($pattern, $replacement,$paginator->next(__('next', true).' >>', array('url' => $paginator->params['pass']), null, array('class'=>'disabled')));
?>
Try this:
$pattern = '/\/(.+?)\/page:(\d)/S';
Your .+ is greedy. In other words, it's sucking up everything between the first / and the last /page:.
The ? operator will make it match the minimum instead.