H Skee
H Skee

Reputation: 49

Something with looping through mysql

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 don't know how to solve it... And I don't know either what to call it.

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.

Upvotes: 0

Views: 64

Answers (5)

Duane Lortie
Duane Lortie

Reputation: 1260

At least 2 ways to do this.. #1, build one LONG string and print outside the while loop, or print each row.

Method #1 one LOOOONG var.. using .= to append new result to prior..

$query = "SELECT * FROM `Artiekel`";
$result = mysqli_query($link, $query);
$deel =''; //create a var to append to
while ($row = mysqli_fetch_assoc($result)){
      // append to $deel var
 $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>

';

} echo $deel; unset($deel); //print it, then free up system RAM

Method 2, print each row.. My preferred method, likely yours too since it only needs one more line of 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>

';
echo $deel; //printed inside the loop, so it isn't overwritten
}

Upvotes: 0

Blueblazer172
Blueblazer172

Reputation: 600

use .= instead of only =

$deel =""; //has to be defined outside the loop
$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>

';

}
echo $deel;

for understanding: this will print $deel = $deel + [the content from the html]

Upvotes: 0

Chris Forrence
Chris Forrence

Reputation: 10094

Since you're using an equals sign in your while-loop, your $deel variable keeps getting overwritten. That's why it ends up taking the value of the last row.

To fix that, you can concatenate the strings together; in PHP, you can concatenate and assign with the .= operator:

// Instantiate $deel
$deel = '';

$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>

';

}

echo $deel;

Depending on where this code is included, you may also want to break out of PHP. Good for a quick and dirty page, not so good for mixing database and view logic together.

<?php
...
while ($row = mysqli_fetch_assoc($result)): ?>
    <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>

<?php endwhile;

Upvotes: 0

Ben
Ben

Reputation: 9001

Whenever you iterate your loop, $deel gets overwritten.

You can either add each iteration to an array, or use .= to append it to $deel:

Array Method

$deel = []; //creating an empty array

while ($row = mysqli_fetch_assoc($result)){
    $deel[] = /* pushing to the array */ '    
        <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>';
}

//echo data
foreach($deel as $d) echo $d;

Append Method

$deel = ""; //creating empty string

while ($row = mysqli_fetch_assoc($result)){    
    $deel .= /* appending to string */ '
        <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>';
}

//echo data
echo $deel;

Upvotes: 0

Jay Blanchard
Jay Blanchard

Reputation: 34416

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>

';

}

Upvotes: 1

Related Questions