Sort While by insertion into MYSQL Database

Asked

Viewed 248 times

1

I’m wanting my while instead of creating the block "element-item" down, create the block up, ie He orders from the youngest to the oldest and not from the oldest to the youngest

EXAMPLE: What is he doing:

BLOCO ID1 - BLOCO ID2 - BLOCO ID3

what I want:

BLOCO ID3 - BLOCO ID2 - BLOCO ID1

My code:

<?php
$servidor = 'localhost';
$banco      = 'apixel_galeria';
$usuario  = 'root';
$senha    = '';
$link     = @mysql_connect($servidor, $usuario, $senha);
$db          = mysql_select_db($banco,$link);
if(!$link)
{
    echo "erro ao conectar ao banco de dados!";exit();
}

$sql = "SELECT * FROM portfolio";
$query = mysql_query($sql);

while($sql = mysql_fetch_array($query)){
$id = $sql["id"];
$nome = $sql["nome"];
$tipo = $sql["tipo"];
$desc = $sql["desc"];
$menu = $sql["menu"];
$imageM = $sql["imageM"];
$imageF = $sql["imageF"];
    ?>
          <div class="element-item <?php echo "$menu";?>" data-category="transition">
       <a href="#portfolioModal54" class="portfolio-link" data-toggle="modal">
                                <img src="<?php echo "$imageM"?>" alt="project 2">
             <div class="mask">    <div class="conteudo_mask" style="
    transform: translateY(-50%);
    top: 50%;
    position: relative;
    /* float: left; */
    ">                   <h1><?php echo "$nome"?></h1>                   <div id="lin" style="
    width: 200px;
"></div>                   <h2><?php echo "$tipo"?></h2>                                                    </div><h3 style="
    transform: translateY(-50%);
    top: 50%;
    position: relative;
">VEJA <br><img src="images/mais.png" alt="mais" style="width: 20px;height: 19px;margin-bottom: -1px;margin-top: 3px;"></h3></div>
                                </a>
  </div>

        <?php
}
?>
  • 4

    ORDER BY id DESC tried that?

  • where do I put this friend?

  • 2

    SELECT * FROM portfolio ORDER BY id DESC

  • vlw friend worked out, had forgotten it :)

  • 1

    Good, you can mark Renato Tavares' reply as you accept :)

  • :) ready thank you.

Show 1 more comment

1 answer

1


Try something like that:

<?php
$servidor = 'localhost';
$banco      = 'apixel_galeria';
$usuario  = 'root';
$senha    = '';
$link     = @mysql_connect($servidor, $usuario, $senha);
$db          = mysql_select_db($banco,$link);
if(!$link)
{
    echo "erro ao conectar ao banco de dados!";exit();
}

$sql = "SELECT * FROM portfolio ORDER BY id DESC";
$query = mysql_query($sql);

while($sql = mysql_fetch_array($query)){
$id = $sql["id"];
$nome = $sql["nome"];
$tipo = $sql["tipo"];
$desc = $sql["desc"];
$menu = $sql["menu"];
$imageM = $sql["imageM"];
$imageF = $sql["imageF"];
    ?>
          <div class="element-item <?php echo "$menu";?>" data-category="transition">
       <a href="#portfolioModal54" class="portfolio-link" data-toggle="modal">
                                <img src="<?php echo "$imageM"?>" alt="project 2">
             <div class="mask">    <div class="conteudo_mask" style="
    transform: translateY(-50%);
    top: 50%;
    position: relative;
    /* float: left; */
    ">                   <h1><?php echo "$nome"?></h1>                   <div id="lin" style="
    width: 200px;
"></div>                   <h2><?php echo "$tipo"?></h2>                                                    </div><h3 style="
    transform: translateY(-50%);
    top: 50%;
    position: relative;
">VEJA <br><img src="images/mais.png" alt="mais" style="width: 20px;height: 19px;margin-bottom: -1px;margin-top: 3px;"></h3></div>
                                </a>
  </div>

        <?php
}
?>

Considerations:

  • Use PDO
  • Never mix HTML and PHP
  • You can echo like this <?= $somevar ?>
  • 1

    thanks for the help :)

Browser other questions tagged

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