Formularios Php+Mysql
#1
Posted 02/08/2011, 20:01
comecei a mexer com php e mysql agora e por isso tenho essa duvida
Criei um livro de visitas funciona perfeitamente.. porem na hora do script ler o banco de dados
ele le de cima para baixo, isso significa que ele le os comentarios mais velhos primeiro, queria que fosse alcontrario, logo que a pessoa postasse o comentario esse comentario ficaria em cima da tela não em baixo..
Obrigado pessoal!
#2
Posted 03/08/2011, 08:49
SELECT * FROM guestbook ORDER BY id DESCO QUESTBOOK é o nome da sua tabela, o ID eo campo primario da tabela aquele automatico
ORDER BY = ordernar por
DESC = do mais novo para o mais velho
ASC = do mais velho para o mais novo
ajudei colabora com um pontinho
- gabriel6 likes this
#3
Posted 03/08/2011, 22:55
ISSÓ é muitooo simples na consulta do sql vc usa
SELECT * FROM guestbook ORDER BY id DESCO QUESTBOOK é o nome da sua tabela, o ID eo campo primario da tabela aquele automatico
ORDER BY = ordernar por
DESC = do mais novo para o mais velho
ASC = do mais velho para o mais novo
ajudei colabora com um pontinho
ajudo eu agradeço e te dou o pontinho
vlw mano !
#4
Posted 04/08/2011, 09:02
#5
Posted 05/08/2011, 05:04
como eu fasso para depois de 10 comentarios na hora que a pessoa criasse o 11 o 1 ia para uma segunda pagina entende? igual forum mesmo!
<HTML>
<HEAD>
<TITLE>New Document</TITLE>
</HEAD>
<BODY>
<body bgcolor="black">
<?php
$con = mysql_connect('localhost', 'root', '') or
die('Não foi possível conectar');
mysql_select_db("administracao_fc", $con);
$result = mysql_query('SELECT * FROM mural ORDER BY id DESC');
mysql_close($con);
while($row = mysql_fetch_array($result)){
echo"<table
style='text-align: left; width: 200px;' align='center'border='1'
cellpadding='2' cellspacing='2'>
";
echo"<tr>";
echo"<th><font color='white'>Nome: <br></font></th>";
echo"</tr>";
echo "<tr><th><font color='white'>" . $row['nome'] . "</font></th>";
echo"<br>";
echo"</tr>";
echo "<tr>";
echo "<th><font color='white'>Mensagem</font></th>";
echo "</tr>";
echo "<td> <font color='white'>" . $row['mensagem'] . "</font></td>";
echo "</tr>";
}
echo "</table>";
?>
<br>
<form action="teste.php" method="post">
<tr>
<table border='1' align='center' width='10' cellspacing='0' cellpadding='0'>
<tr>
<th> <font color="white">Nome:<input type="text" size="25" name="nome" /><br /> </th>
</tr>
<tr>
<th><font color="white">E-mail:<input type="text" name="senha" size="25" /><br> </th>
</tr>
<tr>
<th><font color="white">Mensagem:
</br><textarea name="mensagem" cols="25" rows="6"></textarea><br /><br /><br /><br />
</th>
</tr>
<tr>
<th><input type="submit" /> </font>
<input type="reset" name="Limpar" value="Limpar" /> </th>
</table>
</BODY>
</HTML>
#6
Posted 05/08/2011, 09:41
<?php
class PS_Pagination {
var $php_self;
var $rows_per_pagina = 10; //Number of records to display per pagina
var $total_rows = 0; //Total number of rows returned by the query
var $links_per_pagina = 5; //Number of links to display per pagina
var $append = ""; //Paremeters to append to pagination links
var $sql = "";
var $debug = false;
var $conn = false;
var $pagina = 1;
var $max_paginas = 0;
var $offset = 0;
function PS_Pagination($connection, $sql, $rows_per_pagina = 10, $links_per_pagina = 5, $append = "") {
$this->conn = $connection;
$this->sql = $sql;
$this->rows_per_pagina = (int)$rows_per_pagina;
if (intval($links_per_pagina ) > 0) {
$this->links_per_pagina = (int)$links_per_pagina;
} else {
$this->links_per_pagina = 5;
}
$this->append = $append;
$this->php_self = htmlspecialchars($_SERVER['PHP_SELF'] );
if (isset($_GET['pagina'] )) {
$this->pagina = intval($_GET['pagina'] );
}
}
function paginate() {
//Check for valid mysql connection
if (! $this->conn || ! is_resource($this->conn )) {
if ($this->debug)
echo "MySQL connection missing<br />";
return false;
}
//Find total number of rows
$all_rs = @mysql_query($this->sql );
if (! $all_rs) {
if ($this->debug)
echo "SQL query failed. Check your query.<br /><br />Error Returned: " . mysql_error();
return false;
}
$this->total_rows = mysql_num_rows($all_rs );
@mysql_close($all_rs );
//Return FALSE if no rows found
if ($this->total_rows == 0) {
if ($this->debug)
echo "A Consulta Não Retornou Nada.";
return FALSE;
}
//Max number of paginas
$this->max_paginas = ceil($this->total_rows / $this->rows_per_pagina );
if ($this->links_per_pagina > $this->max_paginas) {
$this->links_per_pagina = $this->max_paginas;
}
//Check the pagina value just in case someone is trying to input an aribitrary value
if ($this->pagina > $this->max_paginas || $this->pagina <= 0) {
$this->pagina = 1;
}
//Calculate Offset
$this->offset = $this->rows_per_pagina * ($this->pagina - 1);
//Fetch the required result set
$rs = @mysql_query($this->sql . " LIMIT {$this->offset}, {$this->rows_per_pagina}" );
if (! $rs) {
if ($this->debug)
echo "A paginação falhou. Verifique sua consulta.<br /><br />Erro Retornado: " . mysql_error();
return false;
}
return $rs;
}
function renderFirst($tag = 'Página anterior') {
if ($this->total_rows == 0)
return FALSE;
if ($this->pagina == 1) {
return "$tag ";
} else {
return '<a href="'. $_SERVER['PHP_SELF']. '?pagina=1' . $this->append . '">' . $tag . '</a> ';
}
}
/**
* Display the link to the last pagina
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to 'Last'
* @return string
*/
function renderLast($tag = 'Próxima página') {
if ($this->total_rows == 0)
return FALSE;
if ($this->pagina == $this->max_paginas) {
return $tag;
} else {
return ' <a href="'. $_SERVER['PHP_SELF'].'?pagina=' . $this->max_paginas . $this->append . '">' . $tag . '</a>';
}
}
/**
* Display the next link
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to '>>'
* @return string
*/
function renderNext($tag = '>>') {
if ($this->total_rows == 0)
return FALSE;
if ($this->pagina < $this->max_paginas) {
return '<a href="'. $_SERVER['PHP_SELF'].'?pagina=' . ($this->pagina + 1) . $this->append . '">' . $tag . '</a>';
} else {
return $tag;
}
}
/**
* Display the previous link
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to '<<'
* @return string
*/
function renderPrev($tag = '<<') {
if ($this->total_rows == 0)
return FALSE;
if ($this->pagina > 1) {
return ' <a href="' . $_SERVER['PHP_SELF']. '?pagina=' . ($this->pagina - 1) . '&' . $this->append . '">' . $tag . '</a>';
} else {
return " $tag";
}
}
/**
* Display the pagina links
*
* @access public
* @return string
*/
function renderNav($prefix = '<span class="pagina_link">', $suffix = '</span>') {
if ($this->total_rows == 0)
return FALSE;
$batch = ceil($this->pagina / $this->links_per_pagina );
$end = $batch * $this->links_per_pagina;
if ($end == $this->pagina) {
//$end = $end + $this->links_per_pagina - 1;
//$end = $end + ceil($this->links_per_pagina/2);
}
if ($end > $this->max_paginas) {
$end = $this->max_paginas;
}
$start = $end - $this->links_per_pagina + 1;
$links = '';
for($i = $start; $i <= $end; $i ++) {
if ($i == $this->pagina) {
$links .= $prefix . " $i " . $suffix;
} else {
$links .= ' ' . $prefix . '<a href="' . $_SERVER['PHP_SELF']. '?pagina=' . $i . $this->append . '">' . $i . '</a>' . $suffix . ' ';
}
}
return $links;
}
/**
* Display full pagination navigation
*
* @access public
* @return string
*/
function renderFullNav() {
return $this->renderFirst() . ' ' . $this->renderPrev() . ' ' . $this->renderNav() . ' ' . $this->renderNext() . ' ' . $this->renderLast();
}
/**
* Set debug mode
*
* @access public
* @param bool $debug Set to TRUE to enable debug messages
* @return void
*/
function setDebug($debug) {
$this->debug = $debug;
}
}
?>
como usar
<?php
$sql = 'SELECT * FROM noticias ORDER BY idnot DESC';
//Create a PS_Pagination object
$pager = new PS_Pagination($con, $sql, 10, 3);
//The paginate() function returns a mysql
//result set for the current page
$rs = $pager->paginate();
//Loop through the result set
while($row = mysql_fetch_assoc($rs)) {
$idnot = $row['idnot'];
$titulo = $row['titulo'];
$imagem = $row['imagem'];
$status = $row['status'];
$categoria = $row['categoria'];
echo "$titulo";
}
?>
#7
Posted 05/08/2011, 14:49
#8
Posted 05/08/2011, 15:17
a Segunda parte vc executa para exibir o resultado com paginação ou seja este é o codigo de exibição que chama a outra função
#9
Posted 05/08/2011, 15:45
depois colokei
$pager = new PS_Pagination($con, $sql, 10, 3);
//The paginate() function returns a mysql
//result set for the current page
$rs = $pager->paginate();
na pagina que eu postei la emcima mas mesmo assim não deu certo...to fazendo algo de errado?
#10
Posted 05/08/2011, 15:52
<?php
//faz a consulta no mysql
$sql = 'SELECT * FROM nome_da_sua_tabela ORDER BY id_automatico DESC';
//chama a função / class o 10 e o numero de resultado por pagina
$pager = new PS_Pagination($con, $sql, 10, 3);
$rs = $pager->paginate();
//aqui vc vai fazer um loop
while($row = mysql_fetch_assoc($rs)) {
//esses aqui são as variavel da tabela
$idnot = $row['idnot'];
//aqui vai exibir
echo "$titulo";
}
?>
#11
Posted 05/08/2011, 16:00
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\wamp\www\FC\mural\mostrar.php on line 24
no Codigo:
<?php
include("paginacao.php");
$con = mysql_connect('localhost', 'root', '') or
die('Não foi possível conectar');
mysql_select_db("administracao_fc", $con);
$result = mysql_query('SELECT * FROM mural ORDER BY id DESC');
$pager = new PS_Pagination($con, $result, 10, 3);
$rs = $pager->paginate();
mysql_close($con);
while($row = mysql_fetch_assoc($rs)){
$cor_texto = $row["cor_texto"];
$mensagem = $row["mensagem"];
$idnot = $row['idnot'];
//aqui vai exibir
echo "$titulo";
echo"<table
style='text-align: left; width: 200px;' align='center'border='0'
cellpadding='2' cellspacing='2'>
";
echo"<tr>";
echo"<th><font color='$cor_texto'>Nome: </font>";
echo "<font color='$cor_texto'>" . $row['nome'] . "</font></th>";
echo"<br>";
echo"</tr>";
echo "<tr>";
echo "<th><font color='$cor_texto'>Mensagem:</font></th>";
echo "</tr>";
echo "<td> <font color='$cor_texto'>" . $row['mensagem'] . "</font></td>";
echo "</tr>";
}
echo "</table>";
?>
OBS: to olhando seu blog e ele é muito bom parabens !
vlw a ajuda
#12
Posted 05/08/2011, 16:05
<?php
include("paginacao.php");
$con = mysql_connect('localhost', 'root', '') or
die('Não foi possível conectar');
mysql_select_db("administracao_fc", $con);
$result = mysql_query('SELECT * FROM mural ORDER BY id DESC');
$pager = new PS_Pagination($con, $result, 10, 3);
$rs = $pager->paginate();
mysql_close($con);
echo"<table
style='text-align: left; width: 200px;' align='center'border='0'
cellpadding='2' cellspacing='2'>";
while($row = mysql_fetch_assoc($rs)){
$cor_texto = $row["cor_texto"];
$mensagem = $row["mensagem"];
$nome = $row['nome'];
//aqui vai exibir
echo"<tr>";
echo"<th><font color='$cor_texto'>Nome: </font>";
echo "<font color='$cor_texto'>$nome</font></th>";
echo"<br>";
echo"</tr>";
echo "<tr>";
echo "<th><font color='$cor_texto'>Mensagem:</font></th>";
echo "</tr>";
echo "<td> <font color='$cor_texto'>$mensagem</font></td>";
echo "</tr>";
}
//Display the navigation
echo $pager->renderFullNav();
echo "</table>";
?>
#13
Posted 05/08/2011, 16:07
mas deu esse erro
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\wamp\www\FC\mural\mostrar.php on line 29
#14
Posted 05/08/2011, 16:19
SELECT * FROM mural ORDER BY id DESC'
tenta fazer isto
$result = mysql_query('SELECT * FROM mural ORDER BY id DESC') or die (mysql_error());que vai mostar o detalhe do erro
#15
Posted 05/08/2011, 16:23
desculpa o incomodo ai mano...vlw a ajuda
1 user(s) are reading this topic
0 membro(s), 1 visitante(s) e 0 membros anônimo(s)










