I am using Laravel 4.2. I have a situation that I need to address in laravel blade..
WITHIN the template I have this ( and it works fine)
<?php
$faqGroup = json_decode(Lng::faq(), true);
$lang = Lng::faq()['view_as'];
$Q = 'question_' . $lang;
$A = 'answer_' . $lang;
foreach ($faqGroup as $key => $faq) {
if (isset($faq[$Q]) && isset($faq[$A])) {
?>
<?php echo $faq[$Q]; ?> <br/>
<?php echo $faq[$A]; ?> <br/><br/>
<?php
}
}
?>
my output is
question1 eng
answer1 eng
question2 eng
answer2 eng
question3 eng
answer3 eng
When I try to do the same with blade
<?php
$faqGroup = json_decode(Lng::faq(), true);
$lang = Lng::faq()['view_as'];
$Q = 'question_' . $lang;
$A = 'answer_' . $lang;
?>
#foreach ($faqGroup as $key => $faq)
{{ isset($faq[$Q]) }} <br/>
{{ isset($faq[$A]) }} <br/><br/>
#endforeach
my output is
1
1
1
If I try this
#foreach ($faqGroup as $key => $faq)
{{ $faq->$Q }} <br/>
#endforeach
I get Trying to get property of non-object
If I try this
#foreach ($faqGroup as $key => $faq)
{{ $faq[$Q] }} <br/>
#endforeach
I get Illegal string offset 'question_eng'
Help?
Based upon your original code and logic which is working - your #foreach loop should be like this
#forreach ($faqGroup as $key => $faq)
#if (isset($faq[$Q]) && isset($faq[$A]))
{{ $faq[$Q] }} <br/>
{{ $faq[$A] }} <br/><br/>
#endif
#endforeach
Related
I am trying to use the localization retrieval of string inside of php foreach loop code in blade template in laravel 8.
Inside the foreach loop, I am trying to manipulate a value called $item['label'] and equate translate the value of it using the language localization that laravel have.
Here's my current code.
#foreach ($items as $item)
#php
$item['label'] = "{{ __($item['label']) }}"
#endphp
#endforeach
But I get an error of
ParseError
syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
In the first place, can I use a {{ __ ('string') }} or #lang('string') inside a #php in the first place?
If I can't is there any other approach for this one?
Thank you very much!
#php and #endphp is a blade syntax, and it is the same as writing:
<?php ?>
So you could do this:
<?php
echo __('profile/username');
?>
Or you can write it using a Blade template engine:
#php
echo __('profile/username');
#endphp
To print the items you could do this:
#foreach ($items as $key => $item)
{{ __($item) }}
#endforeach
Here an example with data:
#php
$items = ['engine_1' => ['label' => 'Google'], 'engine_2' => ['label' => 'Bing']];
#endphp
#foreach ($items as $key => $item)
{{ __($item['label']) }}
#endforeach
// The output will be => Google Bing
In order to save the translation of the item, remove the "{{ }}" and use the key in order to detect on which index to apply the changes, like the following:
#foreach ($items as $key => $item)
#php
$items[$key]['label'] = __($item['label'])
#endphp
#endforeach
Notice what #Nurbek Boymurodov wrote to you, you need to use the $key, because doing something like this will not override the data within a foreach loop:
#foreach ($items as $key => $item)
#php
$item['label'] = __($item['label']); // Wrong way of overriding data
#endphp
#endforeach
while using foreach you cant change its value here try this this will work if $items is array not stdClass
#foreach ($items as $key => $item)
#php
$items[$key]['label'] = __($item['label']);
#endphp
#endforeach
Thanks, #Nurbek Boymurodov!
It was your comment that answered my question.
Here's the code right now:
#foreach ($items as $item)
#php
$item['label'] = __($item['label']);
#endphp
//other codes where I used the manipulated $item['label']
#endforeach
by just deleting the {{ }} I've manipulated the value that I want, Thank you!
How can I correct display data from array. I guess it's a foreach loop, but I do not know how to bite it. I just need only header. It's in table calendars.
Thanks so much!
#foreach($list as $item)
{{ $item->header }}
#endforeach
I tried the above code but throws out an error, unknown value "header"
#foreach($results as $type => $list)
<div class="single-contact-information mb-30">
<h3>{{ $type }} </h3>
{{ $list }}
</div>
#endforeach
[{"id":1,"header":"test","description":"<p>test<\/p>","date":"2020-12-12"}]
Edit:
SearchController:
public function search(Request $request)
{
$search = $request->get('search');
$results = [];
$results['calendars'] = DB::table('calendars')->where('header', 'like', '%'.$search.'%')->get();
return view('pages.search', ['results' => $results]);
}
You should try this:
[{"id":1,"header":"test","description":"<p>test<\/p>","date":"2020-12-12"}]
Above value is json then use below code
$list = [{"id":1,"header":"test","description":"<p>test<\/p>","date":"2020-12-12"}];
#foreach($list as $item)
{{ $item['header'] }}
#endforeach
$list = json_decode($list);
#foreach($list as $item)
{{ $item['header'] }}
#endforeach
Updated Answer
#foreach(json_decode($list) as $item)
{{ $item->header }}
#endforeach
try this one
used json_decode function here
#foreach(json_decode($list) as $item)
{{ $item['header'] }}
#endforeach
I'm using laravel 4 and I'm getting this error when I attempt to create a menu with a dropdown
Trying to get property of non-object (View: /Applications/MAMP/htdocs/test/app/views/layouts/master.blade.php) (View: /Applications/MAMP/htdocs/test/app/views/layouts/master.blade.php)
I can't see where I'm going wrong
My master.blade.php
<li class="dropdown">
work<span class="caret"></span>
<ul class="dropdown-menu" role="menu">
#foreach($dropdowns as $dropdown)
<li>
{{ $dropdown->title }}
</li>
#endforeach
</ul>
</li>
My PageController
$currentPage = new Page($pages[0]->id);
$dropdowns = $currentPage->getPagesSelectList("parent", "", "");
return View::make('index', compact('dropdowns'));
My Page model
public function getPagesSelectList($name, $default = "", $js = "", $flag = true)
{
$list = array(
'name' => $name,
'default' => $default,
'js' => $js,
'pages' => $this->getSubPages($flag, $default)
);
return (object)$list;
}
public function getSubPages($flag, $default, $parent = 0, $num = 1)
{
$pages = array();
$pageSql = \Page::where('parent', '=', $parent)->orderBy('num', 'ASC')->get();
foreach ($pageSql as $result)
{
$page = new Page($result->id);
$page->setFromDatabase();
$page->default = $default;
$page->select = '';
if( $page->id == $default )
$page->select = $default;
$page->space = $num;
if(!empty($page))
$pages[] = $page;
$children = $page->getSubPages($flag, $default, $page->id, $num + 1);
if(!empty($children))
$pages[] = $children;
}
return array_flatten($pages);
}
Assuming that the other code works fine, in Blade instead of:
#foreach($dropdowns as $dropdown)
<li>
{{ $dropdown->title }}
</li>
#endforeach
you should use:
#foreach($dropdowns->pages as $dropdown)
<li>
{{ $dropdown->title }}
</li>
#endforeach
This is because in getPagesSelectList you assign result of getSubPages to pages index of array and then you convert array to object.
I want to enumerate some content in a #foreach. I need something like:
1 - First data
2 - Second data
...
I've displayed the data using a #foreach, but I need to enumerate it. I've tried with #for, combining with #foreach, but it hasn't worked for my... Does anybody know how to do it?
Thank you in advance!!
Assuming the array keys are not the index you want to enumerate, you can do this:
#for ($i = 0; $i < count($data); $i++)
{{ $i }} - {{ $data[$i]->field }}
#endfor
If you can use the array key for enumeration, you can do this:
#foreach ($data as $key => $value)
{{ $key }} - {{ $value->field }}
#endforeach
<?php $i = 1; ?>
#foreach ($collection as $item)
<p>{{ $i }} - {{ $item->property }}</p>
<?php $i++; ?>
#endforeach
I ask this https://stackoverflow.com/a/14277726/1670630 on other post but my problem still exist.
In codeigniter 2.1 I'm trying to display channels by category. So if i have a category called Film, i should see a list of Channels within Film. I tried a nested foreach loop to accomplish this but can't seem to get it to work in the slidshow and limit by number of row.
My model:
<?php
class Pages_model extends CI_Model {
function get_channels_by_categ_tv()
{
$this->db->select('categories.category_name, channels.channel_name');
$this->db->from('type_categ');
$this->db->join('categories', 'categories.category_id = type_categ.category_id');
$this->db->join('channels', 'channels.channel_id = type_categ.channel_id');
$this->db->order_by('categories.category_id');
//$this->db->group_by(array('categories.category_id'));
$query = $this->db->get();
if($query->num_rows() == 0)
{
#no channels
return false;
}
return $query->result_array();
}
}
I have this in the view:
<ul class="slides">
<li>
<?php $cat_shown = ''; ?>
<div class="programe-tv_link">
<?php $cat_show = ''; $cnl_show = '';?>
<?php foreach ($category_chaneels as $category): ?>
<?php
if ($cat_show != $category['category_name']) {
$cat_show = $category['category_name'];
echo '<p>' . $cat_show . '</p>';
}
$cnl_show = $category['channel_name'];
echo '<dd> >>' . $cnl_show . '</dd> ';
?>
<?php endforeach; ?>
</div>
</li>
<li>
<div class="programe-tv_link">
<p>Arte</p>
<dd> >> Acasa</dd>
<dd> >> Antena 1</dd>
<dd> >> Pro TV</dd>
</div>
<div class="programe-tv_link">
<p>Music Box</p>
<dd> >> Acasa</dd>
<dd> >> Antena 1</dd>
<dd> >> Pro TV</dd>
<dd> >> TLC</dd>
</div>
</li>
</ul>
I atache image with ilustration,
sorry for my english and if you don't understund me please write here. THX in advance.
My finale code is this.
<div id="programe-tv-slide" class="flexslider">
<strong>Programe TV</strong>
<div class="redLine"></div>
<?php $cat_cnl = array();
$list = array();
$i=1;
foreach ($category_chaneels as $option) {
$catname = $option['category_name'];
$chlname = $option['channel_name'];
$cat_cnl[$catname][$i] = $chlname;
$list[$i] = $catname;
$i++;
};
?>
<?php
$rows = array_chunk($cat_cnl, 4, TRUE);
foreach ($rows as $row) { //var_dump($rows);
?>
<ul class="slides">
<?php
echo ('<li>');
foreach ($row as $category => $channels) {
echo '<div class="programe-tv_link">';
echo '<p>' . $category . '</p>';
foreach ($channels as $channel) {
echo '<dd>' . $channel . '</dd> ';
};
echo '</div>';
};
echo ('</li>');
?>
</ul>
<?php }; ?>
</div>