How to display array data from MySql in laravel - php

So I a Manifest where you can add multiple customers, produce etc and it saves in the DB as an array
Now I'm making a page where you can view all the manifest info I want it to look something like this
if I try to do something like this {{ $manifest->customer_name }} i get a error htmlspecialchars() expects parameter 1 to be string, array given because it's an array I'm only having trouble with displaying the array data
Controller Code to get the correct manifest
public function view($id) {
$manifests = Manifest::where('id', $id)->where('user_id', Auth::user()->id)->firstOrFail();
return view('users.manifest.view', compact('manifests'));
}
view - usually I would just put {{ $manifets->customer_name }} but i can't because it an array
<table class="table table-striped">
<thead>
<tr>
<th width="15%">Customer</th>
<th width="15%">Produce</th>
<th width="15%">Task</th>
<th width="15%">Units</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="text" class="form-control" value="{{ $manifests->customer_name }}" />
</td>
</tr>
</tbody>
</table>
print_r
App\Manifest Object
(
[table:protected] => manifest
[fillable:protected] => Array
(
[0] => user_id
[1] => date
[2] => driver_name
[3] => truck_number
[4] => run_number
[5] => customer_name
[6] => produce
[7] => task
[8] => units
)
[casts:protected] => Array
(
[customer_name] => array
[produce] => array
[task] => array
[units] => array
)
[connection:protected] => mysql
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 8
[user_id] => 1
[date] => 2017-11-02
[driver_name] => Harry Oberlander
[truck_number] => 7989
[run_number] => 8
[customer_name] => ["evergreen","Surplus"]
[produce] => ["Apples","Meat"]
[task] => ["Pick Up","Delivery"]
[units] => ["3 skids","1"]
[created_at] => 2017-11-02 04:49:49
[updated_at] => 2017-11-02 04:49:49
)
[original:protected] => Array
(
[id] => 8
[user_id] => 1
[date] => 2017-11-02
[driver_name] => Harry Oberlander
[truck_number] => 7989
[run_number] => 8
[customer_name] => ["evergreen","Surplus"]
[produce] => ["Apples","Meat"]
[task] => ["Pick Up","Delivery"]
[units] => ["3 skids","1"]
[created_at] => 2017-11-02 04:49:49
[updated_at] => 2017-11-02 04:49:49
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[events:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
)
here is the updated code
#foreach($manifests as $manifest)
<tr>
<td>
<input type="text" class="form-control" value="{{ $manifest['customer_name'] }}" />
</td>
<td>
<input type="text" class="form-control" value="{{ $manifest['produce'] }}" />
</td>
<td>
<input type="text" class="form-control" value="{{ $manifest['task'] }}" />
</td>
</tr>
#endforeach

The problem is that you are storing array (which is an object in PHP) in database.
This makes a form unrecognisable when you retrieve back the data.
You should serialise the array before storing it to database. It will save the data as a kind of JSON string.
serialize($array_var_name);
Further to get the array back you can use
unserialize($data_var);
Now you can use the array as usual array

You can do something like this:
#foreach($manifets as $manifestos)
#for ($counter = 0; $counter < sizeof($manifestos['customer_name']); $counter++)
<tr>
<td>
<input type="text" class="form-control" name="nameoffield" value="{{ $manifestos['customer_name'][$counter] }}" />
</td>
<td>
<input type="text" class="form-control" name="nameoffield" value="{{ $manifestos['otherfields'][$counter]}}" />
</td>
//more td here depending on the fields you want to display
</tr>
#endfor
#endforeach

Related

show difference between of two price list

I have history list of product price(order by created_at) like this:
Array
(
[0] => stdClass Object
(
[id] => 1
[product_id] => 49
[price] => 14520000.0000
[created_at] => 1592501154
)
[1] => stdClass Object
(
[id] => 2
[product_id] => 49
[price] => 14000000.0000
[created_at] => 1592587554
)
[2] => stdClass Object
(
[id] => 4
[product_id] => 49
[price] => 14800000.0000
[created_at] => 1592673954
)
[3] => stdClass Object
(
[id] => 5
[product_id] => 49
[price] => 10000000.0000
[created_at] => 1592760354
)
[4] => stdClass Object
(
[id] => 6
[product_id] => 49
[price] => 14000000.0000
[created_at] => 1592846754
)
[5] => stdClass Object
(
[id] => 7
[product_id] => 49
[price] => 14000000.0000
[created_at] => 1592933154
)
[6] => stdClass Object
(
[id] => 8
[product_id] => 49
[price] => 14000000.0000
[created_at] => 1593019554
)
)
Now for show data in table I listed price using foreach method like this:
<?php foreach($product_prices_list as $product_price_list):?>
<tr>
<td><?= esc($product_price_list->created_at);?></td>
<td class="text-center"><?= esc(number_format($product_price_list->price));?></td>
<td class="text-center"></td> //show difference between of two price
</tr>
<?php endforeach;?>
I can see true output in table but I need to show difference between of two price in the third column like this picture:
how do can i show difference between of two price in my list?!
You just need to compare the current price property to the price from the previous object in the array?
Something like this should work:
<?php foreach($product_prices_list as $key => $product_price_list):?>
<tr>
<td><?= esc($product_price_list->created_at);?></td>
<td class="text-center"><?= esc(number_format($product_price_list->price));?></td>
<td class="text-center"><?= (!empty($product_prices_list[$key - 1])) ? $product_prices_list[$key + 1]->price - $product_price_list->price: 0; ?></td> //show difference between of two price
</tr>
<?php endforeach;?>
If you are running MySQL 8.0, you can compute this information directly in the database using window functions:
select
t.*,
price
- lag(price, 1, price) over(partition by product_id order by created_at)
as price_diff
from mytable t
This adds one more column to your resultset, that contains the difference between the current price and the previous price of the same product.

how i can access to this values? php

I want print: Id, Nombre_del_paciente__c, Fecha_de_la_cita__c and Hora_de_la_cita__c
This is print_r($response); result:
Object (
[queryLocator] => [done] => 1 [records] => Array (
[0] => SObject Object (
[type] =>
Cita__c [fields] => stdClass Object
(
[Nombre_del_paciente__c] => 0030O000021cPBuQAM
[Fecha_de_la_cita__c] => 2017-11-28
[Hora_de_la_cita__c] => 15:30
)
[Id] => a000O00000tmZH6QAM
)
) [size] => 1
)
I do this and I can acces to ID value... but i cant acceso to other values:
<table>
<tr>
<th>ID </th>
<th>Nombre_del_paciente__c</th>
<th>Fecha_de_la_cita__c</th>
<th>Hora_de_la_cita__c</th>
</tr>
<?php
foreach ($response->records as $record) {
echo '<tr>
<td>'.$record->Id.'</td>
<td></td>
<td></td>
<td></td>
</tr>';
}
?>
</table>
Im try to do:
$record->type->Nombre_del_paciente__c
$record->Cita__c ->Nombre_del_paciente__c
$record->Cita__c['fields'] ->Nombre_del_paciente__c
but i cant acces to values
Your key 'type' is not a object array. you can try this-
$record->type['fields']->Nombre_del_paciente__c
$record->type['fields']->Fecha_de_la_cita__c
$record->type['fields']->Hora_de_la_cita__c

PHP Laravel display array as HTML

I'm quite new to PHP Blade/Laravel and I'm trying to display an array, which is inside an variable, as HTML.
So I got an variable named $products and first of all I did this:
{{ print_r($products) }}
This gave me:
Array
(
[0] => Array
(
[title] => Belt
[image] => img/products/belt.jpg
[price] => 79.00
[specialPrice] =>
)
[1] => Array
(
[title] => Hat
[image] => img/products/hat.jpg
[price] => 89.00
[specialPrice] => 69.00
)
[2] => Array
(
[title] => Bag
[image] => img/products/bag.jpg
[price] => 99.00
[specialPrice] => 59.00
)
[3] => Array
(
[title] => Scarf
[image] => img/products/scarf.jpg
[price] => 119.00
[specialPrice] =>
)
)
1
So, I want to display this Array as HTML. I tried this:
#foreach($products as $key => $item)
<p>{{#item->title}}</p>
#endforeach
But that didnt work. What am I doing wrong?
Try this :
#foreach($products as $product)
<p>{{$product->title}}</p>
#endforeach
or this
#foreach($products as $product)
<p>{{$product[title]}}</p>
#endforeach

How to access multidimensional array using django templates in html page

I have a multidimensional array in php page like this:
$comment=Array
(
[655436] =>
[655435] => Array
(
[id] => 5864928
[shortmessage] => rytrht
)
[655434] => Array
(
[id] => 5864934
[shortmessage] => vcxv
)
[654990] =>
[654989] => Array
(
[id] => 5864948
[shortmessage] => fgfj
)
[654983] => Array
(
[id] => 5864909
[shortmessage] => state
)
[654981] =>
[654979] =>
[654978] => Array
(
[id] => 5864933
[shortmessage] => ggjj
)
[654329] => Array
(
[id] => 5864974
[shortmessage] =>
)
)
echo $renderer->render('sample.html', array( 'index' => commentId,'Comment' => $comments));
I wish to use the $comment array values in HTML page using Django temlates with out looping over the array
sample.html:
<td><input class="Comment" type="text" value="{{Comment[index]['shormessage']}}" /></td>
But {{Comment[index]['shormessage']}} not giving any value and the whole page itself not displaying
I dont know how to access the multidimensional array.
Is there any way to do this?
Use For Loop for this
Try by Using
{% for Comment in Comments %}
<input class="Comment" type="text" value="{{Comment.shormessage}}" />
{%endfor%}

How to group models array in yii2

Hi everyone i have a page on which i am displaying data using foreach loop from models. I wants to know that how can i group those classes and display accordingly.
For example I have registration forms on the page which has dates and classes.
So i want to group them by dates first and than classes.My registration models has fields like below.
Registration
id
event_id
dates
class_id
user_id
title
description
Now on this view file i am passing array of $modelRegistration so i dont actually have simple multi dimensional array so i cant use that logic to group that and display.Here is my view code to display the forms
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Date</th>
<th>Class</th>
<th>User Name</th>
<th>Title</th>
<th>Description</th>
</tr>
</thead>
<tbody class="container-items">
<?php foreach ($modelsRegistration as $indexRegistration => $modelRegistration): ?>
<tr value="<?= $modelRegistration->date ?>" class="house-item">
<td class="col-lg-1 col-md-1 col-sm-1 col-xs-1"><?= $modelRegistration->class->name ?></td>
<td class="col-lg-2 col-md-2 col-sm-2 col-xs-2">
<label><?= $modelRegistration->user->name ?></label>
<td class="col-lg-2 col-md-2 col-sm-2 col-xs-2"><?= $modelRegistration->title ?> </td>
<td class="col-lg-1 col-md-1 col-sm-1 col-xs-1"><?= $modelClass->description ?> </td>
</tr>
<?php endforeach; ?>
</tbody>
I want to get array like below
Registration
(
[0] => 10/12/2015
(
[0] => class-1(
[0] => array(
[title] => "Title 1"
[id] => 5
[username] => John
[Description] => Team Leader
)
[1] => array(
[title] => "Title 2"
[id] => 6
[username] => John 1
[Description] => Team 2
)
)
[1] => class-2(
[0] => array(
[title] => "Title 4"
[id] => 7
[username] => John 4
[Description] => Team 4
)
[1] => array(
[title] => "Title 5"
[id] => 9
[username] => John 5
[Description] => Team 6
)
)
[2] => class-3(
[0] => array(
[title] => "Title 1"
[id] => 5
[username] => John
[Description] => Team Leader
)
[1] => array(
[title] => "Title 2"
[id] => 6
[username] => John 1
[Description] => Team 2
)
)
)
[0] => 12/12/2015
(
[0] => class-1(
[0] => array(
[title] => "Title 8"
[id] => 12
[username] => John 10
[Description] => Team 77
)
[1] => array(
[title] => "Title 27"
[id] => 67
[username] => John 17
[Description] => Team 27
)
)
[1] => class-3(
[0] => array(
[title] => "Title 41"
[id] => 71
[username] => John 41
[Description] => Team 41
)
[1] => array(
[title] => "Title 51"
[id] => 59
[username] => John 54
[Description] => Team 64
)
)
)
If i can group it by dates first and than classes than i want to display the date only once and than under that date all the grouped classes and under classes i can display other information.Thank you
Map Your Model Data using date group Array helper Map Group in yii2
$model = Registration::findAll();
$registrationModels = ArrayHelper::map($model , 'event_id' ,'class_id' ,'dates');
Suppose that your model name is Registration and view name is vrego.
In your controller
// select all and order by date and class_name
$registrations= Registration::find()
->orderBy('date, class_name')
->all();
return $this->render('vrego', [
'registrations' => $registrations,
]);
Then in your view loop through $registrations and display each record.
i am geting the result i tried my self :
you need to create a class of Registration and i think you created that one then use this below query you will get that info .
$events = Registration::find()->all();
foreach($events as $event){
$new_array[$event->dates_date][][$event->class_id][]=$event;
}
<pre>
after print_r($new_array);
<pre>

Categories