Jump to content
Fórum Script Brasil
  • 0

(Resolvido) Limitar resultados do Banco de Dados


kel777

Question

Bom dia galera, é o seguinte, no meu site eu tenho uma parte de blog, onde eu posto tudo pelo admin.

E na minha página inicial eu quero que mostre apenas as duas ultimas postagens.

meu código é esse:

<?php
$host="localhost"; // Host name 
$username="proativa_blog"; // Mysql username 
$password="q4w3e2r1"; // Mysql password 
$db_name="proativa_blog"; // Database name 
$tbl_name="forum_question"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending
?>
    
<table width="630px" border="0" cellspacing="1" cellpadding="0">
  <tr>
    <td colspan="2" id="blog">
                <h2 class="a_bubble">
                    <span>Em Quanto isso no Blog...</span>
                    <a href="blog.php" class="latest-articles">VER TODOS</a>
                </h2>
    </td>
  </tr>
  </table>
  <p>
    <?php
// Start looping table row
while($rows=mysql_fetch_array($result)){
?>
  </p>

<table width="630" border="0" cellspacing="1" cellpadding="0">
  <tr>
    <td>
    <div class="blog-posts">
                    <ul>
                        <!-- Blog Post -->
                        <li>
                            <h3><a href="blog_topico.php?id=<? echo $rows['id']; ?>"><? echo $rows['topic']; ?></a></h3>
                            <h4>Postado em <? echo $rows['datetime']; ?> por <? echo $rows['name']; ?></h4>
                            
                            <div class="image">
                              <a href="blog_topico.php?id=<? echo $rows['id']; ?>"><img src="admin/fotos/<? echo $rows['foto']; ?>" alt="Ver Postagem" /></a>
                            </div>
                            
                            <div class="data">
                              <p><? echo $rows['detail']; ?> <a href="blog_topico.php?id=<? echo $rows['id']; ?>"> Continuar lendo...<br /></a></p>
                            </div>
                            <div class="cl"> </div>
                            
                            <div class="buttons">
                                <a href="blog_topico.php?id=<? echo $rows['id']; ?>" class="button rarr">LER MAIS</a>
                                <a href="blog_topico.php?id=<? echo $rows['id']; ?>" class="button comments">COMENTAR</a>
                              <div class="cl"> </div>
                          </div>
                        </li>
                        <!-- /Blog Post -->
                        
                    </ul>
                </div>
    </td>
  </tr>
</table>

                

<?php
// Exit looping and close connection 
}
mysql_close();
?>

Abraços a todos!

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

Ae pessoal, consegui

Só acrescentei no inicio o código:

$numreg = 2; // Quantos registros por página vai ser mostrado
        if (!isset($pg)) {
                $pg = 0;
        }
        $inicial = $pg * $numreg;
e acrescentei no SELECT:
LIMIT $inicial, $numreg"
Abraço a todos!
Bom dia galera, é o seguinte, no meu site eu tenho uma parte de blog, onde eu posto tudo pelo admin. E na minha página inicial eu quero que mostre apenas as duas ultimas postagens. meu código é esse:
<?php
$host="localhost"; // Host name 
$username="proativa_blog"; // Mysql username 
$password="q4w3e2r1"; // Mysql password 
$db_name="proativa_blog"; // Database name 
$tbl_name="forum_question"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending
?>
    
<table width="630px" border="0" cellspacing="1" cellpadding="0">
  <tr>
    <td colspan="2" id="blog">
                <h2 class="a_bubble">
                    <span>Em Quanto isso no Blog...</span>
                    <a href="blog.php" class="latest-articles">VER TODOS</a>
                </h2>
    </td>
  </tr>
  </table>
  <p>
    <?php
// Start looping table row
while($rows=mysql_fetch_array($result)){
?>
  </p>

<table width="630" border="0" cellspacing="1" cellpadding="0">
  <tr>
    <td>
    <div class="blog-posts">
                    <ul>
                        <!-- Blog Post -->
                        <li>
                            <h3><a href="blog_topico.php?id=<? echo $rows['id']; ?>"><? echo $rows['topic']; ?></a></h3>
                            <h4>Postado em <? echo $rows['datetime']; ?> por <? echo $rows['name']; ?></h4>
                            
                            <div class="image">
                              <a href="blog_topico.php?id=<? echo $rows['id']; ?>"><img src="admin/fotos/<? echo $rows['foto']; ?>" alt="Ver Postagem" /></a>
                            </div>
                            
                            <div class="data">
                              <p><? echo $rows['detail']; ?> <a href="blog_topico.php?id=<? echo $rows['id']; ?>"> Continuar lendo...<br /></a></p>
                            </div>
                            <div class="cl"> </div>
                            
                            <div class="buttons">
                                <a href="blog_topico.php?id=<? echo $rows['id']; ?>" class="button rarr">LER MAIS</a>
                                <a href="blog_topico.php?id=<? echo $rows['id']; ?>" class="button comments">COMENTAR</a>
                              <div class="cl"> </div>
                          </div>
                        </li>
                        <!-- /Blog Post -->
                        
                    </ul>
                </div>
    </td>
  </tr>
</table>

                

<?php
// Exit looping and close connection 
}
mysql_close();
?>

Abraços a todos!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...