PHP to Laravel loop conversion - php

I am trying to convert this PHP code:
<?php
foreach ($arr as $v) {
echo '<tr><td>' . $v['bookTitle'] . '</td><td>';
$ar = array();
foreach ($v['authors'] as $key => $value) {
**$ar[] = '' . $value . '';**
}
echo implode(' , ', $ar) . '</td></tr>';
}
?>
into Laravel code, but have problems
#foreach ($arr as $v)
<tr><td> {{ $v['bookTitle'] }}</td><td>
<?php $ar = array(); ?>
#foreach ($v['authors'] as $key => $value)
***$ar[] = . $value . ;*** //{{ Html::link("all?authorID= $key", "$value")}}
#endforeach
{{implode(' , ', $ar)}}</td></tr>
#endforeach
Can someone please help me with this?
#foreach ($arr as $v)
<tr><td> {{ $v['bookTitle'] }}</td><td>
#php $ar = array(); #endphp
#foreach ($v['authors'] as $key => $value)
#php $ar[]; #endphp = {{ Html::link("all?authorID= $key", "$value")}}
#endforeach
{{implode(' , ', $ar)}}</td></tr>
#endforeach
FatalErrorException
Cannot use [] for reading

Your mistake is at this line. The $val is not exist as your are make each object in loop to $value variable
$ar[] = . $val . ;
Secondly, the blade syntax is like below. Refer to Docs for detail usage.
#foreach ($v['authors'] as $key => $value)
<td>{{ $value }}</td>
#endforeach
Note: Please keep the long code in controller instead of routes/web.php

As you're using Larvel 5.4.x, you can simply do the following and not have to re-write it using blade:
#php
foreach ($arr as $v) {
echo '<tr><td>' . $v['bookTitle'] . '</td><td>';
$ar = array();
foreach ($v['authors'] as $key => $value) {
$ar[] = '' . $value . '';
}
echo implode(' , ', $ar) . '</td></tr>';
}
#endphp
Albeit it's not the best solution, but, until you can move this logic into the controller and not have it within the view, it might be a good interim solution.
If you open up the following link for Control Structures within the Laravel documentation: https://laravel.com/docs/5.4/blade#control-structures and scroll down to the heading named "PHP" it should give you everything you need.

Related

Append HTML File Using Laravel

I need to add an HTML file to my laravel view, but I can't.
My back code:
$fls = array();
foreach ($docItems as $key => $value) {
$fl = array("flName" => $value["tab_name"], "flContent" => readfile("documents/". $referenceId .
"/" . $value["tab_name"] . ".html"));
array_push($fls, $fl);
}
$data['itemsContent'] = $fls;
return view('pages.doc', $data);
My laravel blade view code:
#foreach ($itemsContent as $k => $v)
<div>{{ $v["flContent"] }}</div>
#endforeach
Do you guys know why it is not working? I mean, it is adding my html code, but at the beginning of the page, and this is not what I want. Ahh and I am using Laravel 7 and PHP 7.

Why is this foreach loop to generate links not working?

I'm fairly new to php and I'm having trouble understanding why this piece of code doesn't work:
foreach ($titles as $values) {
echo '' . $values . '';
}
$titles=array('title1','title2','title3');
if (is_array($titles)){
foreach ($titles as $values) {
echo '' . $values . '';
}
}
Please check if your $titles is an array first with is_array().

Undesirable duplicate content with double foreach in blade

how can I make sure I use more foreach in my blade, without the content repeating more than once?
$review = Review::orderBy('created_at', 'desc')->paginate(9);
$info_games = array();
$games = array();
foreach ($review as $value) {
$info_games[] = InfoGames::where('id_game', $value->id_game)->first();
$games[] = Games::where('id', $value->id_game)->first();
}
return view('front.pages.review.list', compact('review','info_games','games'));
and my blade:
#foreach ($review as $value)
#foreach ($info_games as $info_game)
<div>
<img src="https://gamelite.net/{{$info_game->image}}">
<p>{!! str_limit($value->body, 150) !!}</p>
</div>
#endforeach
#endforeach
{!! $review->render() !!}
use array_filter($array) . It will return unique .
in your case
$info_games = array_filter($info_games);

Add key and value to Zend MultiCheckbox dynamically

i have retrieved courseList and courseId from databse like this
foreach ($courses as $item) {
$checkBoxText = '';
$checkBoxText .= $item['courseRubric']. "-". $item['courseNumber']. " ". $item['courseTitle']. " [".$item['semester']. " " . $item['year']. "]";
$this->courseList[] = $checkBoxText;
$checkboxId = '';
$checkboxId .= $item['id'];
$this->courseId[] = $checkboxId;
}
Now, I want to add these array items to Zend_MultiCheckbox,
foreach ($this->courseId as $key => $value) {
$courseId[$value]= $value;
}
foreach ($this->courseList as $key => $value) {
$element->addMultiOptions(array(
$courseId[$key] => $value
));
}
This logic is not working.Can anyone suggest me how I can get
Course
Thanks
You have 2 solutions:
1 :
foreach ($this->courseList as $key => $value) {
$element->addMultiOption("$courseId[$key]", "$value");
}
2:
$opions = array();
foreach ($this->courseList as $key => $value) {
$options[$courseId[$key]] = $value;
}
$element->addMultiOptions($options);
I think, the 2nd is better.
Good luck.

getting object properties and values recursivly

so i have an object:
$user->name->first = "Bob";
$user->name->last = "Smith";
$user->address->street = "1234 anywhere st.";
$user->address->city = "Chicago";
$user->address->state = "Texas";
how can i iterate through this object without knowing the "name,address" property?
I want to be able to do
foreach ($user as $key -> $value)
{}
now i can do this:
foreach ($user as $key -> $value)
{
foreach ($value as $k => $v)
{
echo $k . "," . $v . "\n";
}
}
and i get a nice little list of
first,Bob
last,Smith
street,1234 anywhere st.
city,Chicago
state,Texas
but how can i get the property name or address to print?
i.e.
name,first,Bob
name,last,Smith
address,street,1234 anywhere st.
address,city,Chicago
address,state,Texas
The value you want is in $key already from the first foreach loop.
foreach ($user as $key => $value)
{
foreach ($value as $k => $v)
{
echo "$key,$k,$v\n";
}
}
IDEOne.com demo

Categories