Error <?php echo json_encode($variable) ?> and {{!! json_encode($variable) !!}} - laravel

I'm using Laravel 5.4 on a small project. I have a controller processing some data, producing an array and storing it on a $transactions variable.
When I return the variable from the controller (temporarily and just to check the content, after which I delete that instruction) I use
return $transactions;
and the result displayed on the browser is:
[
["Entrada por Ajuste","20170204","2017-02-05","Inventario al 04FEB2017 Pag. 1",7,70,10,70,7],
["Venta","20170206","2017-02-06","Ventas del Lunes",1,10,0.8695652173913,60,69]
]
however when I use the same controller to return the data to a view (with:
return view('products.kardex', compact(
'product',
'transactions',
'beforeCost',
'beforeQty')
);
to render the array data in a view with the instruction:
var dataSet = {{!! json_encode($transactions) !!}};
I get this rendered:
var dataSet = {[["Entrada por Ajuste","20170204","2017-02-05","Inventario al 04FEB2017 Pag. 1",7,70,10,70,7],["Venta","20170206","2017-02-06","Ventas del Lunes",1,10,0.8695652173913,60,69]]};
adding an extra {[[ in the rendered view which is giving me some trouble.
Additionally if I try this in the view:
var dataSet = <?php echo json_encode($transactions) ?>;
I get this rendered:
var dataSet = [["Entrada por Ajuste","20170204","2017-02-05","Inventario al 04FEB2017 Pag. 1",7,70,10,70,7],["Venta","20170206","2017-02-06","Ventas del Lunes",1,10,0.8695652173913,60,69]];
Which is what I actually need in the view
My questions is aren't <?php echo ?> and {{!! !!}} equivalents?. What am I doing wrong?

Simply do
{!! json_encode($transactions) !!}
Remove one curly brace '{'

Related

How to create dynamic Checkboxlist in Yii?

I would like a help to solve this problem. I'm using Yii 1.1 and trying to create a dynamic CheckBox list in which its elements change depending on the value selected in a DropDown element.
I created this DropDown element with the arguments in Ajax to perform the function update in the Controller. The function in the Controller is doing the lookup in the table according to the value passed.
Up to this point, the code is working fine, generating an array with the values that should be displayed. The problem is that I'm not able to configure the return of these data and I can't even display them in the View.
Below is the code data:
View - DropDown element:
echo CHtml::activeDropDownList($model, 'standard',
array(CHtml::listData(standard::model()->findAll(), 'name','name')),
array('empty'=>'',
'ajax'=>array(
'type'=>'POST',
'url'=>CController::createUrl('/getTeam',array('form'=>'Team','field'=>'standard')),
'update'=>'#Audit_team'),
)
);?>
Controller:
public function actionGetTeam($form,$field) {
$post = $_POST;
$standard = $post[$form][$field];
$Lists = TblAuditStandard::model()->findAll("standard = $standard");
foreach ($Lists as $List){
$AuditTeam[] = $List->name." - ".$List->login;
asort($AuditTeam);
foreach ($AuditTeam as $id=>$value )
echo CHtml::tag('option',array('value'=>$id),$value,true);
}
}
View - Checkbox element:
<div class="row">
<?php echo $form->labelEx($model,'team'); ?>
<?php echo CHtml::activeCheckBoxList($model,'team',array()); ?>
<?php echo $form->error($model,'team'); ?>
</div>
I hope someone can help me solve this problem. Thanks.

Laravel View::render() gives error

I'm trying to render a view with čćžšđ letters in it, but it's failing and giving me this error:
The Response content must be a string or object implementing __toString(), \"boolean\" given."
I extracted a partial, and when I load this partial using normal way (with #include) then everything is ok.
But I need "load more" function, so I'm using this partial to render html for me, and I only append it to DOM using jQuery.
This is the partial:
<?php $flag = true; ?>
#foreach($tags as $tag)
#if(empty($tag->tag)) <?php continue; ?> #endif
#if($flag == true)
<?php $temp = $tag->tag[0]; ?>
<br><label class="firstLetterLabel">{{ strtoupper($temp) }}</label><br><hr><br>
<?php $flag = false; ?>
#endif
#if($temp != $tag->tag[0])
<br><label class="firstLetterLabel">{{ strtoupper($tag->tag[0]) }}</label><br><hr><br>
#endif
<div class="singleTag">
{{ $tag->tag }}
</div>
<?php $temp = $tag->tag[0]; ?>
#endforeach
This is how I use it in "load more" function:
$tags = Tag::orderBy("tag")->take(Config::get("settings.num_tags_per_page"))->skip(($page-1)*Config::get("settings.num_tags_per_page"))->get();
$data = array();
$data['data'] = View::make("discover.partials.tags")->with("tags", $tags)->render();
if(count($tags)%Config::get("settings.num_tags_per_page") == 0 && count($tags) > 0)
$data['msg'] = 'ok';
else
$data['msg'] = 'stop';
return json_encode($data);
This partial read tags and sort them alphabetically, and it extracts first letter of every tag because I need that letter somewhere else.
And when this partial finds one of these letters čćžšđ then it gives me above error.
How to solve this?
Try this:
json_encode($output, JSON_UNESCAPED_UNICODE)
For future notice, I solved this problem by replacing $tag->tag[0] with mb_substr($tag->tag, 0, 1). This command extracts utf-8 characters from string while my previous approach wasn't properly encoding utf-8 chars.
More info here: Get first character of UTF-8 string

load view via ajax cakephp 2.x

i want to load a list of items on my view via ajax here is my code on lst.cpt
<div id='benpane' class='clearfix'>
<script type="text/javascript">
<?php echo $ajax->remoteFunction(array(
'url'=>array('controller'=>'benefits', 'action'=>'display'),
'update'=>'benpane',
'indicator'=>'benIndicator'
)); ?>
</script>
</div>
here is the lst function in my controller
function lst() {
$this->paginate = array('order' => array('ben_name' => 'ASC'),'conditions' => array($this->Benefit->parseCriteria($this->passedArgs)));
$benefit = $this->paginate('Benefit');
$this->set('bens', $benefit);
}
when i try to open the view i get the error
Error: Call to a member function remoteFunction() on a non-object
File: /var/www/hassportal/app/View/Benefits/lst.ctp
Line: 14
what could i be doing wrong?
Besides the fact that the Ajax Helper has been deprecated (and/or perhaps you are not using cakephp 2.x), you seem to be calling the wrong action:
'action'=>'display'
should be:
'action'=>'lst'
Also, I would move the code outside of the DIV that is supposed to be updated with the data coming from that action.
Use this
$this->Ajax->remoteFunction
insited of
$ajax->remoteFunction
Ex:-
<?php echo $this->Ajax->remoteFunction(array(
'url'=>array('controller'=>'benefits', 'action'=>'display'),
'update'=>'benpane',
'indicator'=>'benIndicator'
)); ?>

Retrieve product custom media image label in magento

I have a custom block loading products on my front page that loads the four newest products that have a custom product picture attribute set via:
$_helper = $this->helper('catalog/output');
$_productCollection = Mage::getModel("catalog/product")->getCollection();
$_productCollection->addAttributeToSelect('*');
$_productCollection->addAttributeToFilter("image_feature_front_right", array("notnull" => 1));
$_productCollection->addAttributeToFilter("image_feature_front_right", array("neq" => 'no_selection'));
$_productCollection->addAttributeToSort('updated_at', 'DESC');
$_productCollection->setPageSize(4);
What I am trying to do is grab the image_feature_front_right label as set in the back-end, but have been unable to do so. Here is my code for displaying the products on the front end:
<?php foreach($_productCollection as $_product) : ?>
<div class="fll frontSale">
<div class="productImageWrap">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'image_feature_front_right')->directResize(230,315,4) ?>" />
</div>
<div class="salesItemInfo">
<p class="caps"><?php echo $this->htmlEscape($_product->getName());?></p>
<p class="nocaps"><?php echo $this->getImageLabel($_product, 'image_feature_front_right') ?></p>
</div>
</div>
I read that $this->getImageLabel($_product, 'image_feature_front_right') was the way to do it, but produces nothing. What am I doing wrong?
Thanks!
Tre
It seems you asked this same question in another thread, so to help others who might be searching for an answer, I'll anser it here as well:
I imagine this is some sort of magento bug. The issue seems to be that the Magento core is not setting the custom_image_label attribute. Whereas for the default built-in images [image, small_image, thumbnail_image] it does set these attributes - so you could do something like:
$_product->getData('small_image_label');
If you look at Mage_Catalog_Block_Product_Abstract::getImageLabel() it just appends '_label' to the $mediaAttributeCode that you pass in as the 2nd param and calls $_product->getData().
If you call $_product->getData('media_gallery'); you'll see the custom image label is available. It's just nested in an array. So use this function:
function getImageLabel($_product, $key) {
$gallery = $_product->getData('media_gallery');
$file = $_product->getData($key);
if ($file && $gallery && array_key_exists('images', $gallery)) {
foreach ($gallery['images'] as $image) {
if ($image['file'] == $file)
return $image['label'];
}
}
return '';
}
It'd be prudent to extend the Magento core code (Ideally Mage_Catalog_Block_Product_Abstract, but I don't think Magento lets you override Abstract classes), but if you need a quick hack - just stick this function in your phtml file then call:
<?php echo getImageLabel($_product, 'image_feature_front_right')?>
Your custom block would need to inherit from Mage_Catalog_Block_Product_Abstract to give access to that method.
You could also use the code directly from the method in the template:
$label = $_product->getData('image_feature_front_right');
if (empty($label)) {
$label = $_product->getName();
}

codeigniter session issue - some session info not sticking

I'm using codeigniter session library to hold data that is used in a series of 3 pages and I'm experiencing strange behavior. My session variables remain in tact but the values disapear. Even stranger: I'm trying to store a serialized array in my session data and the first item of the array ends up being stored in a different variable?
I've attached a link that starts at the first page in the series where it is possible to click to the next page. I've printed the user_session data at the top of both pages (the third page isn't set up yet).
http://playmatics.com/nypl/site/index.php/member_area/quest/accept_quest/12
Sessions work everywhere else, for example I'm using a session to store login data and that works fine.
I've attached my controller and view below
//CONTROLLER:
function accept_quest() {
$assoc_quest_id = end($this->uri->segments);
if(!isset($quest_id)) {
redirect('member_area/quest');
//SEND A MESSAGE: NO QUEST STARTED
}
$quest_rows = $this->quest_model->get_quest_with_images($assoc_quest_id);
$quest = current($quest_rows);
$images = $this->pull_out_images($quest_rows);
//the data array is used both in the session,
//to pass values over to the next function in the quest chain
//and in the template
$data = array();
$data['quest_id'] = $assoc_quest_id;
$data['instruction_text'] = $quest->instructions;
$data['quest_title'] = $quest->name;
$data['quest_time_limit'] = $quest->time_limit;
$data['points_awarded'] = $quest->points_availible;
$data['quest_images'] = serialize($images);
//save data in a flash session to be used in the next function call in the quest chain: quest_action
$this->session->set_userdata($data);
print_r($this->session->all_userdata());
//the following data aren't needed in the session so they are added to the data array after the session has been set
$data['annotation_text'] = $quest->note;
$data['main_content'] = 'quests/quest_desc';
$this->load->view('includes/template', $data);
}
function quest_action() {
print_r($this->session->all_userdata());
$quest_id = $this->session->userdata('quest_id');
echo "the quest id is: $quest_id";
if(!isset($quest_id)) {
redirect('member_area/quest');
//SEND A MESSAGE: NO QUEST STARTED
}
$data['quest_id'] = $quest_id;
$data['quest_title'] = $this->session->userdata('quest_title');
$data['quest_images'] = $this->session->userdata('images');
$data['instruction_text'] = $this->session->userdata('instructions');
$data['quest_time_limit'] = $this->session->userdata('quest_time_limit');
$data['main_content'] = 'quests/quest_action';
$this->load->view('includes/template', $data);
}
//VIEW
//quest_desc:
<h1><?= $quest_title ?></h1>
<div id="quest_elements">
<figure>
<? foreach(unserialize($quest_images) as $image): ?>
<img class="media" src="<?= $image ?>" alt="<?= $quest_title ?> image"/>
<? endforeach; ?>
<figcaption>annotation: <?= $annotation_text ?></figcaption>
</figure>
<?= anchor("member_area/quest/quest_action", "Start Quest", array('title' => 'start quest')); ?>
</div><!-- end quest_elements -->
//quest_action:
<h1><?= $quest_title ?></h1>
<div id="quest_elements">
<figure>
<? foreach(unserialize($quest_images) as $image): ?>
<img class="media" src="<?= $image ?>" alt="<?= $quest_title ?> image"/>
<? endforeach; ?>
<figcaption>instructions: <?= $instruction_text ?></figcaption>
</figure>
<div id="timer">
<?= $quest_time_limit; ?>
</div>
<?= anchor("#start_timer", "Start Timer", array('title' => 'start quest timer')); ?>
</div>
If you are hitting the cookie size limit, I would suggest switching to CodeIgniter's native Database Sessions class. This enables you to store session information in a database, effectively removing the cookie size limitation, you are simply restricted to the size of the user_data field in the ci_sessions database.
Following the link above, the section on utilizing database sessions is near the bottom, providing you the proper DB schema and the config switch to database sessions.
As others have said, it is likely that you are hitting the 4k cookie limit of CI's session library. There are other alternative libraries available that use standard PHP sessions - http://codeigniter.com/wiki/PHPSession/ and http://codeigniter.com/wiki/Native_session/ for instance.

Resources