How can I print a find('all',$params) query in browser output in cakephp2.x? - cakephp-2.0

$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();

Related

Database query to get decimal value in Joomla 2.5

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.

combobox input of form using array variable in CakePHP

<?php
$types_size=sizeof($types)-1;
$array_type=' ';
while($types_size!=-1)
{
$array_type.='"'.$types[$types_size]["Type"]["name_type"].'", ';
$types_size--;
}
echo $array_type;
echo $this -> form -> input ('id_type', array('options' => array($array_type)));
echo $this -> form -> input ('id_type', array('options' => array(shoes, shirts, jeans,)));
?>
I created $array_type to store types data.
I got the types data : "shoes", "shirts", "jeans",
I wanna created an combobox input with 3 values just like the second input
Question : The code above doesn't show as I expected. The combobox of the first input shows only one value: shoes, shirts, jeans, not like the expected second input
Hopefully someone understand my problem !!!
In the above code you will be realized that $array_type is a string variable not an array.
You should declare an array variable using $array_type = array() not $array_type = ''
You can try to print $array_type variable and verify whether it is an array or a string.
Now your code should looks like:
<?php
$types_size=sizeof($types)-1;
$array_type= array();
while($types_size!=-1)
{
$array_type[] = $types[$types_size]["Type"]["name_type"];
$types_size--;
}
//echo $array_type; // if you echo an array variable it will print `Array`.
//Use print_r() to print an array
print_r($array_type);
echo $this -> form -> input ('id_type', array('options' => $array_type));
echo $this -> form -> input ('id_type', array('options' => array(shoes, shirts, jeans,)));
?>
<?php
$types_size=sizeof($types)-1;
$array_type= array();
while($types_size!=-1)
{
$array_type[] = $types[$types_size]["Type"]["name_type"];
$types_size--;
}
//echo $array_type; // if you echo an array variable it will print `Array`.
//Use print_r() to print an array
print_r($array_type);
echo $this -> form -> input ('id_type', array('options' => $array_type));
echo $this -> form -> input ('id_type', array('options' => array(shoes, shirts, jeans,)));

Cake php Text Helper Issue

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.

PHP: How to echo strings from an Array?

I'm stuck trying to echo strings from an array, all I get is "Array" as text.
This is the array:
$_SESSION['lista'][] = array(
'articulo' => $articulo,
'precio' => $precio,
'cantidad' => $cantidad);
This is the echo:
echo "1. ".$_SESSION['lista'][0][0]." ".$_SESSION['lista'][0][1]." unidades".", ".$_SESSION['lista'][0][2]." CRC.";
The current output is:
1. Array Array unidades, Array CRC.
Remove [] so it looks like these
And put session_start() at starting line;
<?php
session_start();
$_SESSION['lista'] = array(
'articulo' => $articulo,
'precio' => $precio,
'cantidad' => $cantidad);
?>
To access the array:
echo $_SESSION['lista']['articulo'];
echo $_SESSION['lista']['precio'];
You can't access an associative array member with a numerical key as an offset.
Try this...
echo $_SESSION['lista'][0]['articulo'];
An array's to string type method is called (and returns Array) when you try to implicitly convert it to a string, e.g. with echo.
Take a look at print_r along with var_dump etc. As stated in the manual, these functions print the contents of arrays/objects in human readable format.

preg_replace multiple problem

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.

Resources