get values of all selected checkboxes symfony2 - symfony

<th scope="col">{{ d.id }} <input type="checkbox" value="{{ d.id }}" name="checkboxD" class="checker"></th>
how to get list of values from the selected checkbox ??
my controller action
public function traiterAction() {
$IDs = array();
if ( $this->getRequest()->isMethod('POST') ) {
$IDs = $this->getRequest()->get('checkboxD');
}
return $this->render('#EgovPoste/test.html.twig',array('ids'=>$IDs));
}

You need to change the name to checkboxD[] .
I hope that will help you

Related

Creating a Search Box without a Button in Laravel 9

I have a page with a several articles and I would like to be able to search the articles by title (words in their title not the whole title). I have created a form in a column side bar on the page. I do not want to be redirected to a new page but rather to have the search filter the articles already on the page. I have tried many different things from my internet research but nothing is working out and I think I am just not seeing something. I have only been working with Laravel for a month or two so it is entirely likely!
Form in the Blade:
<form action="{{ route('search') }}" method="get">
<div class="search-box">
<input type="submit" class="search" name="search" placeholder="Search..." value="">
</div>
</form>
Routes:
Route::get('/news', [ArticleController::class, 'grid'])
->name('news');
Route::get('/news/{article_id}', [ArticleController::class, 'article'])
->where('course', '[0-9]')->name('article');
Route::get('/news/search', [ArticleController::class, 'search'])
->name('search');
Controller:
public function search(Request $request)
{
$articles = Article::all();
$articles = $articles->where('title', 'LIKE', '%' . $request . '%')
->orderBy('created_at', 'desc')
->paginate(4);
return view('search', compact('articles'));
}
public function article($slug)
{
return view('pages/article', ['article' => Article::find($slug)]);
}
public function grid(Request $request)
{
$categories = Category::all();
$users = User::all();
$tags = Tag::all();
$all = Article::with('user')
->orderBy('created_at', 'desc')
->paginate(4);
$columns = Schema::getColumnListing('articles');
return view('pages/news', compact('categories', 'users', 'tags', 'all', 'columns'));
}

Show multiple data from an Entity

I want to show the best 5 scores from each different Game I have. So I made this function :
public function records (){
$em = $this->getDoctrine()->getManager();
$games = $em->getRepository(Game::class)->findAll();
foreach($games as $g){
$records = new ArrayCollection;
$records = $em->getRepository(Game::class)->findAllRecords($g->getId());
}
return $this->render('game/records.html.twig', [
'games' => $games,
'records' => $records,
]);
}
Here is the repository function :
public function findAllRecords($id){
$qb = $this->createQueryBuilder('g');
$qb->select('g.name')
->innerJoin('g.Parties', 'p')
->innerJoin('p.playeds', 'y')
->innerJoin('y.joueur', 'j')
->addSelect('y.score')
->addSelect('j.nom, j.prenom')
->where('g.id = :id')
->setParameter('id', $id)
->orderBy('y.score', 'DESC')
->setMaxResults('5');
var_dump($qb->getDQL());
$query = $qb->getQuery();
return $query->getResult();
}
And finally the view :
{% for g in games %}
{{ g.name }}
<table class="table">
<tbody>
<tr>
<th>score</th>
</tr>
{% for r in records %}
<tr>
<td>{{ r.score }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endfor %}
It doesn't completely works as I just get the data from the last game ID. How can I show the data for each game ?
foreach($games as $g){
$records = new ArrayCollection;
$records = $em->getRepository(Game::class)->findAllRecords($g->getId());
}
Here's your issue. This is always overwriting. You want to do something like:
$records = new ArrayCollection;
foreach($games as $g) {
$records[] = $em->......;
}
That should solve your issue

show total amount of orders on the top of a sonata admin list

After filtering for a particular criteria, display total of the sales transaction amounts on top of the page. This should be total of all the pages if pages are more than one.
Can someone guide me how to do this
We did it in the following manner and it is working like a charm
Step 1 : Added two methods in orderAdmin
public function getTemplate($name)
{
if($name == 'list') {
return 'AppBundle:OrderAdmin:list.html.twig';
}
return parent::getTemplate($name); stub
}
public function getSumOf($field)
{
$datagrid = $this->getDatagrid();
$datagrid->buildPager();
$query = $datagrid->getQuery();
$query->select('SUM( ' . $query->getRootAlias() . '.' . $field . ') as total');
$query->setFirstResult(null);
$query->setMaxResults(null);
$result = $query->execute(array(), \Doctrine\ORM\Query::HYDRATE_SINGLE_SCALAR);
return $result;
}
Step 2: Created a template file list.html.twig
{% extends 'SonataAdminBundle:CRUD:base_list.html.twig' %}
{% block list_header %}
<div class="pull-right" style="margin-right: 10px;">
<label for="{{ admin.uniqid }}_sum_of_orders" class="control-label">Grand Total: </label>
<label class="control-label"> S$ {{ admin.getSumOf('orderAmount') }} </label>
</div>
{% endblock %}

