T
T
themailvnk2014-10-30 11:36:05
PHP
themailvnk, 2014-10-30 11:36:05

Why is the loop not output in twig?

hello here is the php code

$posts = mysql_query("SELECT * FROM ".$db_prefix."posts");
$posts = mysql_fetch_array($posts);
foreach($posts as $key => $value){
 
$name_post = $posts["name"];
$mini_desc = $posts["mini_desc"];
$view = $posts["view"];
$date = $posts["date"];
$min_img = $posts["img"];
}

And here is what is in the twig template
{% extends "main.html" %}
 
{% block content %}
{% for posts in key %}
<div id="content">
 
<div class="post-item ">
<h2><a href="blog/">{{ name_post }}</a></h2>
<div class="img-conteiner">
<img src="/images/icons/708.jpg" />
</div>
    <p>{{ mini_desc}}</p>
    <div class="clear"></div>
    <div class="post-info">
    <span class="date_add">{{ date }}</span>
    <span style="background: #0ba0dd;" class="post-rub"><a href="blog/moi-sobyitiya.html">{{ cat }}</a></span>
                                                
    <span class="views">Просмотров: {{ view }}</span>
                                            
</div>
{% endfor %}
{% endblock %}

It is not possible to display a cycle with records.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Pavel Solovyov, 2014-10-30
@pavel_salauyou

{% for post in posts %}
{{ post.name %}
{{ post.img }}
{% endfor %}

S
Sergey, 2014-10-30
Protko @Fesor

In the name of Satan, of course!
I think you should still read the twig documentation. I have no idea why you need this foreach where you send data. I also don't understand why you have posts in key in the loop even though it should be post in posts. And I don't understand why mysql_* and not mysqli/PDO. Stop using obsolete extensions.

$posts = mysqli_fetch_all($result, MYSQLI_ASSOC);
$twig->render('template.twig', compact('posts'));

{% for post in posts %}
    {{ post.name }}
{% endfor %}

E
Eugene, 2014-10-30
@Eugeny1987

$posts = array()
$result = mysql_query("SELECT * FROM ".$db_prefix."posts");
while($post = mysql_fetch_assoc($result))
  $posts[] = $post;
}

do you add a variable to output in twig?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question