I have a course description web page as a university project, where I'm currently working on the page where I'm displaying information regarding the approval status of all the courses. I'm retrieving all the courses stored in a database to an array in php, and then passing this array to the twig-file where I want to iterate through the course-array.
However twig doesn't seem to recognize what the 'course' variable in 'courseList' is, as no information is displayed.
Twig
<tbody>
{% for course in courseList %}
<tr>
<th scope="row">{{ loop.index }}</th>
<td>{{ course.getCourseCode }}</td>
<td>{{ course.getCourseName(0, course.getCourseId) }}</td>
<td class="font-weight-bold">{{ course.getCourseApprovalDue }}</td>
<td>
<div class="progress">
<div class="progress-bar {% if course.getStatus == 0 %} bg-warning
{% elseif coruse.getStatus == 1 %} bg-danger {% else %} bg-success {% endif %}" role="progressbar"
style="width: 100%" aria-valuenow="10" aria-valuemin="0" aria-valuemax="10">{{ course.getStatus }}
</div>
</div>
</td>
</tr>
{% endfor %}
</tbody>
PHP
if ($session->has('loggedin')) {
$courseList = $database->getAllCourses();
echo $twig->render('courseStatus.twig', array('user' => $session->get('user'), 'loggedIn' => true, 'courseList' => $courseList));
} else {
echo $twig->render('login.twig');
}
The database queries and the php functions are working properly. I can verify this by passing a hardcoded course object to the twig file. The coruseList array isn't empty either, as the twig file displays 5 rows when rendering it, which is the same amount as the courses stored in the database.
The array returned from getAllCourses() does not create a new instance of Course, it's just an array containing arrays with Course-data. The solution is to get the arrays, and create a new array with Course-objects with the information returned, and pass this array to the php-file.
Related
I would appreciate any kind of help for this problem I'm having.
I have 2 Symfony Forms :
One for the search part (Serial Number)
Another form that will be filled out if the Serial Number exists.
I need to get dynamicly the value of the ID.
Here is a snippet of my code :
{% for ClientInfo in getInstallationClientBySerialNumber %}
<tr id="ClientInfo_{{ installClientInfo.idRma }}">
<td>{{ ClientInfo.numeroSerie }}</td>
{% endfor %}
The loop works but I'm struggling with the JQuery part to get the ID. I've tried :
$("#client_sn").click(function(){
var elmId = $("#ClientInfo_id").attr("id");
alert(elmId);
});
Any help would be very kind.
Many Thanks in advance.
HTML ID's have to be unique on a page. You're looping over multiple elements, giving them all the same ID "client_sn"
What you can do with Twig in a for loop is access loop.index as a counter
https://twig.symfony.com/doc/2.x/tags/for.html#the-loop-variable
{% for ClientInfo in getInstallationClientBySerialNumber %}
<tr id="ClientInfo_{{ installClientInfo.idRma }}">
<td>{{ ClientInfo.numeroSerie }}</td>
{% endfor %}
In the jQuery you can then use a pattern to match all elements with an ID starting client_sn:
$("a[id^='client_sn']").click(function(){
var elmId = $("#ClientInfo_id").attr("id");
alert(elmId);
});
https://api.jquery.com/attribute-starts-with-selector/
Or use some other selector to identify the links.
Alternatively instead of {{loop.counter}} you could reuse the installClientInfo.idRma ID you're also using:
{% for ClientInfo in getInstallationClientBySerialNumber %}
<tr id="ClientInfo_{{ installClientInfo.idRma }}">
<td>{{ ClientInfo.numeroSerie }}</td>
{% endfor %}
I am working on a webapplication but ive ran into a hiccup, I have gathered data from my database and displayed the records into a datatable, my problem is that I can not just have one table the client wants me to divide the table into multiple based on what interger is in a column(omloopNummer).
The best ive gotten so far is with the following code it wil split the table up into lets say 4 tables if the interger in the database wil go 1,2,3,4 but it wil only show 1 record in each table while it should be more.
If there is any other simple solution to this instead of using twig I would be happy to try it.
<div id="index" class="table-responsive">
{% set i = 1 %}
{% for duel in duels %}
{% if duel.omloopNummer == i %}
{% set i = i + 1 %}
<table id="" class="table display-" style="width: 90%; margin-top: 30px">
<thead>
<tr>
<th>Omloop</th>
<th>Team 1</th>
<th>vs</th>
<th>Team 2</th>
<th>Acties</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ duel.omloopNummer }}</td>
<td>{{ duel.team1 }}</td>
<td>vs</td>
<td>{{ duel.team2 }}</td>
<td>{{ include('duel/_delete_form.html.twig') }}</td>
</tr>
</tbody>
</table>
{% endif %}
{% endfor %}
You could use PHP to group Duels by omloopNummer in the controller before passing to twig.
$duels = $duelRepository->findAll();
$sorted = [];
foreach($duels as $duel){
$sorted[$duel->getOmloopNummer()][] = $duel;
}
return $this->render('template.html.twig', [
'duel_groups' => $sorted,
]);
Then in twig, loop through the sorted array building a table for each non-empty group and inside each tbody loop through the group.
{% for group in duel_groups %}
...
{% if group is not empty %}
<table>
...
<tbody>
{% for duel in group %}
<tr>...</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
...
{% endfor %}
Whenever I check the "wetsoil" checkbox, my search results correctly filter through and display all the plant species that grow in wet soil, but whenever I don't check the checkbox, there are no search results. I want all the objects to appear if the checkbox is unchecked, not none of them. Not sure if my logic is incorrect, or maybe my syntax? In the database, "x" means yes.
My controller:
public function SearchResultsAction(Request $request)
{
$botanicalname = $request->query->get('botanicalname');
$commonname = $request->query->get('commonname');
$wetsoil = $request->query->get('wetsoil');
$repository = $this->getDoctrine()->getRepository('AppBundle:Shrubs');
$query = $repository->createQueryBuilder('p');
$shrubs = $query
->where($query->expr()->like('p.botanicalname',':botanicalname'))
->setParameter('botanicalname', '%'.$botanicalname.'%')
->andwhere($query->expr()->like('p.commonname',':commonname'))
->setParameter('commonname', '%'.$commonname.'%')
->andWhere($query->expr()->like('p.wetsoil',':wetsoil'))
->setParameter('wetsoil', $wetsoil)
->orderBy('p.commonname', 'ASC')
->getQuery()
->getResult();
return $this->render('shrubs/searchresults.html.twig', array(
'shrubs' => $shrubs,
));
}
And my input on the HTML page:
<label for="wetsoil">Tolerates Wet Soil: </label>
<input type="checkbox" name="wetsoil" value="x">
Note: I chose not to use the standard Twig syntax ({{ form_widget(form.wetsoil) }}) due to other technical reasons.
Front end:
<table class="table table-striped">
<thead>
{% if shrubs | length == 0 %}
<h3>No species match your search criteria. Try again.</h3>
{% else %}
<tr>
<th>Common Name</th>
<th>Botanical Name</th>
</tr>
</thead>
<tbody>
<tr>
{% endif %}
{% for shrub in shrubs %}
<td>{{ shrub.commonname }}</td>
<td>{{ shrub.botanicalname }}</td>
{#<td>#}
{#<ul>#}
{#<li>#}
{#show#}
{#</li>#}
{#<li>#}
{#edit#}
{#</li>#}
{#</ul>#}
{#</td>#}
</tr>
{% endfor %}
</tbody>
</table>
In Symfony I am using a query to retrieve all questions and their relative answers from a database. Using a for in twig I am trying to display in a table all questions with the relative answers row by row. At the moment I am able to access and display the questions only but not the answers.
The relation between my entities Questions and Answers is (one to many) thus when retrieving the question, also the answers are retrieved. Using dump, I can see the the answers are put in an Array Collection.
Trying to get only a single question from the database with another query, using the getAnswers() function from my Question entity I am able to get the answers from the array collection using this statement:
$answer = $question->getAnswers()->getValues();
Using this statement I tried to get the answers for all the questions using the bellow code in the controller, but I get this error:
Error: Call to a member function getAnswers() on a non-object
I believe this is because the query returns multiple questions and not just one.
How can I get the answers for each individual question from the Array Collection? Thanks in advance for the help.
Controller Code:
$question = $this->getDoctrine()
->getRepository('QuizBundle:Question')
->findByIdJoinedToCategory();
return $this->render('QuizBundle:Default:admin.html.twig', array('data' => $question));
Question Repository:
public function findByIdJoinedToCategory()
{
$query = $this->getEntityManager()
->createQuery(
'SELECT a, q FROM QuizBundle:Question a
JOIN a.answers q');
try {
return $query->getResult();
} catch (\Doctrine\ORM\NoResultException $e) {
return null;
}
}
Twig Code:
{% for item in data %}
<tbody>
<tr>
<th scope="row">{{ item.id }}</th>
<td>{{ item.image }}</td>
<td>{{ item.question }}</td>
<td></td>
<tr>
<tbody>
{% endfor %}
Dump screenshot of the data from the query:
With Twig and according to your doctrine query, you can display your questions/answers like this :
{% for item in data %}
<tbody>
<tr>
<th scope="row">{{ item.id }}</th>
<td>{{ item.image }}</td>
<td>{{ item.question }}</td>
{% for answer in item.answers %}
{{ answer.id }}
{% endfor %}
<tr>
<tbody>
{% endfor %}
Btw, if it exists a relation between the two entities Question and Answer and you want to display all the questions and their associative answers, you can directly doing this :
In your controller:
$questions = $this->getDoctrine()
->getRepository('QuizBundle:Question')->findAll();
return $this->render('QuizBundle:Default:admin.html.twig', array('data' => $questions));
In your Twig :
{% for question in data %}
<tbody>
<tr>
<th scope="row">{{ question.id }}</th>
<td>{{ question.image }}</td>
{% for answer in question.answers %}
{{ answer.id }}
{% endfor %}
<tr>
<tbody>
{% endfor %}
I Have a table with this columns:
login ,couleur1,parties,gagnees
I would like to print an array exactly like this one:
In Silex I get all data in an array
$app->get('/userlist', function(Application $app) {
$recup= $app['db']->executeQuery('SELECT * FROM users');
$results = $recup->fetchAll();
return $app['twig']->render('example.twig', array('users' => $results));
});
$app->run();
[enter image description here][2]?>
In Twig I tried to align them, but I can't get it like in the photo.
{% for row in users %}
<ul style="list-style: none;">
<li style="float:left; margin-right:30px" >{{ row.login }}</li>
<li style="float:left; margin-right:30px">{{row.parties}}</li>
<li style="float:left; margin-right:30px">{{row.couleur1}}</li>
<li style="float:both">{{row.couleur2}}</li>
</ul>
{% endfor %}
I would make that without separating CSS and Twig.
It will be something like this:
<table>
<tr>
<td>Joueur</td>
<td>Parties</td>
<td>Gagness</td>
<td>Colueur Preferee</td>
</tr>
{% for row in users %}
<tr>
<td>{{ row.login }}</td>
<td>{{row.parties}}</td>
<td>{{row.couleur1}}</td>
<td>{{row.couleur2}}</td>
</tr>
{% endfor %}
</table>
and apply some CSS for colors and looks.