How to automatically generate fields

Asked

Viewed 181 times

0

I need to go to the database to find the image and show it in each <li> </li> But I needed to make it automatic, because there are many photos, there is some 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>

Connected 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 enter manually, but needed automatic, has some way?

2 answers

1


There are many problems:

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

  2. According to your HTML is full of problems, it seems to me just a typo problem

  3. Third don’t use more functions that begin with mysql_, use mysqli_ or PDO, read:
  4. No two queries are 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>
  • this giving error in my connection file, from a pf look, I will leave in question it

  • did not work, it does not display anything, but if I change to mysql and with my connection code, it works, there is something wrong in this part of mysqli

  • was missing in the mysql_fetch_array, vc n put mysqli, it worked, thanks

  • 1

    ss, I understood is q in your code you put mysql, then I arranged for mysqli

  • @Guilhermefelipesimão our vacilei, sorry :)

  • haha alright, I’m having a problem in another my Carousel, I need to limit it to display only 4 img of the highest values. <? php $result = mysqli_query($link,'SELECT code FROM ORDER file BY visu DESC limit 0,4'); ? > I used the limit, but it worked, it does not display correctly inside the Carousel

  • @Guilhermefelipesimão is not missing 2 columns? Try so $resultado = mysqli_query($link, 'SELECT nome, id_pessoa, codigo FROM arquivo ORDER BY visu DESC LIMIT 4');

  • is another code q I’m having problems, yours worked out. I tried LIMIT 4, it displayed 4 only q one under the other and not inside the side Carousel sludge.

  • @Guilhermefelipesimão vc must have touched the HTML, or your Carousel is not working.

  • I create another question with the new code, or I can edit this question here ?

  • Do not need, open the page by firefox or Chrome, press Ctrl+U, will appear the source code generated in HTML only, copy everything and paste in http://pastebin.com and send it to me.

  • http://pastebin.com/8DYQ6SMG here

  • @Guilhermefelipesimão what I said? I said "open the page by firefox Chrome and get the generated HTML", you sent me PHP. PHP doesn’t exactly help the result after you access it through your browser. Another thing, you put link inside link and already changed all the HTML, no longer has anything to do with the answer here, so there is no way to solve, do the following just send me the generated source code.

  • @Guilhermefelipesimão looking here, you again spoiled (with the pardon of the word) the HTML, has tag without close and link inside link, and what should be inside the WHILE vc put out. You’re in trouble again.

  • And I’m still learning =/. Sorry, Agra is the generated html link. http://pastebin.com/fTtsbtn2

Show 10 more comments

0

I don’t even want to go into the details of doing SELECT on the view, but let’s go. Is making the loop (while) inside the <li> in case, it should be out of it right, like this:

<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>
  • Pq cannot do inside the <li>?

  • if you do inside the LI you are only generating loop of the images and not of a list that is what it seems you need by what I understood of your question

  • This code that I showed, it looks for a img and puts in the LI, then in the other LI would have to copy dnv code and put in the LI and so on, I wonder if there is a way to do a search only in the bank and go automatically creating the LI with the data.

  • So, the way I put it, it picks it up at the bank and automatically creates the LI

Browser other questions tagged

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