I have this table
id | Type_number | Number
1 fax 01234
2 mobile 789-123
3 phone 000-555
4 fax 022354
5 phone 000-687
I need to group by type and list the number in group by type
Example
FAX
01234
022354
mobile
789-123
phone
000-555
000-687
if i have a this code :
<div class="colour_container">
{% for rec in list_number if rec.type == "fax" %}
{{ rec.type_number }} {{ rec.number }}
{% endfor %}
</div>
<div class="colour_container">
{% for rec in list_number if rec.type == "phone" %}
{{ rec.type_number }} {{ rec.number }}
{% endfor %}
</div>
The cycle "for" function only for the first loop "fax" in the second no.
why ?
It works for me. Just try this example.
index.php
<?php
require 'vendor/autoload.php';
$app = new \Slim\Slim(array(
'view' => new \Slim\Views\Twig()
));
$view = $app->view();
$view->parserExtensions = array(
new \Slim\Views\TwigExtension()
);
$view->parserOptions = array(
'debug' => true
);
$menu = array(
array(
"id" => 1,
"type" => "fax",
"number" => "01234"
),
array(
"id" => 2,
"type" => "mobile",
"number" => "789-123"
),
array(
"id" => 3,
"type" => "phone",
"number" => "000-555"
),
array(
"id" => 4,
"type" => "fax",
"number" => "022354"
),
array(
"id" => 5,
"type" => "phone",
"number" => "000-687"
)
);
$app->get('/', function () use($app, $menu) {
$app->render('index.twig', array(
'list_number' => $menu,
'types' => array("fax", "mobile", "phone")
));
});
$app->run();
index.twig
<div class="colour_container">
{% for type in types %}
{% set printed = false %}
{{ type }}
<br/>
{% for rec in list_number if rec.type == type %}
{{ rec.number }}<br/>
{% endfor %}
<br/>
{% endfor %}
</div>
Related
I'd like to display two lists of events : one list with events created by organizers with organizer status and a list with events created by organizers with member status. My conditions are like this : "if some event exist" and "if the organizer who created the event is an organizer (or a regular member)". For information, "organizer" and "member" depend on the status of the organizer. Because of the second condition, I had this error :
Key "organizer" for array with keys "0" does not exist.
I don't understand why... What needs to be corrected ?
Thank you very much for your help.
My twig file :
{% extends 'base.html.twig' %}
{% block title %}Liste des activités{% endblock %}
{% block main %}
<div class="events">
<div class="vr fixed-top start-50"></div>
<div class="row">
<div class="col-12 col-md-6">
<h2 class="text-center my-4">
<img src="{{ asset('img/titres/zpeak-sorties.svg') }}" alt="Les Zpeak Sorties !">
</h2>
<ul class="list-group">
{% if events and events.organizer.status == 'organizer' %}
{% for event in events %}
<a class="list-group-item list-group-item-action">
<img src="{{ asset('img/flag_images/' ~ event.spokenlanguage.image) }}" alt="Drapeau {{ event.spokenlanguage.name }}" class="me-2"> {{ event.title }}
</a>
{% endfor %}
{% else %}
<p>Il n'y a pas de zpeak sortie organisée.</p>
{% endif %}
</ul>
</div>
<div class="col-12 col-md-6">
<h2 class="text-center my-4">
<img src="{{ asset('img/titres/zpeak-idees.svg') }}" alt="Les Zpeak Idées !">
</h2>
<ul class="list-group">
{% if events and events.organizer.status == 'member' %}
{% for event in events %}
<a class="list-group-item list-group-item-action">
<img src="{{ asset('img/flag_images/' ~ event.spokenlanguage.image) }}" alt="Drapeau {{ event.spokenlanguage.name }}" class="me-2"> {{ event.title }}
</a>
{% endfor %}
{% else %}
<p>Il n'y a pas de zpeak idée proposée.</p>
{% endif %}
</ul>
</div>
</div>
</div>
{% endblock %}
My EventsController.php file :
<?php
namespace App\Controller\Front;
use App\Form\SearchType;
use App\Repository\EventsRepository;
use App\Repository\CategoriesRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class EventsController extends AbstractController
{
#[Route('/search', name: 'search')]
public function search(Request $request, SessionInterface $sessionInterface)
{
$data = $request->request->all();
$sessionSearchFormData = $sessionInterface->get('searchFormData');
$form = $this->createForm(SearchType::class, ['data' => $sessionSearchFormData]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$sessionInterface->set('searchFormData', $data);
return $this->redirectToRoute('events', [$data]);
}
return $this->renderForm('front/search.html.twig', [
'form' => $form
]);
}
#[Route('/events', name: 'events')]
public function events(
EventsRepository $eventsRepository,
CategoriesRepository $categoriesRepository
){
$events = $eventsRepository->findAll();
dd($events);
$categories = $categoriesRepository->findAll();
return $this->render("front/events.html.twig", ['events' => $events, 'categories' => $categories]);
}
}
dd($events) in EventsController.php :
EventsController.php on line 45:
array:1 [▼
0 => App\Entity\Events {#1026 ▼
-id: 1
-title: "Soirée à Belleville"
-description: "Viens boire un verre, rencontrer des natifs et pratiquer l'espagnol !"
-category: Proxies\__CG__\App\Entity\Categories {#933 ▶}
-spokenlanguage: Proxies\__CG__\App\Entity\Language {#981 ▶}
-address: Proxies\__CG__\App\Entity\Location {#744 ▶}
-date: DateTime #1665446400 {#1022 ▶}
-starttime: DateTime #72000 {#1023 ▶}
-endtime: DateTime #86340 {#1024 ▶}
-organizer: Proxies\__CG__\App\Entity\User {#996 ▼
-id: 1
-email: null
-roles: []
-password: null
-gender: null
-lastname: null
-firstname: null
-birthdate: null
-occupation: null
-photo: null
-status: null
-nationality: null
-nativelanguage: null
+__isInitialized__: false
…2
}
-participations: Doctrine\ORM\PersistentCollection {#1002 ▶}
}
]
In your case, you can have many Events, so in your twig, you need to use a for loop.
Place your for loop before the if.
Something like this :
<ul class="list-group">
{% for event in events %}
{% if event and event.organizer.status == 'organizer' %}
<a class="list-group-item list-group-item-action">
<img src="{{ asset('img/flag_images/' ~ event.spokenlanguage.image) }}" alt="Drapeau {{ event.spokenlanguage.name }}" class="me-2"> {{ event.title }}
</a>
{% else %}
<p>Il n'y a pas de zpeak sortie organisée.</p>
{% endif %}
{% endfor %}
</ul>
In case you are sure to have only one item, you can use events[0].organizer.status == 'organizer'
Regards,
i'm working with ckanext-scheming and trying to add a custom field similar to repeating subfields with few modifications (below are few files that define the field and it's behavior), for the most part it works correct but it doesn't get submitted to the database (although once i intercept the package create action, i can see the field data is there), any ideas are welcomed:
the scheming yaml part:
- field_name: tag_string
label: Tags
form_snippet: custom_tags.html
display_snippet: modified_repeating_subfields.html
help_text: >-
Additional keywords useful for ...
the form snippet:
{% import 'macros/form.html' as form %}
{% asset 'ckanext-dalrrdemcdcpr/facets-active-js' %}
{%
set options=[
{'value': '001', 'text': _('Discipline')}
, {'value': '002', 'text': _('Place')}
, {'value': '003', 'text': _('Stratum')}
, {'value': "004", "text":_("Temporal")}
, {'value': "005", "text":_("Theme")}
]
%}
{%- set extra_html = caller() if caller -%}
{% set classes = (classes|list) %}
{% call form.input_block(field.field_name, field.label, "", classes, extra_html=extra_html, is_required=false) %}
<div class="repeating_custom_tag_row-index-0" id="repeating_custom_tag_row-index-0" data-module="repeating_tags_handler">
<div class="metadata_tags_holder">
<div class="metadata_tags_styler">
<div class="metadata_tags_input">
<label for="custom_delete" class="input-group-text">{{ _('Input tag') }}</label>
<input class="form-control" id="repeating_custom_tag_row-index0-tag-input-index-0" type="text" name="repeating_custom_tag_row-index0-tag-input-index-0" value="" placeholder=""/>
</div>
<div class="metadata_tags_type_select">
<div><label for="tag_select" class="input-group-text">{{_('Input tag type')}}</label></div>
<select id="tag_select" name="repeating_custom_tag_row-index0-select-input-index-0" required>
{% for option in options %}
<option value="{{ option.value }}"{% if option.value == selected %} selected{% endif %}>{{ option.text or option.value }}</option>
{% endfor %}
</select>
</div>
<div class="tags_buttons_wrapper">
<div class="tags_handling_buttons">
<button class="btn btn-success add_metadata_tags_btn">Add</button>
<button class="btn btn-danger remove_metadata_tags_btn"><i class="fa fa-trash"></i>Remove</button>
</div>
</div>
</div>
</div>
</div>
{% endcall %}
the display snippet (don't think it's useful):
{% set fields = data[field.field_name] %}
{% block subfield_display %}
{% for field_data in fields %}
<div class="panel panel-default">
<div class="panel-body">
<dl class="scheming-subfield-list">
{% for subfield in field.repeating_subfields %}
<dt class="dataset-label">
{{ h.scheming_language_text(subfield.label) }}
</dt>
<dd>
{%- snippet 'scheming/snippets/display_field.html',
field=subfield,
data=field_data,
entity_type=entity_type,
object_type=object_type
-%}
</dd>
{% endfor %}
</dl>
</div>
</div>
{% endfor %}
{% endblock %}
this is how the relevant part of the form looks (the tags):
the js module controlling tags fields behavior:
ckan.module("repeating_tags_handler", function ($){
/*
control muliple interaction regarding the tags
section, handles both css and buttons behavior.
*/
return {
initialize: function(){
$.proxyAll(this,/_on/);
// set the styles before copying the section
let add_btns = $(".add_metadata_tags_btn")
add_btns.each((idx,add_btn) => {add_btn.style.marginRight="2px"})
$(document).on('click','.add_metadata_tags_btn',this._on_add_tags)
$(document).on('click','.remove_metadata_tags_btn',this._on_remove_tags)
let repeating_fields_wrapper = $(".repeating_custom_tag_row-index-0")
repeating_fields_wrapper.css({"margin-bottom":"30px"})
let tags_holder = $(".metadata_tags_holder")
tags_holder.css("width:100%")
$(".metadata_tags_styler").css({"width":"100%", "display": 'flex', 'flex-direction': 'row'})
$(".metadata_tags_input").css({"width":"40%"})
$(".metadata_tags_type_select").css({"width":"30%"})//tag_select
$("#tag_select").css({"width":"100%", "height":"34px", "margin-left":"4px"})
$(".tags_buttons_wrapper").css({"margin-left": "4px", "padding-top": "2px"})
$(".tags_handling_buttons").css({"width":"100%","padding-top":"22px", "display": "flex", "flex-direction": "row", "margin-left":"4px"})
this.section_html = tags_holder.html()
const config = { attributes: true, childList: true, subtree: true };
//const added_items_observer = new MutationObserver(this.added_items_mutation_observer);
//added_items_observer.observe(repeating_fields_wrapper.get(0), config)
},
_on_add_tags:function(e){
e.preventDefault()
//let search_parent = e.target.parentElement.parentElement.parentElement.parentElement
let search_parent = e.target.closest(".metadata_tags_styler")
let new_index = Array.from(search_parent.parentNode.children).indexOf(search_parent) +1
let new_index_text = `index-${new_index}`
this.section_html = this.section_html.replace(/index-[0-9]/g, new_index_text)
this.el.append(this.section_html)
},
_on_remove_tags:function(e){
e.preventDefault()
let removed_tag_row = e.target.closest(".metadata_tags_styler")
//let removed_tag_row = e.target.parentElement.parentElement.parentElement.parentElement
removed_tag_row.remove()
},
}
})
and finally this is a Runtime error screenshot i raised for you - dear - to see the tags data is there:
thank you!
im new using laravel and i get a problem.
and there is my var_dump (post request)
+request: Symfony\Component\HttpFoundation\ParameterBag {#44 ▼
#parameters: array:12 [▼
"_token" => "R1fnZMtNA55fy1plkTuztuhMZ1ExvkSw01GJq4rN"
"spg" => "test"
"nama" => "Reynaldo"
"alamat" => "123"
"hp" => "2134"
"ccs" => "1"
"cos" => "2"
"oss" => "Qty"
"tvs" => "Qty"
"tcs" => "Qty"
"lngs" => "Qty"
"tks" => "Qty"
]
this if my HTML form:
#foreach ($products as $product)
<div class="form-group row col-md-12">
<label for="{{ $product->slug }}" class="col-sm-3 col-form-label">{{ $product->nama }}</label>
<div class="col-sm-7">
<select class="custom-select col-sm-2" name="{{ $product->slug }}" required>
<option hidden>Qty</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
</div>
#endforeach
the problem is, how can I modify post request (add array and don't send 'qty' value)?
i want to look like this :
+request: Symfony\Component\HttpFoundation\ParameterBag {#44 ▼
#parameters: array:12 [▼
"_token" => "R1fnZMtNA55fy1plkTuztuhMZ1ExvkSw01GJq4rN"
"spg" => "test"
"nama" => "Reynaldo"
"alamat" => "123"
"hp" => "2134"
product array [
"ccs" => "1"
"cos" => "2"
"oss" => "Qty"
"tvs" => "Qty"
"tcs" => "Qty"
"lngs" => "Qty"
"tks" => "Qty"
]
]
very grateful if someone helps. sorry for my broken English.
thankyou.
try this
<select class="custom-select col-sm-2" name="{{ $product->slug }}[]" required>
now your select data come as array formate and its name will be $product->slug's name
I have a list in October CMS like this:
<ul>
<li {% if this.page.id=='some-id' %} class = "active" {% endif %}>
Item 1
</li>
<li {% if this.page.id=='some-id' %} class = "active" {% endif %}>
Item 2
</li>
<li {% if this.page.id=='some-id' %} class = "active" {% endif %}>
Item 3
</li>
</ul>
However, as you can see there is a lot of repitition so I decided to create array variables. I can pass on a single array variable but I am not sure how to do it for multiple arrays so my code can look something like this:
==
function onStart(){
$this['pageData']=[
'data' => [
'item' => ['Item 1','Item 2','Item 3'],
'id' => ['pageid1','pageid2','pageid3'],
'link' => ['dir/page1','dir/page2','dir/page3']
]
];
}
==
<ul>
{% for items in pageData %}
<li {% if this.page.id == items.data.id %} class="active" {% endif %}>
{{items.data.item}}
</li>
{% endfor %}
</ul>
I know this code is wrong but this is the idea. I can retrieve one object array if I write:
{% for items in pageData.data.item %}
<li>{{items}}</li>
{% endfor %}
But this of course only gives me a list of items inside the 'item' object. I hope I explained it well. Please let me know if I can provide more information to get this problem solved.
You can write it like this
==
function onStart(){
$this['records'] = [
[
'id' => 'pageid1',
'item' => 'Item 1',
'link' => 'dir/page1'
],
[
'id' => 'pageid2',
'item' => 'Item 2',
'link' => 'dir/page2'
],
[
'id' => 'pageid3',
'item' => 'Item 3',
'link' => 'dir/page3'
],
];
}
====
<ul>
{% for record in records %}
<li {% if this.page.id == record.id %} class="active" {% endif %}>
{{record.item}}
</li>
{% endfor %}
</ul>
this will work
if any doubts please comment.
I have a multi-select option and i want to save all record in database, now it's saving only the last one, how can I do that? I need to save multi-select from tags with comma (,) between. .
Here is my controller and what I tried
$news = News::create([
'locale' => Session::get('admin_locale'),
'title' => $request['title'],
'slug' => Slugify::slugify($request['title']),
'news_class' => $request['news_class'],
'description' => $request['description'],
'tag' => $request['tag'],
'tags' => $request->input['tags'],
'category' => 'news',
'category_id' => $request['category_id'],
'metatitle' => $request['title'],
'metadescription' => substr(strip_tags($request['description']), 0, 160),
'image' => $image,
]);
Here is my view:
<div class="row d-flex justify-content-center mt-100 col-md-12 g-mb-30" >
<div class="col-md-12" >
<label class="g-mb-10">Tags</label>
<select id="choices-multiple-remove-button" placeholder="Select" multiple title="Category Talent" name="tags">
#foreach($news as $tag)
<option value="{{ $tag->tag }}">{{ $tag->tag }}</option>
#endforeach
</select> </div>
</div>
To pass multiple values you probably want to rename your input to be an array:
<select ... name="tags[]">
Then on the server side you should receive them as an array under the input tags:
$tags = $request->input('tags', []);
You can join the array elements with implode to get a string representation:
$tags = implode(',', $tags);
PHP Manual - Function Reference - Text Processing - Strings - Functions - implode
$news = News::create([
...
'tags' => implode(',', $request->input('tags', [])),
...
]);