How to remove 3 articles in the block in the cycle



  • Hello. I' articles on the website by the cycle:

    $last_news = mysql_query("SELECT * FROM `news` ORDER BY `id` DESC");
        while ($lnews = mysql_fetch_assoc($last_news)) {
    

    //разметка

    }

    I need to get this on the page.

    <div id="r_block">
    тут три статьи
    </div>

    <div id="r_block">
    тут три статьи
    </div>

    <div id="r_block">
    тут три статьи
    </div>

    And every time, there's three articles in the block. r_block

    How do I put this in my cycle? I'll be grateful for your help.



    1. For mysql_query, PDO should be used
    2. Instead of releasing right on the ground, a template should be used.

    So we do this:

    first obtained the necessary data in the mass

    $sql = "SELECT * FROM `news` ORDER BY `id` DESC LIMIT 15";
    $last_news = $pdo->query($sql)->fetchAll();
    

    Then we'll break it to pieces in three lines.

    $out = array_chunk($last_news, 3);
    

    and we'll end up in the box.

    <?php foreach($out as $chunk): ?>
        <div id="r_block">
            <?php foreach($chunk as $row): ?>
                <div>
                    <a href="<?=$row['url']?>"><?=$row['title']?></a>
                </div>
            <?php endforeach ?>
        </div>
    <?php endforeach ?>
    

    The approach described in another response was used in the past century when no one was able to write on the RR. And now that you're laughing at work with the OBD, HTML and your fingers, counting on how many lines to remove each time, it's another way to confirm the reputation of the RBR as a scumbag despised by all the languages. Don't do that.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2