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
Related
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 '{'
controllers/home.php
$this->page_model->counter($this->data['post_detail']->post_ID);
$this->data['post_list'] = $this->post_model->post($this->data['post_detail']->post_ID);
$this->data['gallery_list'] = $this->post_model->post($this->data['post_detail']->post_ID, true);
$isAjax = array('news-post');
if (in_array($this->data['post_detail']->template_name, $isAjax))
{
if ( ! $this->input->is_ajax_request())
redirect($this->data['page_detail']->post_alias);
return $this->load->view($this->data['post_detail']->template_name, $this->data);
}
$this->load->view($this->data['post_detail']->template_name, $this->data);
views/news.php
<?php
$this->load->library('pagination');
$config['base_url'] = 'http://gsa-constructionspecialist.com/articles/article';
$config['total_rows'] = 14;
$config['per_page'] = 5;
$this->pagination->initialize($config);
?>
<div class="w626 content right">
<?php
if ($post_list){
foreach ($post_list as $pl){
?>
<div>
<p><br><br><strong><?php echo $pl->post_title; ?></strong></p>
<p><?php echo date('F jS, Y',strtotime($pl->post_date)); ?></p>
<br/>
<div style="text-align:justify"><?php echo word_limiter(strip_tags($pl->post_content),25); ?><span style="color:#fff"> Read More ></span></div>
</div>
<?php } } ?>
<?php echo $this->pagination->create_links(); ?>
I am trying to set the pagination but it only appears on the bottom and it does not hide the articles that suppose to be on the next page.
Please help fix the codes? Thanks in advance.
The pagination class will generate pages numbers based on the information you provide to it, however its up to you to generate the actual results for a particular page. You might tell the pagination class it's 5 results per page, but in reality it doesn't know whether you're outputting 5 results per page or 500.
It seems to me as though:
$this->data['post_list'] = $this->post_model->post($this->data['post_detail']->post_ID);
needs the current page number passing in, i.e.
$this->data['post_list'] = $this->post_model->post($this->data['post_detail']->post_ID, $current_page);
You then need to modify your database query within the model using LIMIT to limit the number of results (and the offset) for that page.
Does this help?
In Joomla Page heading showing inside of an article I want to change the position of page heading, is it possible to customize page heading position?
I had included following code in template/protostar/index.php
<?php if ($this->params->get('show_page_heading', 1)) : ?>
<div class="page-header">
<h1> <?php echo $this->escape($this->params->get('page_heading')); ?> </h1>
</div>
<?php endif;
if (!empty($this->item->pagination) && $this->item->pagination && !$this->item->paginationposition && $this->item->paginationrelative)
{
echo $this->item->pagination;
}
?>
What you can do:
Just update one of the css files in the correct template to display the header correctly. If the header should only be reformatted on some pages and not all then you should be using different templates.
What you should do:
Otherwise (if you want to change the php instead) you can override the components/com_content/views/article/default.php using the standard joomla override method.
You can do both the above if necessary.
You should not need to override the index.php of your template in order to do this. However if you really want to i would use the code
$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
if ($option=="com_content" && $view=="article") {
$ids = explode(':',JRequest::getString('id'));
$article_id = $ids[0];
$article =& JTable::getInstance("content");
$article->load($article_id);
echo $article->get("title");
}
Sorry if you want more you need to give more :)
PS. I am on joomla 2.5 but i know that for joomla 3 it is more or less the same thing.
Sources: http://forum.joomla.org/viewtopic.php?t=525350
http://docs.joomla.org/How_to_override_the_output_from_the_Joomla!_core
I'm trying to learn web scraping with Xpath. The code below works, however the output contains of incorrect characters and I can't manage to get this right.
Example:
Output: Emåmejeriet
How it should be: Emåmejeriet
PHP Code:
<?php
// Tried with these parameters but they doesn't make any difference
$html = new DOMDocument('1.0', 'UTF-8');
$html->loadHtmlFile('http://thesite.com/thedoc.html);
$xpath = new DOMXPath($html);
$nodelist = $xpath->query("//table");
foreach ($nodelist as $n) {
echo $n->nodeValue."\n";
}
?>
What can I do to fix this?
You should try encode() & decode() php functions if using ISO8859-15 or iconv() if not.
Example :
<?php
iconv_set_encoding("internal_encoding", "UTF-8");
iconv_set_encoding("output_encoding", "ISO-8859-1");
?>
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();
}