Luuk Wuijster Luuk Wuijster - 23 days ago 7
MySQL Question

Something with looping trough mysql, I dont really know what to call it

Okay. Here's my problem.
I'm creating a "news" site (just for practice).
I have all my articles in a mysql database and I want to print them out. But when I do that I only get the last one. I know why this is, But I dont know how to solve it... And I dont know either what to call it or google it, so I decided to ask it here.

This is my code:

$query = "SELECT * FROM `Artiekel`";
$result = mysqli_query($link, $query);

while ($row = mysqli_fetch_assoc($result)){

$deel = '

<article>
<header>
<hgroup>
<h2>'.$row['titel'].'</h2>
<h3>door: '.$row['auteur'].'</h3>
</hgroup>
</header>
<p><br>
'.$row['inhoud'].'
</p>
<footer>
<p>'.$row['datum'].'</p>
</footer>
</article>

';

}


All the variables are in dutch, but it doesn't really matter.

What you see here is code that makes one article, The last one. I know why it does it, but don't know how to solve it... Can someone help me out?

Thanks in advance,

Luuk Wuijster

Answer

Change your loop so you do not overwrite $deel. First, declare $deel outside of the loop, then concatenate to it each time you loop:

$deel = '';
while ($row = mysqli_fetch_assoc($result)){

//note the concatenation here, using .=
$deel .= ' 

        <article>
            <header>
                <hgroup>
                    <h2>'.$row['titel'].'</h2>
                    <h3>door: '.$row['auteur'].'</h3>
                </hgroup>
            </header>
            <p><br>
                '.$row['inhoud'].'
            </p>
            <footer>
                <p>'.$row['datum'].'</p>
            </footer>
        </article>

';

}
Comments