How to generate automatic fields

0

I need to go to the database to fetch the img and go showing it in every <li> </li> of the carousel, however I had to leave it automatic, since it's a lot of photos, is there any way?

<div id="wrapper">      
    <div id="carousel" class="carousel">
      <div class="call-to-content wow fadeInUp">
        <h3>fileira 1</h3>
      </div>
        <a class="nav prev" href="#"></a>

        <div class="carousel_items_holder">
            <ul class="carousel_items">
                <li>
                 <a href="#">
                  <?php
                      $img=mysql_query("SELECT * FROM arquivo WHERE id_pessoa = 1 ORDER BY visu DESC limit 0,1 ") 
                      or die("Impossível executar a query");        

                      $nome=mysql_query("SELECT  nome, id_pessoa FROM arquivo WHERE id_pessoa = 1   
                      ORDER BY visu DESC limit 0,1") or die("Impossível executar a query");

                      while($row=mysql_fetch_object($img)) { 
                          echo "<img src='getImagem.php?PicNum=$row->codigo' \">"; 
                      } 

                      while($row=mysql_fetch_array($nome)) { 
                          $c_nome = $row["nome"];
                          $c_id = $row["id_pessoa"];
                      }         

                   ?>
                </a> 

                <div class="center">
                    <p>
                      <?php             
                        echo "<a href='perfil.php?id=$c_id'> $c_nome </a>";             
                     ?>
                    </p>
                </div>  
                </li>   
        <a class="nav next" href="#"></a>
    </div>  
</div>

Connection file

<?php
$host = "localhost";
$username = "root";
$password = "";
$db = "imagem";

$link = mysqli_connect($host,$username,$password) or die("Impossível conectar ao banco."); 

@mysqli_select_db($db) or die("Impossível conectar ao banco"); 

? >

With this code above, I manually enter, however needed automatic, do you have any way?

    
asked by anonymous 01.02.2017 / 21:07

2 answers

1

There are many problems:

  • First Phpmyadmin has nothing to do with the problem (I'm just commenting because you put the phpmyadmin tag in the question, see the review ), read:

  • As your HTML is full of problems, it seems to me just a typo

  • Third do not use plus functions that begin with mysql_ , use mysqli_ or PDO , read:
  • Two queries are not required for a single table in this specific case
  • The connection code should look like this:

    <?php
    $host = "localhost";
    $username = "root";
    $password = "";
    $db = "imagem";
    
    $link = mysqli_connect($host, $username, $password, $db);
    
    if (mysqli_connect_errno()) {
        printf('Connect failed: %s\n', mysqli_connect_error());
        exit();
    }
    

    And your HTML would look better this way:

    <div id='wrapper'>
        <div id='carousel' class='carousel'>
          <div class='call-to-content wow fadeInUp'>
            <h3>fileira 1</h3>
          </div>
            <a class='nav prev' href='#'></a>
    
            <div class='carousel_items_holder'>
            <?php
            $resultado = mysqli_query($link, 'SELECT nome, id_pessoa, codigo FROM arquivo ORDER BY visu DESC');
            ?>
    
            <?php if (!$resultado): ?>
                <p>Impossivel conectar</p>
    
                <?php else: ?>
                    <ul class='carousel_items'>
                    <?php while ($row = mysqli_fetch_array($resultado, MYSQLI_ASSOC)): ?>
    
                    <li>
                        <a href='perfil.php?id=<?php echo $row['id_pessoa']; ?>'>
                            <img src='getImagem.php?PicNum=<?php echo $row['codigo']; ?>'>
                        </a>
    
                        <div class='center'>
                            <p><?php echo $row['nome']; ?></p>
                        </div>
                    </li>
    
                    <?php endwhile; ?>
                    </ul>
                <?php endif; ?>
            </div>
            <a class='nav next' href='#'></a>
        </div>
    </div>
    
        
    01.02.2017 / 21:26
    0

    I do not want to go into details of being SELECT na view, but let's go. If you are looping (while) within <li> in this case, it should be outside of it, so:

    <ul class="carousel_items">
        <?php
            $img=mysql_query("SELECT * FROM arquivo WHERE id_pessoa = 1 ORDER BY visu DESC limit 0,1 ") 
            or die("Impossível executar a query");
        ?>
    
        <?php while($row=mysql_fetch_array($img)) { ?>
    
            <li>
              <a href="#">
                  <?php  echo "<img src='getImagem.php?PicNum=$row->codigo' \">"; ?>
              </a> 
    
          </li> 
    
        <?php } ?>
    
    </ul>
    
        
    01.02.2017 / 21:18