Codeigniter populating drop down from database

Hello friends i am having a problem with codeigniter drop down list....
it is working finely but there is a problem which i m unable to solve...
coming direct to code here is my *mode*l ..
function getBatchId(){
$batchId = $this->db->get('batch');
$batchList = array();
if($batchId->num_rows() >0){
foreach ($batchId->result_array() as $tablerow) {
$batchList[] = $tablerow['code'];
}
return $batchList;
}else {return false;}
my controller
$this->load->model('AdmissonModel');
$content = array(
'progid' => $this->AdmissonModel->getProgrameCode(),
'batch' => $this->AdmissonModel->getBatchId(),
'depid' => $this->AdmissonModel->getDepId()
);
$this->load->view('Admissions/admForm_view',$content);
and my view
<tr>
<td>BATCH</td>
<td><?php echo form_dropdown('Batch',$batch); ?></td>
</tr>
the result produced is like this
<select name="Batch">
<option value="0">BCS12</option>
<option value="1">BCS14</option>
<option value="2">IMS01</option>
<option value="3">INU01</option>
<option value="4">INU02</option>
<option value="5">INU03</option>
</select>
now the problem is that i want the values also like BCS12,BCS14,IMS01 etc not 0,1,2,3...but the values are 0,1,2,3,4.....can any 1 help me plz...thanks alot in advance
in your model update this function to
function getBatchId(){
$batchId = $this->db->get('batch');
$batchList = array();
if($batchId->num_rows() >0){
foreach ($batchId->result_array() as $tablerow) {
$batchList[$tablerow['code']] = $tablerow['code'];
}
return $batchList;
}else {return false;}

Symfony2: entity field (html SELECT) set SELECTED item

I am using a form (filterForm) to filter entities on a twig view.
The 'filterForm' only has a field of 'entity' type. On the view it shows a HTML-SELECT-OPTIONs tag.
When the user changes the selection, the same controller is called doing the neccesary stuff to filter the entities-list.
All is working fine but I need to show the SELECT-field with the value that is filtering the list. And here is the problem, I don't know how to do it.
A bit of code of the field from the index.html.twig:
{{ form_widget(personalFilterForm.personaFiltrarMail,
{ 'empty_value': 'Todos',
'attr': {'selected': personaFiltrarMail,
'onChange': 'javascript:document.filtrado.submit()' }
}
)
}}
That code is generating this html code:
<select name="test_onebundle_type[personaFiltrarMail]" id="test_onebundle_type_personaFiltrarMail"
onchange="javascript:document.filtrado.submit()"
required="required" selected="two#mail.com">
<option value="">Todos</option>
<option value=one#mail.com">Name One</option>
<option value=two#mail.com">Name Two</option>
<option value=three#mail.com">Name three</option>
The real problem here (I think) is knowing how can I get access to the OPTIONS sub-element to set de SELECTED attribute on a concrete OPTION item.
Thanks.
=== Controller ===
Here the 'Controller'...
All four numbered 'echoes' gives me the mail: 'two#mail.com' But the SELECT html TAG is always located on the first one OPTION tag.
class HorasController extends Controller
{
/**
* Lists all Horas entities.
*
* #Route("/", name="horas")
* #Template()
*/
public function indexAction()
{
$em = $this->getDoctrine()->getEntityManager();
$personas = $em->getRepository('PtGhorgaBundle:Personal')->findAll();
$personalFilterForm = $this->createForm(new PersonalFilterType(), $personas);
$request = $this->getRequest();
$formVars = $request->request->get('pt_ghorgabundle_type');
$personaFiltrarMail = $formVars['personaFiltrarMail'];
//echo "1.- [".$personaFiltrarMail."]<br />";
if (!isset($personaFiltrarMail) || $personaFiltrarMail=="") {
$entities = $em->getRepository('PtGhorgaBundle:Horas')->findAll();
} else {
$criterio = array('persona' => $personaFiltrarMail,);
$entities = $em->getRepository('PtGhorgaBundle:Horas')->findBy($criterio);
$criterio = array('mail' => $personaFiltrarMail,);
$personaFiltrarMail = $em->getRepository('PtGhorgaBundle:Personal')->find($criterio)->getMail();
echo "2.- [".$personaFiltrarMail."]<br />";
$personalFilterForm->personaFiltrarMail = $personaFiltrarMail;
echo "3.- [".$personaFiltrarMail."]<br />";
echo "4.- [".$personalFilterForm->personaFiltrarMail."]<br />";
}
return array('entities' => $entities,
'personas' => $personas,
'personalFilterForm' => $personalFilterForm->createView(),
'personaFiltrarMail' => $personaFiltrarMail,
);
}
In your data you can set the property personaFiltrarMail to the according value.
For example in your controller :
$object = new Object();
$object->personaFiltrarMail = 'two#mail.com';
$form = $this->createFormBuilder($object);
Then render your template.
I have found it:
just belown
echo "4....." line
$data = array('personaFiltrarMail'=> $personaFiltrarMail);
$personalFilterForm->setData($data);
Regards.

Resources