how to create top php from database information

Asked

Viewed 35 times

0

I made an administrative page where I register in a form the information of books and their position (1st, 2nd place...), but I can not rescue from the database the information I need. 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>
  • What is the structure of your table tb_top? Edit your question with the structure.

  • Welcome Caroline, be sure to read this post https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

1 answer

0

That’s how it’s gonna 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 are concerned about performance and have no interest in portability, the best option is always mysqli.

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");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.