Outputting a Multidimensional Array - php

I'm trying to call this array but I'm getting an Array to string conversion Error. I'm not sure what I'm doing wrong, I'm just learning. If anyone could help me get this set up correctly I would greatly appreciate it!
Array:
public function getPricing()
{
$windows = array("Storm Windows" => array("(includes removal, cleaning,
and replacement)", " - $12.00")
, "Double Hung" => array("(opens up and down)" ,
" - $9.00")
, "Casement" => array("(crank out or stationary
window)" ," - $8.00")
, "Transom" => array("(rectangle window over top of a
window)" ," - $3.00")
);
return view('pages.pricing')
->with("windows", $windows);
}
Calling it in the view:
#foreach ($windows as $window => $DescriptionPrice)
<h2>{{$window . $DescriptionPrice}}</h2>
#endforeach

Try this code: You were tying to output an array which is not a string. So, you need to select the array item you need to print out as shown below.
#foreach ($windows as $window => $DescriptionPrice)
<h2>For {{ $window }} - {{$DescriptionPrice[0]}} - {{$DescriptionPrice[1]}}</h2>
#endforeach

$DescriptionPrice is an array so can't be output as a string. You will need to do something like this instead:-
#foreach ($windows as $window => $DescriptionPrice)
<h2>{{$window}} {{$DescriptionPrice[0]}} {{$DescriptionPrice[1]}}</h2>
#endforeach

Related

why does't explode function work in laravel project?

It is working in the editor and gives a expected output.
But when running the project, it would not get expected output.
code:
#php
$pro ="cat-19,subcat-52,subcat-55";
$var=explode(",",$pro);
#endphp
#foreach($var as $row)
{{$row}}
#endforeach
What could be the problem in my code?
I guess you want to have a preview of the array.
$pro ="cat-19,subcat-52,subcat-55";
$var=explode(",",$pro);
print_r($var);
/* this will give you an associative array
result from print_r will be Array ( [0] => cat-19 [1] => subcat-52 [2] => subcat-55 ) */
#endphp
#foreach($var as $row)
{{$row}}
#endforeach
/* inside the loop since you are printing the value you will get the output as,
cat-19 subcat-52 subcat-55 */

How to echo array elements in laravel

I want to echo arry elemnts in view
followings are my code (controller)
$next_d_dts= \DB::table('tb_billing_cycle')->select('next_due_bill_date')->where('customer_id',$row->customer_id)->get();
return view('invoice.view')->with('next_d_dts',$next_d_dts);
I can print it using print function, eg:
print_r($next_d_dts);
Output:
Array ( [0] => stdClass Object ( [next_due_bill_date] => 2019-03-28 ) [1] => stdClass Object ( [next_due_bill_date] => 2020-02-28 ) )
You need to iterate over the collection of objects:
#foreach ($next_d_dts as $object)
{{ $object->name }}
#endforeach
If you just want to see its contents but not stop the script:
{{ var_dump($next_d_dts) }}
You've also asked how to iterate without Blade:
foreach ($next_d_dts as $object) {
echo $object->name;
}
#foreach($next_d_dts as $value)
{{$value['next_due_bill_date']}}
#endforeach
you should used foreach loop in laravel like this
#foreach ($next_d_dts as $value)
<p>Some text here{{ $value->id }}</p>
#endforeach
for more information read Laravel Manual blade template
Also you can used
dd($next_d_dts) //The dd function dumps the given variables and ends execution of the script
You can convert it into array using toArray() and iterate over it
$next_d_dts= \DB::table('tb_billing_cycle')->select('next_due_bill_date')->where('customer_id',$row->customer_id)->get()->toArray();
In view
#foreach($next_d_dts as $value)
{{ $value['column_name'] }}
#endforeach
Or
print_r($next_d_dts)
#foreach($array as $item)
{{$item}}
#endforeach
Simple.
you need to use Blade Loops syntax invoice.view.blade.php
#foreach($next_d_dts as $next )
{{ $next }}
#endforeach

Blade foreach cast output to an object

