(and sorry for my English)
I am working on a Silex project and I start using the form.
Without form, i have something like this :
And using the form I would like to have the same thing (for the select)
End with the form i have this :
As you can see the rendering is not great.
I would like to get a few things like:
<option value="1">Méthode</option>
<option value="2">Marketing</option>
<option value="3">Production</option>
At the code level, I have something very simple (
I start on the forms, be indulgent haha) :
Controller :
public function add(Application $app, Request $request) {
if (! $app['security.authorization_checker']->isGranted('ROLE_ADMIN')) {
return new Response('Access Denied!', 403);
}
$repo = new DepartementRepository($app);
$departements = $repo->getAllDepartements();
$data = [];
$erreurs = [];
$form = $app['form.factory']->createBuilder(FormType::class, $data)
->add('nom',null,array('label' => 'Nom :'))
->add('prenom',null,array('label' => 'Prenom :'))
->add('departement_id',ChoiceType::class,array('choices' => $departements,'label' => 'Departement :'))
->add('ville',null,array('label' => 'Ville :'))
->add('salaire',null,array('label' => 'Salaire :'))
->add('date_embauche',null,array('label' => 'Date d\'embauche :'))
->getForm();
$form->handleRequest($request);
if ($form->isValid())
{
$data = $form->getData();
}
return $app['twig']->render('employe/new.html.twig', array('form' => $form->createView(),'erreurs'=> $erreurs));
}
if i dump my $departements variable, i have this :
array:4 [▼
0 => array:2 [▼
"id" => "2"
"nom_dep" => "Marketing"
]
1 => array:2 [▼
"id" => "3"
"nom_dep" => "Méthode"
]
2 => array:2 [▼
"id" => "1"
"nom_dep" => "Production"
]
3 => array:2 [▼
"id" => "4"
"nom_dep" => "Recherche et developpement"
]
]
And my view :
<form action="#" method="post">
{{ form_widget(form) }}
<input type="submit" name="submit" />
</form>
Before I did that:
{% for departement in departements %}
<option value="{{ departement.id }}"
{% if donnees.departement_id is defined and departement.id == donnees.departement_id %}selected{% endif %}>
{{ departement.nom_dep }}
</option>
{% endfor %}
But I do not know how to do this with the form.
Any help is welcome.
Thanks !!
ChoiceType takes an array as parameter:
Array style (like your current repo)
$departements = $repo->getAllDepartements();
$departementChoices = [];
foreach( $departements as $departement) {
$departementChoices[$departement["id"]] = $departement["nom_dep"]
}
Object style
$departements = $repo->getAllDepartements();
$departementChoices = [];
foreach( $departements as $departement) {
$departementChoices[$departement->getId()] = $departement->getLabel()
}
Related
I have a ecommerce website which shows multiple variants like size, color etc...I have create a products details array as below.
array:1 [▼
0 => array:12 [▼
"proId" => 2268
"name" => "PAMPERS BABY DRY PANTS LARGE -64 PANTS"
"oprice" => 1099.0
"ofprice" => 1044.05
"slug" => "pampers-pants-large-64s-9-14kg"
"variants" => array:5 [▼
0 => array:2 [▼
"title" => "NOS"
"items" => array:1 [▼
0 => array:7 [▼
"items_id" => 567
"items_value" => "42 NOS"
"sku" => "1058898"
"ean" => "4902430645577"
"mrp" => 699.0
"mfp" => 664.05
"combo" => ""
]
]
]
1 => array:2 [▼
"title" => "NOS"
"items" => array:1 [▼
0 => array:7 [▼
"items_id" => 568
"items_value" => "24 pants"
"sku" => "PM50"
"ean" => "4902430900485"
"mrp" => 399.0
"mfp" => 379.05
"combo" => ""
]
]
]
2 => array:2 [▶]
3 => array:2 [▶]
4 => array:2 [▼
"title" => "Weight"
"items" => array:1 [▼
0 => array:7 [▼
"items_id" => 591
"items_value" => "100gm"
"sku" => "wet"
"ean" => "346"
"mrp" => 436.0
"mfp" => 33.0
"combo" => ""
]
]
]
]
]
]
The code for the above is,
$product = Products::select('products.*', 'brand.name', 'category.id as cat_id')->join('category', 'category.id', '=', 'products.category_id')->join('subcategory', 'subcategory.id', '=', 'products.subcategory_id')->join('brand', 'brand.id', '=', 'products.brand_id', 'left')->where(array('products.slug' => $slug, 'products.status' => 1))->get();
$datas = [];
foreach ($product as $pro) {
$item = [
"proId" => $pro->id,
"name" => $pro->title,
"oprice" => $pro->org_price,
"ofprice" => $pro->off_price,
"slug" => $pro->slug,
"variants" => []
];
$variantlist = Provariants::select('products_variants.id', 'variants.name', 'products_variants.variants_type_id', 'products_variants.sku', 'products_variants.ean', 'products_variants.price', 'products_variants.mfp', 'products_variants.combo')->join('variants_type', 'variants_type.id', '=', 'products_variants.variants_type_id')->join('variants', 'variants.id', '=', 'variants_type.variants_id')->where(array('products_variants.products_id' => $pro->id, 'products_variants.status' => 1))->get();
foreach ($variantlist as $list) {
$typelist = Variantstype::where(array('id' => $list->variants_type_id, 'status' => 1))->get();
$subitems = [];
foreach ($typelist as $type) {
$subitems[] = [
"items_id" => $list->id,
"items_value" => $type->name,
"sku" => $list->sku,
"ean" => $list->ean,
"mrp" => $list->price,
"mfp" => $list->mfp,
"combo" => $list->combo
];
}
$item['variants'][] = [
"title" => $list->name,
"items" => $subitems,
];
}
$datas[] = $item;
}
Now the problem is, the title is showing multiple times and one items displaying under it. To get an idea check below image.
https://i.stack.imgur.com/tLjc9.png
I want title to be shown once and all the items under each title should be place together. Please check the required output.
https://i.stack.imgur.com/RFddf.png
Current result code is,
#if (count($pro['variants']) > 0)
#foreach ($pro['variants'] as $variants)
<div class="variants mt-4">
<div>
<h6>{{ $variants['title'] }}</h6>
#foreach ($variants['items'] as $var_items)
<p>{{ $var_items['items_value'] }}</p>
#endforeach
</div>
</div>
#endforeach
#endif
How can I achieve the required output?
A similar solution to the one by #sta, but I think this might be a little more flexible.
#php
$title = '';
#endphp
#foreach ($pro['variants'] as $variant)
<div class="variants mt-4">
<div>
#if($title !== $variant['title'])
<h6>{{ $variant['title'] }}</h6>
#endif
#foreach($variant['items'] as $var_item)
<p>{{ $var_item['items_value'] }}</p>
#endforeach
</div>
</div>
#php
$title = $variant['title'];
#endphp
#endforeach
I have not tested this, but it should write out the title every time $variant['title'] changes.
This will work :
#php
$x = 1;
#endphp
#foreach ($pro['variants'] as $variants)
<div class="variants mt-4">
<div>
#if($x == 1)
<h6>{{ $variants['title'] }}</h6>
#endif
#php
$x += $x;
#endphp
#foreach ($variants['items'] as $var_items)
<p>{{ $var_items['items_value'] }}</p>
#endforeach
</div>
</div>
#endforeach
Above code tested here
I have cloned HTML prepared with input group. Some checkbox and radio are not selected. I want to push into the selected email.
I've tried many methods.
array_merge()
array_combine()
array_push()
array_merge_recursive()
But none of these worked. Or I don't know how to get them to work.
HTML
<div class="form-group">
<label for="name">E-posta</label>
<div class="input-group">
<input type="email" class="form-control email-address" name="email[]" placeholder="E-Posta giriniz">
<div class="btn-group">
<label class="btn btn-default">
<input class="primary-radio" type="radio" name="primary[]" checked autocomplete="off"> <span
class="fas fa-star"></span>
</label>
<label class="btn btn-default">
<input class="ban-checkbox" type="checkbox" name="ban[]" autocomplete="off"> <span
class="fas fa-ban"></span>
</label>
<label class="btn btn-default">
<input class="invalid-checkbox" type="checkbox" name="invalid[]" autocomplete="off"> <span
class="fas fa-exclamation-circle"></span>
</label>
</div>
</div>
</div>
PHP
public function add(Request $request)
{
$emails = $request->input('email');
$primary = $request->input('primary');
$ban = $request->input('ban');
$invalid = $request->input('invalid');
$emailAddresses = array();
foreach ($emails as $emailKey => $emailValue) {
$emailAddresses[$emailKey] = [
'emailAddress' => $emailValue,
'invalid' => $invalid,
'lower' => $emailAddresses,
'ban' => $ban,
'primary' => $primary
];
}
dd($emailAddresses);
}
Output
array:3 [▼
0 => array:5 [▼
"emailAddress" => "dodis#live.com"
"invalid" => null
"lower" => "dodis#live.com"
"ban" => array:2 [▼
0 => "on"
1 => "on"
]
"primary" => array:1 [▼
0 => "on"
]
]
1 => array:5 [▼
"emailAddress" => "test#live.com"
"invalid" => null
"lower" => "test#live.com"
"ban" => array:2 [▼
0 => "on"
1 => "on"
]
"primary" => array:1 [▼
0 => "on"
]
]
2 => array:5 [▼
"emailAddress" => "bundayok#live.com"
"invalid" => null
"lower" => "bundayok#live.com"
"ban" => array:2 [▼
0 => "on"
1 => "on"
]
"primary" => array:1 [▼
0 => "on"
]
]
]
Such an output comes. It shouldn't be like that. Here's the example I want it to be.
0: {
emailAddress: "bilgi#ekinciler.com.tr"
invalid: false
lower: "bilgi#ekinciler.com.tr"
optOut: false
primary: true
}
1: {
emailAddress: "muhasebe#ekinciler.com"
invalid: false
lower: "muhasebe#ekinciler.com"
optOut: false
primary: false
}
I am sorry for my English. I hope I can. I would be glad if you help.
Try below code,
What I have done is added array index to each loop to get correct value of that index, here your
"ban" => array:2 [▼
0 => "on"
1 => "on"
]
is array itself so you need 0 index for the first array data. and same applies for all the array.
public function add(Request $request)
{
$emails = $request->input('email');
$primary = $request->input('primary');
$ban = $request->input('ban');
$invalid = $request->input('invalid');
$emailAddresses = array();
$i = 0;
foreach ($emails as $emailKey => $emailValue) {
$emailAddresses[$emailKey] = [
'emailAddress' => $emailValue,
'invalid' => $invalid[$i]?$invalid[$i]:'',
'lower' => $emailAddresses,
'ban' => $ban[$i]?$ban[$i]:'',
'primary' => $primary[$i]?$primary[$i]:''
];
$i++;
}
dd($emailAddresses);
}
I have the following array result set, I'm trying to loop through each of the results and just echo them out onto the page. I'm using Laravel 5.2 and the blade templating engine
Collection {#240 ▼
#items: array:3 [▼
0 => array:2 [▼
"name" => "desktop"
"views" => "349"
]
1 => array:2 [▼
"name" => "mobile"
"views" => "151"
]
2 => array:2 [▼
"name" => "tablet"
"views" => "68"
]
]
}
This is what I have so far
#foreach($devices as $device)
$key = 0; $key++; $key < 2;
{{ $device[$key] }},
#endforeach
#foreach($devices as $device)
{{ $device->name }}
{{ $device->views}}
#endforeach
Will be enough.
You need to echo object properties:
#foreach($devices as $device)
{{ $device->name }} has {{ $device->views }}
#endforeach
If you like to use key then
#foreach($devices as $key => $val)
{{ $device[$key]->name }},
{{ $device[$key]->views }}
#endforeach
I am passing a variable $mailchimp from my Controller to my View.
this is what I got with {{dd($mailchimp)}}
array:8 [▼
"id" => "xyz123"
"email_address" => "john.doe#discworld.com"
"unique_email_id" => "c9a36649c8"
"email_type" => "html"
"status" => "subscribed"
"merge_fields" => array:2 [▼
"FNAME" => "John"
"LNAME" => "Doe"
]
"stats" => array:2 [▼
"avg_open_rate" => 0
"avg_click_rate" => 0
]
"list_id" => "769808qeqw92"
]
how can I loop through this array ($mailchimp) ? With the code below I get an exception: "htmlentities() expects parameter 1 to be string, array given"
#foreach($mailchimp as $user)
#if(is_array($user))
#foreach($user as $key => $value)
{{$value}}
#endforeach
#endif
#endforeach
Update:
With this Code in My Controller
public function index()
{ //Fetch all subscribers from DB
$subscribers = Subscriber::where('user_id', Auth::user()->id)->orderBy('created_at','asc')->get();
foreach ($subscribers as $key => $subscriber) {
//Check if the local subscriber is also present in mailchimp
$mailchimp = Newsletter::getMember($subscriber->email);
}
return view('backend.newsletter.contacts.index')->withSubscribers($subscribers)
->withMailchimp($mailchimp);
}
I need to iterate the mailchimp array. As there are multiple users, alexey's suggestion doesn't work out anymore.
This stil doesn't work:
#foreach($mailchimp as $key => $user)
{{$user}}
#endforeach
You don't need to iterate over $user. If $mailchimp is an array of users, do this:
{{ $mailchimp['email_adress'] }}
{{ $mailchimp['merge_fields']['FNAME'] }} {{ $mailchimp['merge_fields']['LNAME'] }}
Since you are only interested in printing the values in your array, you can use array_flatten to get rid of the nested arrays, and then loop through the result:
#foreach(array_flatten($mailchimp) as $userData)
{{$userData}}
#endforeach
I have completely changed the format of my array now, and I had what I hope is a simple misunderstanding on my part. So my array now looks like the following
array:9 [▼
0 => array:4 [▼
"leadData" => array:7 [▼
"LeadID" => "1232806"
"Client" => "Some Client"
"LeadName" => "Test"
"Owner" => "Someone"
"Value" => "2160.00"
]
"clientData" => array:2 [▼
"Prospect" => "No"
]
"quoteData" => array:8 [▼
"QuoteID" => "Q0020"
"ProjectName" => "Test"
"Amount" => "1234"
]
"customData" => array:2 [▼
0 => array:1 [▼
"Type" => "New"
]
1 => array:1 [▼
"Month" => "June 16"
]
]
]
2 => array:4 [
...
]
]
So it is essentially now 4 inner arrays. Now if I do the following, I can print out all the data for the leadData inner array
foreach($leadArray as $array)
<tr>
foreach($array['leadData'] as $leadKey => $leadData)
<td>
{{ $leadData }}
</td>
endforeach
</tr>
endforeach
That works fine. However, I only want to display certain parts of this array. I would have presumed doing something like the following would work
foreach($leadArray as $array)
<tr>
foreach($array['leadData'] as $leadKey => $leadData)
<td>
{{ $leadData['LeadID'] }}
</td>
<td>
{{ $leadData['LeadName'] }}
</td>
endforeach
</tr>
endforeach
However if I do this I get and Illegal String Offset error. Is this not how I would access this data?
p.s. Ignore the way I do the foreach loop etc, this is because I am using a template engine.
Thanks
You don't have to loop over the second array, you can use the keys to get the data.
foreach($leadArray as $array)
<tr>
<td>
{{ $array['leadData']['LeadID'] }}
</td>
<td>
{{ $array['leadData']['LeadName'] }}
</td>
</tr>
endforeach
Your main array will be like this, where you want to work, right?
<pre>
$aMainArray = array(
0 => array(
"leadData" => array(
"LeadID" => "1232806",
"Client" => "Some Client",
"LeadName" => "Test",
"Owner" => "Someone",
"Value" => "2160.00",
)
)
);
foreach ($aMainArray AS $aSubArray) {
print_r($aSubArray);
// You can echo your required values like below
echo $aSubArray['leadData']['LeadID'];
echo $aSubArray['leadData']['LeadName'];
// OR like this one
foreach ($aSubArray AS $value) {
echo $value['LeadID'];
echo $value['LeadName'];
}
}
</pre>