include "vars.php"; include "connect.php"; $query = mysqli_select_db($link , $db_name) OR DIE ("Не моу выбрать базу, либо она отсутствует"); $result = mysqli_query($link , "SELECT `title` , `text` , `img` FROM `news` "); while($row = $result->fetch_assoc()) {?> <div class="post"> <h2><?= $row['title'];?></h2> <img src="IMG/<?= $row['img'];?>" title="" /> <p><?= $row['text'];?></p> </div> <?}?> 

I would like to remove / hide or transfer to another file exactly this:

 $query = mysqli_select_db($link , $db_name) OR DIE ("Не могу выбрать базу, либо она отсутствует"); $result = mysqli_query($link , "SELECT `title`, `text`, `img` FROM `news`"); while($row = $result->fetch_assoc()) 

i.e leave only:

 include "vars.php"; include "connect.php"; {?> <div class="post"> <h2><?= $row['title'];?></h2> <img src="IMG/<?= $row['img'];?>" title="" /> <p><?= $row['text'];?></p> </div> <?}?> 

How can this be implemented, right ??

    2 answers 2

    Here it is hardly worth include , except for storing a convenient handler function that calls a callback function in a loop. For example, you can put this fragment into a function, for example, cursor() , and place it in the corresponding file cursor.php

     <?php function cursor($link, $query, $callback) { $result = mysqli_query($link, $query); while($row = $result->fetch_assoc()) { $callback($row); } } 

    The cursor() function takes as an argument a callback function into which you can place the processing of the resulting table. In your case, the target file might look like this.

     <?php include_once "vars.php"; include_once "connect.php"; include_once "cursor.php"; $query = "SELECT `title`, `text`, `img` FROM `news`"; cursor($link, $query, function($row) { ?> <div class="post"> <h2><?= $row['title'];?></h2> <img src="IMG/<?= $row['img'];?>" title="" /> <p><?= $row['text'];?></p> </div> <? }); 
    • Kilometer Error Thrown Out - user33274
    • Can you give an error message? - cheops
    • Parse error: syntax error, unexpected '<' in Z: \ home \ site.tab.ru \ www \ index.php on line 53 at 53 </ body> - user33274
    • 2
      AT ALL ANYWHERE YOU NEED to write mysqli_select_db - Ipatiev
    • 2
      @LenovoID because you have already selected a database in mysqli_connect - Ipatiev

    I think the author is asking about a simple template system.

    That is, it needs to change the file structure - it is necessary not to request data from the database, but output it in a separate file. Thus, he will get a primitive controller and a primitive view.

    Accordingly, using the primitive template system , in the “controller” we write

     <?php include "vars.php"; include "connect.php"; $data = []; $result = mysqli_query($link , "SELECT `title`, `text`, `img` FROM `news`"); while($row = $result->fetch_assoc()) { $data[] = $row; } $tpl = "tpl_images.php"; include "tpl_main.php"; 

    And in tpl_images.php we just write

     <?php foreach($data as $row): ?> <div class="post"> <h2><?= $row['title'];?></h2> <img src="IMG/<?= $row['img'];?>" title="" /> <p><?= $row['text'];?></p> </div> <? endforeach ?> 
    • and thank you so much, but the question is not how to display or something else img namely, to remove myself while, in general, I realized that together with you the owner of the best answer told me how to do it - user33274
    • one
      Well, I removed the while from the template - as required. The “best” answer actually mixes business logic and presentation, pushing the view into the controller. You would first decide what you want. - Ipatiev
    • as I am far from the theoretical part of php, and no one is learning, the php.net templates are standard there, but how are you - I can not and do not know what to do based on the situation, thank you - user33274
    • one
      Look, you have the right approach. But understanding is wrong. It is necessary not to get rid of while, but to separate SQL and HTML. You just need to follow one simple rule - there should not be a single file in which both SQL and HTML code would be encountered at the same time. Either one or the other. And everything will immediately fall into place. - Ipatiev
    • SQL is what I have in $ result = mysqli_query ($ link, "SELECT title , text , img FROM news "); ? - user33274