I am trying to figure out how I can give my data output in the blade file, the Laravel look;
like $data->name
But I can't get the output to be casted as an object. I think I have to make an array of the data before I can loop it proper in a foreach but this doesn't feel like the right way.
I am relatively new to Laravel and I want to do this the nice way, can someone point me in the right direction? Thanks in advance
Controller:
$data = collect($this->api->organization->index())->toArray();
return View::make('pages.organization.index', array('data' => $data[0]));
View:
#foreach($data as ((object)$organization))
{{ $organization->name }}
#endforeach
I know this will not work, but I think it illustrates my question a little bit.
EDIT
What I didn't realize is that $data = collect($this->api->organization->index()); is returning an array with all the data arrays inside because I didn't name it in my return like this:
return (object)['all' => $data];
After adding all I could reference the code inside my view like I wanted to. I know this is not a very detailed answer, if you run into the same problem message me I'll edit the answer.
Object:
$data = collect($this->api->organization->index());
#foreach($data as $organization))
{{ $organization->name }}
#endforeach
Array:
$data = collect($this->api->organization->index())->toArray();
#foreach($data as $organization))
{{ $organization['name'] }}
#endforeach

error of foreach with empty array

I'm working in Laravel 5 using Blade as motor of templates. I'm passing an array from the controller to the view, and I noticed that when I loop on it using the foreach clausule and the array is empty it gives error, exactly this:
Invalid argument supplied for foreach()
I had the same error in the controller and I fix it temporaly making:
if(count($student)!=0)
I said temporaly because I don't think it this the best way to do it.
The code in the controller is:
foreach($students as $student){
if(count($student->contracts)!=0)
foreach($student->contracts as $contract){
//something
}//end foreach
}//end foreach
I made some operations over the arrays, and then I send them to the view:
return view('myview')->with(['students'=>$students]);
The array is passing to the view correctly. I said is the foreach, beacause earlier I had the database full of registers and it worked fine, but now I have some students that doesn't have contracts and then I got that error. But, in the view I have the same error. So, it's normal? how could I fix it in a better way? why when the array is empty the foreach clausule gives that error?
PHP will not return that warning if the array contained at $student->contracts is empty. It will return it if it is of an invalid type (i.e. not an array).
Rather than checking the count() of $student->contracts, you'd be better to check if it's actually an array, as follows:
foreach($students as $student)
{
// Make sure that $student->contracts is actually an array (to bypass errors):
if( is_array($student->contracts) )
{
// Now loop through it:
foreach( $student->contracts as $contract)
{
// Do something here
}
}
}
Try this
$people = [
"Person A", "Person B", "Person C"
];
return view ('pages', compact('people'));
and loop through it like this:
#if (count($people))
<h3>People:</h3>
<ul>
#foreach($people as $person)
<li>{{ $person }}</li>
#endforeach
</ul>
#endif

Laravel (4.2) form:text issue: htmlentities() expects parameter 1 to be string

I've got a view in my Laravel app (4.2) that is for editing a database record.
I'm storing two values - colour and heading - as arrays using the form::text method:
#foreach($colours as $key => $value)
{{ Form::label('heading_' . $key,'Heading ' . ($key + 1)) }}
{{ Form::text('heading[]', '', ['id' => 'heading_' . $key, 'class' => 'u-full-width']) }}
{{ Form::label('colour_' . $key,'Colour ' . ($key + 1)) }}
{{ Form::text('colour[]', $value, ['id' => 'colour_' . $key, 'class' => 'u-full-width']) }}
#endforeach
The initial edit form is fine but if I experience an issue (for example a field being empty) when I return to the original view (through our controller) it throws the following error:
htmlentities() expects parameter 1 to be string, array given
Bizarrely, it works fine for the colour[] fields but seems to be throwing an error for the heading[] fields.
Any thoughts on why this might be happening?
Cheers
Cole
You get the error htmlentities() expects parameter 1 to be a string, array given because the [] appended to heading and colour make them an array in HTML and when POSTed.
You're better off doing something like this (in PHP, in your controller, when you submit):
$colourArray = [];
foreach(Input::get('colours') as $colour) {
array_push($colourArray, htmlentities($colour));
}
Then you'll have stripped colours in the $colourArray.
I haven't actually tested this, but I hope it makes sense.

Categories