How to use truncate of text helper inside html(tag) helper in view of cakephp? - cakephp-2.1

echo $this->Html->tag('span',$user['User']['user_title'], array('class' => "nameOfUser");
###
in this tag i want to truncate user_title to 20 characters .
SO how i can used truncate in HTML tag ?

Use the TextHelper; http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#texthelper
echo $this->Html->tag('span',
$this->Text->truncate($user['User']['user_title'], 20),
array('class' => "nameOfUser")
);

Related

Why does adding a class to a p tag in the json of a seeder break that seeder?

I have a seeder that, in one part of it seeds a json column as shown below. If I have the text in a p tag everything seeds just fine.
This works:
DB::table('table_name')->insert(array(
array(
'json' => '<p>some text</p>'
)
))
However if I add a class or a style tag to the text, it breaks the seeder! why? How? How do I get around this?
DB::table('table_name')->insert(array(
array(
'json' => '<p class="subtitle">some text</p>'
)
))
The error that I get is <<Unknown error>>: 3140 Invalid JSON text: "Invalid value." at position 27806 in value for column
Try it like this:
DB::table('table_name')->insert(array(
array(
'json' => json_encode('<p class="subtitle">some text</p>')
)
))

Active records on codeigniter page

I want print a table from database on view page with where condition, means I want print table of student where class is seven and fee status is paid.
How we do this?
i tried it but not work:
<?php
$students = $this->db->get_where('student' , array('status' => paid,'class_id'=>7))->result_array();
foreach($students as $row):?>
I have used this code to find the answer for you Please see it and try it out
$query = $this->db->get_where('tasks', array('description' => 7,'status' => 1));
echo "<pre>";
print_r($query->result_array());
change your array element as this since paid is a string it must be in quots
array('description' => "paid",'class_id'=>7)

How to add array to $data array CodeIgniter

I have some $data that i am transfering to view, ok that is easy
But i want this
$data['promenjive']=array
(
'1' => prva,
'2' => 'druga',
'3' => 'treca',
);
And
$this->load->view("view_home", $data);
My question is how to echo a single value from $data['promenjive']
To make something like this
1
I dont want to foreach entire array just one element?
In your example prva would echoed from within your view template with:
<?php echo $promenjive[1];?>
To echo 1 from your example you would have to do:
<?php echo array_search('druga',$promenjive);?>

cakephp211 date insertion

this is my variable:
<?php
//...
$creat='2000-03-15' ,from an input field.
?>
this is my insertion code:
<?php
//...
$this->Comment->updateAll(
array('Comment.c' => $creat ),
array('Comment.id' => '3' ));
?>
when i want to save it in a table which have a date field type, instead of '2000-03-15', i have '0000-00-00'?
why?
thanks for comments!
the updateAll function does not escape your input. It will also not wrap your data in quotes.
In your example, to properly escape your $creat variable, i would suggest using the following code:
<?php
App::uses('Sanitize', 'Utility');
$Comment->updateAll(array(
'Comment.c' => "'" . Sanitize::escape($creat) . "'"
), array(
'Comment.id' => 3
));
We wrap the date with quotes and also pass it through Sanitize::escape to make sure we protect ourselves against SQL injection.

Magento flat product table - retrieving "Most Viewed" product names

I currently show a collection of the "Most Viewed" products as a list in my footer. I get the collection using the following code:
$_viewed_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addViewsCount()
->setOrder('views_count', 'desc')
$_viewed_productCollection->load();
This works fine until I enable flat products, and then it can no longer retrieve the product names or prices. It can however still get the url, the sku etc, which really confuses me. I printed out the array of what can be returned, and it seems there’s no name data there at all, only the following:
Array (
[views] => 29
[entity_id] => 18
[entity_type_id] => 10
[attribute_set_id] => 38
[type_id] => simple
[sku] => sw810i
[created_at] => 2007-08-23 15:47:44
[updated_at] => 2008-08-08 14:50:56
[has_options] => 0
[required_options] => 0
[is_salable] => 1
[stock_item] => Varien_Object Object
( [_data:protected] => Array
( [is_in_stock] => 1 )
[_origData:protected] =>
[_idFieldName:protected] =>
[_isDeleted:protected] => )
[gift_message_available] => 2
)
Is there anyway to get a product name using it's SKU? Something like this perhaps:
<?php $sku = $_product->getData('sku'); echo $this->htmlEscape($_product->getName($sku)) ?>
I think this is actually a bug, as why would you not be able to get a products name? So, I've submitted it as such:
http://www.magentocommerce.com/bug-tracking/issue?issue=9812
I did come up with a workaround for this, if anyone needs it:
<?php foreach ($_productCollection as $_product): ?>
<?php $sku = $_product->getData('sku'); $_currentproduct = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku); ?>
<li><?php echo $this->htmlEscape($_currentproduct->getName()) ?></li>
<?php endforeach; ?>
Basically you’re just using the sku, which you can get straight from the product collection, to load the name separately.

Resources