how to create top in php from database information

0

I made an administrative page where I have entered the information for books and their position (1st, 2nd place ...) in a form, but I can not retrieve the information I need from the database. My need is that, from your position, I put the data in html:

<article id="top">
      <?php       
        include "acessoResponsivo.php";
           $comando="select * from tb_top";
           $matriz=mysql_query($comando);
           $linha=mysql_fetch_array($matriz);
           $posicao=$linha["posicao"];


            if ($posicao=="1") {
        ?> 
            <h2>Top mais lidos</h2>
            <div id="topum" class="numeracao">
            <?php echo '<img src="img/' .$linha["imagemtop"]. '.jpg"> <br/> '; ?>
            <h4 class="num">1</h4> <br> <?php echo '<p class="toptitulo">'.$linha["toptitulo"].'</p> '; ?></div>    

        <?php    
            }

        //__________________________________dois_____________

            else if ($posicao=="2") {
        ?>
            <div id="topdois" class="numeracao">
            <?php echo '<img src="img/' .$linha["imagemtop"]. '.jpg"> <br/> '; ?>
            <h4 class="num">2</h4> <br> <?php echo '<p class="toptitulo">'.$linha["toptitulo"].'</p> '; ?></div>        
        <?php    
            }
        //__________________________________tres_____________

            else if ($posicao=="3") {
        ?>
            <div id="toptres" class="numeracao">
            <?php echo '<img src="img/' .$linha["imagemtop"]. '.jpg"> <br/> '; ?>
            <h4 class="num">3</h4> <br> <?php echo '<p class="toptitulo">'.$linha["toptitulo"].'</p> '; ?></div>
        <?php    
            }
        //__________________________________quatro_____________

            else if ($posicao=="4") {
        ?>              
            <div id="topquatro" class="numeracao">
            <?php echo '<img src="img/' .$linha["imagemtop"]. '.jpg"> <br/> '; ?>
            <h4 class="num">4</h4> <br> <?php echo '<p class="toptitulo">'.$linha["toptitulo"].'</p> '; ?></div>
        <?php    
            }
        //__________________________________cinco_____________

            else if ($posicao=="5") {
        ?>          
            <div id="topcinco" class="numeracao">
            <?php echo '<img src="img/' .$linha["imagemtop"]. '.jpg"> <br/> '; ?>
            <h4 class="num">5</h4> <br> <?php echo '<p class="toptitulo">'.$linha["toptitulo"].'</p> '; ?></div>
        <?php    
            }
        ?>          

    </article>
    
asked by anonymous 27.12.2017 / 22:34

1 answer

0

This will work

<article id="top">
  <?php       
    include "acessoResponsivo.php";

       $comando="select * from tb_top";
       $matriz=mysql_query($comando); 

    while($linha = mysql_fetch_array($matriz)){

        $posicao=$linha["posicao"];

        if ($posicao=="1") {
    ?> 
        <h2>Top mais lidos</h2>
        <div id="topum" class="numeracao">
        <?php echo '<img src="img/' .$linha["imagemtop"]. '.jpg"> <br/> '; ?>
        <h4 class="num">1</h4> <br> <?php echo '<p class="toptitulo">'.$linha["toptitulo"].'</p> '; ?></div>    

    <?php    
        }

    //__________________________________dois_____________

        else if ($posicao=="2") {
    ?>
        <div id="topdois" class="numeracao">
        <?php echo '<img src="img/' .$linha["imagemtop"]. '.jpg"> <br/> '; ?>
        <h4 class="num">2</h4> <br> <?php echo '<p class="toptitulo">'.$linha["toptitulo"].'</p> '; ?></div>        
    <?php    
        }
    //__________________________________tres_____________

        else if ($posicao=="3") {
    ?>
        <div id="toptres" class="numeracao">
        <?php echo '<img src="img/' .$linha["imagemtop"]. '.jpg"> <br/> '; ?>
        <h4 class="num">3</h4> <br> <?php echo '<p class="toptitulo">'.$linha["toptitulo"].'</p> '; ?></div>
    <?php    
        }
    //__________________________________quatro_____________

        else if ($posicao=="4") {
    ?>              
        <div id="topquatro" class="numeracao">
        <?php echo '<img src="img/' .$linha["imagemtop"]. '.jpg"> <br/> '; ?>
        <h4 class="num">4</h4> <br> <?php echo '<p class="toptitulo">'.$linha["toptitulo"].'</p> '; ?></div>
    <?php    
        }
    //__________________________________cinco_____________

        else if ($posicao=="5") {
    ?>          
        <div id="topcinco" class="numeracao">
        <?php echo '<img src="img/' .$linha["imagemtop"]. '.jpg"> <br/> '; ?>
        <h4 class="num">5</h4> <br> <?php echo '<p class="toptitulo">'.$linha["toptitulo"].'</p> '; ?></div>
    <?php    
        }

        } //end while
    ?>          

</article>
  

Since MySQL has been discontinued, I suggest migrating to MySQLi or PDO

If you're concerned with performance and are not interested in portability, mysqli is the best choice.

With MySQLi

<article id="top">
<?php
include "acessoResponsivo.php";

  $consulta = "SELECT * FROM tb_top";
  $result = $conn->query($consulta);

  while($linha = $result->fetch_array()){

    $posicao=$linha["posicao"];

    if ($posicao=="1") {
    ?> 
        <h2>Top mais lidos</h2>
        <div id="topum" class="numeracao">
        <?php echo '<img src="img/' .$linha["imagemtop"]. '.jpg"> <br/> '; ?>
        <h4 class="num">1</h4> <br> <?php echo '<p class="toptitulo">'.$linha["toptitulo"].'</p> '; ?></div>    

    <?php    
        }

    //__________________________________dois_____________

        else if ($posicao=="2") {
    ?>
        <div id="topdois" class="numeracao">
        <?php echo '<img src="img/' .$linha["imagemtop"]. '.jpg"> <br/> '; ?>
        <h4 class="num">2</h4> <br> <?php echo '<p class="toptitulo">'.$linha["toptitulo"].'</p> '; ?></div>        
    <?php    
        }
    //__________________________________tres_____________

        else if ($posicao=="3") {
    ?>
        <div id="toptres" class="numeracao">
        <?php echo '<img src="img/' .$linha["imagemtop"]. '.jpg"> <br/> '; ?>
        <h4 class="num">3</h4> <br> <?php echo '<p class="toptitulo">'.$linha["toptitulo"].'</p> '; ?></div>
    <?php    
        }
    //__________________________________quatro_____________

        else if ($posicao=="4") {
    ?>              
        <div id="topquatro" class="numeracao">
        <?php echo '<img src="img/' .$linha["imagemtop"]. '.jpg"> <br/> '; ?>
        <h4 class="num">4</h4> <br> <?php echo '<p class="toptitulo">'.$linha["toptitulo"].'</p> '; ?></div>
    <?php    
        }
    //__________________________________cinco_____________

        else if ($posicao=="5") {
    ?>          
        <div id="topcinco" class="numeracao">
        <?php echo '<img src="img/' .$linha["imagemtop"]. '.jpg"> <br/> '; ?>
        <h4 class="num">5</h4> <br> <?php echo '<p class="toptitulo">'.$linha["toptitulo"].'</p> '; ?></div>
    <?php    
        }

  } //end while
?>          

</article>

accessResponsivo.php

$conn = new mysqli("localhost","USUARIO","SENHA", "NOME_DB");
    
28.12.2017 / 00:54