The less you consult the better, right?

4

It is possible to vary the writing of the results, as an example below where I want to use.

<table width="1000" align="center">
    <tr> 
      <td>&nbsp;NOTICIAS</td>
      <td>&nbsp;</td>
    <tr> 
      <td width="350"> <?php
                                $sql = "SELECT * FROM noticias WHERE categoria='noticias' ORDER BY idnoticia DESC LIMIT 1";
                                $stmt = DB::prepare($sql);
                                $stmt->execute();
                                $exibe = $stmt->fetchAll();
                                foreach ($exibe as $u) { 
                                echo "<div style='float:left;width:99%;margin-right:10px;'><a  style='color:#000;text-decoration:none;' href='{$u->categoria}.php?idnoticia={$u->idnoticia}'>";
                                echo "<div class='thumbnail'> <img src='img/{$u->idnoticia}/{$u->imagem}' class='img-responsive'>";
                                echo "<div class='limit'>{$u->titulo}";
                                echo "</div></div></a></div>";

                                ?></td>
      <td width="650"><?php
                                $sql = "SELECT * FROM noticias WHERE categoria='noticias' ORDER BY idnoticia DESC LIMIT 6 OFFSET 1";
                                $stmt = DB::prepare($sql);
                                $stmt->execute();
                                $exibe = $stmt->fetchAll();
                                foreach ($exibe as $u) { 
                                echo "<div style='float:left;width:99%;margin-right:10px;'><a  style='color:#000;text-decoration:none;' href='{$u->categoria}.php?idnoticia={$u->idnoticia}'>";
                                echo "<div class='thumbnail'> <img src='img/{$u->idnoticia}/{$u->imagem}' class='img-responsive'>";
                                echo "<div class='limit'>{$u->titulo}";
                                echo "</div></div></a></div>";

                                ?></td>
  </table>

Can I take advantage of the same select used in the first <td> to continue the result after the 2nd <td> ? How to write the 1st result, "pause" and rewrite the remainder in another way?

    
asked by anonymous 04.04.2015 / 15:02

2 answers

7

Yes, it is possible. I'm considering that the code is correct (but I think it has errors):

<?php
$sql = "SELECT * FROM noticias WHERE categoria='noticias' ORDER BY idnoticia DESC LIMIT 6";
$stmt = DB::prepare($sql);
$stmt->execute();
$exibe = $stmt->fetchAll();
?>
<table width="1000" align="center">
    <tr> 
      <td>&nbsp;NOTICIAS</td>
      <td>&nbsp;</td>
    </tr>
    <tr> 
      <td width="350">
          <?php
          echo "<div style='float:left;width:99%;margin-right:10px;'><a  style='color:#000;text-decoration:none;' href='{$exibe[0]->categoria}.php?idnoticia={$exibe[0]->idnoticia}'>";
          echo "<div class='thumbnail'> <img src='img/{$exibe[0]->idnoticia}/{$exibe[0]->imagem}' class='img-responsive'>";
          echo "<div class='limit'>{$exibe[0]->titulo}";
          echo "</div></div></a></div>";
          ?>
      </td>
      <td width="650">
          <?php
          $tamanho = count($exibe);
          for ($i = 1; $i < $tamanho; $i++) { 
              echo "<div style='float:left;width:99%;margin-right:10px;'><a  style='color:#000;text-decoration:none;' href='{$exibe[$i]->categoria}.php?idnoticia={$exibe[$i]->idnoticia}'>";
              echo "<div class='thumbnail'> <img src='img/{$exibe[$i]->idnoticia}/{$exibe[$i]->imagem}' class='img-responsive'>";
              echo "<div class='limit'>{$exibe[$i]->titulo}";
              echo "</div></div></a></div>";
          }
          ?>
        </td>
    </tr>
</table>

You read all 6 items you want all at once, then use the first one separated from the next 5. Instead of two trips to fetch the information in the database, you only do one and resolve the separation within PHP.

    
04.04.2015 / 15:17
5

Each case is a case, but in your case, yes, the fewer queries the better! Even because essentially you are repeating the same query.

Generally in PHP, the tendency is to divide the information processing into 3 distinct layers, in order to facilitate reading and future maintenance. For your case, we essentially talk about:

  • Data layer - Database Interaction
  • Business layer - Information processing
  • Interface layer - HTML
  • Applying this concept, we are already answering your question because if we are going to treat the information before using it, a query serves to bring all the "news" and in the treatment phase we separate the first of the remaining:

  • Interact with database

    /* Data layer
     * Consultar base de dados para recolher informação
     */
    $sql = "SELECT * FROM noticias WHERE categoria='noticias' ORDER BY idnoticia DESC LIMIT 7";
    $stmt = DB::prepare($sql);
    $stmt->execute();
    $exibe = $stmt->fetchAll();    
    
  • Working and preparing information

    /* Business layer
     * Trabalhar os dados preparando o HTML a inserir na interface
     */
    $primeiraNoticia = $outrasNoticias = '';
    $contador = 0;
    
    foreach ($exibe as $u) {
    
        $noticia = '
        <div class="noticiasWrapper">
          <a href="'.$u->categoria.'.php?idnoticia='.$u->idnoticia.'">
            <div class="thumbnail">
              <img src="img/'.$u->idnoticia.'/'.$u->imagem.'" class="img-responsive">
              <div class="limit">'.$u->titulo.'</div>
            </div>
          </a>
        </div>';
    
        if ($contador>0) {
          $outrasNoticias .= $noticia;
        } else {
          $primeiraNoticia .= $noticia;
        }
    
        $contador++;
    }
    
  • Present information

    <!-- Interface layer
         Aplicar a informação no nosso layout e enviar a mesma para o navegador
     -->
    <style type="text/css">
        /* CSS deve ficar em ficheiro .CSS mas caso no documento,
           desta forma não andamos a duplicar a informação para o navegador */
        .noticiaWrapper{
          float:left;width:99%;margin-right:10px;
        }
        .noticiaWrapper > a{
          color:#000;text-decoration:none;
        }
    </style>
    
    <table width="1000" align="center">
        <tr>
            <td>NOTICIAS</td>
            <td> </td>
        </tr>
        <tr>
            <td width="350"><?php echo $primeiraNoticia; ?></td>
            <td width="650"><?php echo $outrasNoticias; ?></td>
        </tr>
    </table>
    
  • Notes: You can have everything in the same file .PHP in the order presented above, or later split into multiple files in an agile way as you already have the different layers properly separated.

        
    04.04.2015 / 16:05