Ir para conteúdo
Fórum Script Brasil
  • 0

Iniciando no PHP - Ajuda


CaioKroh

Pergunta

Bom, com ajuda de alguns tutos, eu criei um sistema de web news, com PHP + MySql.

Tem duas paginas nesse sistema: index.php (mostra a ultima noticia inserida na tabela)

- teste.php (mostra os ultimos 5 titulos inseridos na tabela)

Como que eu faço para colocar o link nos titulos da pagina teste.php, para que eles redirecionem para a noticia que foi clicada ?

Desde já agradeço. :rolleyes:

Editado por CaioKroh
Link para o comentário
Compartilhar em outros sites

9 respostass a esta questão

Posts Recomendados

  • 0

você vai ter que criar uma pagina para exibir as noticias, por exemplo exbNoticia.php

Essa exbNoticia.php terá de ter um select no banco filtrando os registro pela PK (primary key) da tabela com as noticias no seu banco de dados

$id=$_GET['ID']; // TODO: tratar prevenção de sql inject
mysql_query("SELECT * FROM noticias WHERE ID = $id");
Faz um laço e dá um echo Na pagina com o titulo a ser linkado você vai simplesmente colocar o link
<a href="exbNoticia.php?ID=<?php echo $row['ID'] ?>"><?php echo $row['titulo'] ?></a>

Onde $row é a array que trás os dados do seu select

Link para o comentário
Compartilhar em outros sites

  • 0

Bom, eu coloquei o link no titulo certinho na pagina, ai eu clico e ele redireciona para a pagina que vai mostrar a noticia pelo id selecionado.

O problema chega aki: Eu coloquei a tabela pra puxar pelo $id, mas onde que eu coloco $id=$_GET['ID']; na pagina ?

-------------EDIT------------

Na parte do browse não aparece o numero do id, aparece apenas: linknoticia.php?id=$id

Editado por CaioKroh
Link para o comentário
Compartilhar em outros sites

  • 0

facil, vou dar um exemplo simples:

Digamos que seu banco se chame "noticias" e que tenha 3 colunas "id" e "titulo" e "noticia";

pagina todas_noticias.php

//selecionando id do banco
$seleciona = mysql_query("SELECT * FROM noticias");
while( $linhas = mysql_fetch_assoc($seleciona)){
$id = $linhas['id'];
$titulo = $linha['titulo'];

print '<a href="pagina_da_noticia.php?id='.$id.'">'.$titulo.'</a>';
}
pagina exibir_noticia.php
$id = $_GET['id'];


$exibir = mysql_query("SELECT * FROM noticias WHERE ID ='$id'");
$linhas = mysql_fetch_assoc($exibir);

print $linhas['noticia'];

Nem testei mas deve funcionar certinho.

Qlqer dúvida avisa aew.

Link para o comentário
Compartilhar em outros sites

  • 0

Ainda tah danod pau...

vo passa os codigos inteiros :

Index2.php

<?php require_once('Connections/conexao.php'); ?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {

case "text":

$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

break;

case "long":

case "int":

$theValue = ($theValue != "") ? intval($theValue) : "NULL";

break;

case "double":

$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";

break;

case "date":

$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

break;

case "defined":

$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

break;

}

return $theValue;

}

}

$maxRows_inicio = 5;

$pageNum_inicio = 0;

if (isset($_GET['pageNum_inicio'])) {

$pageNum_inicio = $_GET['pageNum_inicio'];

}

$startRow_inicio = $pageNum_inicio * $maxRows_inicio;

mysql_select_db($database_conexao, $conexao);

$query_inicio = "SELECT * FROM tbnoticias";

$query_limit_inicio = sprintf("%s LIMIT %d, %d", $query_inicio, $startRow_inicio, $maxRows_inicio);

$inicio = mysql_query($query_limit_inicio, $conexao) or die(mysql_error());

$row_inicio = mysql_fetch_assoc($inicio);

if (isset($_GET['totalRows_inicio'])) {

$totalRows_inicio = $_GET['totalRows_inicio'];

} else {

$all_inicio = mysql_query($query_inicio);

$totalRows_inicio = mysql_num_rows($all_inicio);

}

$totalPages_inicio = ceil($totalRows_inicio/$maxRows_inicio)-1;

?>

<?php

$seleciona = mysql_query("SELECT * FROM tbnoticias");

while($linhas = mysql_fetch_assoc($seleciona)){

$id = $linhas['id'];

$titulo = $linha['titulo'];

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns=http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>R&aacute;dio Explosion</title>

<style type="text/css">

<!--

.style2 {font-size: 14px}

-->

</style>

</head>

<body>

<div align="center">

<table width="500" border="0" cellspacing="0" cellpadding="0">

<tr>

<td><form id="forminicio" name="forminicio" method="get" action="">

<?php do { ?>

<table width="500" border="0" cellspacing="0" cellpadding="0">

<tr>

<td width="138"><span class="style2"><?php $tdatetime = explode(' ',$row_inicio['data]); $tdata = explode('-',$tdatetime[0]); echo $tdata[2].'/'.$tdata[1].'/'.$tdata[0].' '.$tdatetime[1]; ?></span></td>

<td width=362"><span class="style2">- <a href = "<?php echo "noticia2.php?id='$id'"; ?>"><?php echo $row_inicio['titulo]; ?></a></span></td>

</tr>

</table>

<?php } while ($row_inicio = mysql_fetch_assoc($inicio)); ?></form>

</td>

</tr>

</table>

</div>

</body>

</html>

<?php

mysql_free_result($inicio);

?>

noticia2.php

<?php require_once('Connections/conexao.php'); ?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {

case "text":

$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

break;

case "long":

case "int":

$theValue = ($theValue != "") ? intval($theValue) : "NULL";

break;

case "double":

$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";

break;

case "date":

$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

break;

case "defined":

$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

break;

}

return $theValue;

}

}

mysql_select_db($database_conexao, $conexao);

$id = $_GET['id'];

$query_data = "SELECT * FROM tbnoticias WHERE ID =$id'";

$data = mysql_query($query_data, $conexao) or die(mysql_error());

$row_data = mysql_fetch_assoc($data);

$totalRows_data = mysql_num_rows($data);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>WEB NEWS - R&aacute;dio Explsoion</title>

<style type="text/css">

<!--

body {

background-color: #333333;

}

-->

</style>

</head>

<body>

<div align="center">

<table width="600" border="0" cellspacing="0" cellpadding="0">

<tr>

<td><form id="formnoticia" name="formnoticia" method="get" action="">

<table width="100%" border="1" cellpadding="2" cellspacing="2" bgcolor="#F0F0F0">

<tr>

<td height="28" colspan="2" bgcolor="#999999"><div align="center"><strong>NEWS - R&aacute;dio Explosion</strong></div></td>

</tr>

<tr>

<td width="28%" height="28"><?php $tdatetime = explode(' ',$row_data['data]); $tdata = explode('-',$tdatetime[0]); echo $tdata[2].'/'.$tdata[1].'/'.$tdata[0].' '.$tdatetime[1]; ?></td>

<td width=72%"><?php print $row_data['titulo]; ?></td>

</tr>

<tr>

<td height="28" colspan="2"><?php print $row_data['conteudo']; ?></td>

</tr>

</table>

</form>

</td>

</tr>

</table>

</div>

</body>

</html>

<?php

mysql_free_result($data);

?>

Editado por CaioKroh
Link para o comentário
Compartilhar em outros sites

  • 0

Você está fazendo usando DW, se tivesse postado na sala DW eu teria te explicado como fazer sto que você quer sem nem tocar no código.

se o dw faz isso

$row_inicio = mysql_fetch_assoc($inicio);
pra você, não é necessário fazer isso
<?php 

$seleciona = mysql_query("SELECT * FROM tbnoticias");
while($linhas = mysql_fetch_assoc($seleciona)){
$id = $linhas['id'];
$titulo = $linha['titulo'];
}

?>
tenta assim: index2.php
<?php require_once('Connections/conexao.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
    {
        $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

        $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

        switch ($theType) {
            case "text":
                $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
                break;
            case "long":
            case "int":
                $theValue = ($theValue != "") ? intval($theValue) : "NULL";
                break;
            case "double":
                $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
                break;
            case "date":
                $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
                break;
            case "defined":
                $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
                break;
        }
        return $theValue;
    }
}

$maxRows_inicio = 5;
$pageNum_inicio = 0;
if (isset($_GET['pageNum_inicio'])) {
    $pageNum_inicio = $_GET['pageNum_inicio'];
}
$startRow_inicio = $pageNum_inicio * $maxRows_inicio;

mysql_select_db($database_conexao, $conexao);
$query_inicio = "SELECT * FROM tbnoticias";
$query_limit_inicio = sprintf("%s LIMIT %d, %d", $query_inicio, $startRow_inicio, $maxRows_inicio);
$inicio = mysql_query($query_limit_inicio, $conexao) or die(mysql_error());
$row_inicio = mysql_fetch_assoc($inicio);

if (isset($_GET['totalRows_inicio'])) {
    $totalRows_inicio = $_GET['totalRows_inicio'];
} else {
    $all_inicio = mysql_query($query_inicio);
    $totalRows_inicio = mysql_num_rows($all_inicio);
}
$totalPages_inicio = ceil($totalRows_inicio/$maxRows_inicio)-1;
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>R&aacute;dio Explosion</title>
<style type="text/css">
<!--
.style2 {font-size: 14px}
-->
</style>
</head>

<body>
<div align="center">
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<?php do { ?>
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="138"><span class="style2"><?php $tdatetime = explode(' ',$row_inicio['data']); $tdata = explode('-',$tdatetime[0]); echo $tdata[2].'/'.$tdata[1].'/'.$tdata[0].' '.$tdatetime[1]; ?></span></td>
<td width="362"><span class="style2">- <a href = "<?php 
/* ao declarar uma varriavel você deve concatenar a mesma ao html impresso de preferencia */
echo "noticia2.php?id=".$id; ?>"><?php echo $row_inicio['titulo']; ?></a></span></td>
</tr>
</table>
<?php } while ($row_inicio = mysql_fetch_assoc($inicio)); ?>
</td>
</tr>
</table>
</div>
</body>
</html>
<?php
mysql_free_result($inicio);
?>

Link para o comentário
Compartilhar em outros sites

  • 0

Tipo, aqui não apareceu nada...

eu fiz umas mudificações, mas agora só aparece o id=1, e a pagina de noticia2.php está abrindo (só a noticia do id=1)

Os codes:

index2.php

<?php require_once('Connections/conexao.php'); ?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {

case "text":

$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

break;

case "long":

case "int":

$theValue = ($theValue != "") ? intval($theValue) : "NULL";

break;

case "double":

$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";

break;

case "date":

$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

break;

case "defined":

$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

break;

}

return $theValue;

}

}

$maxRows_inicio = 5;

$pageNum_inicio = 0;

if (isset($_GET['pageNum_inicio'])) {

$pageNum_inicio = $_GET['pageNum_inicio'];

}

$startRow_inicio = $pageNum_inicio * $maxRows_inicio;

mysql_select_db($database_conexao, $conexao);

$query_inicio = "SELECT * FROM tbnoticias";

$query_limit_inicio = sprintf("%s LIMIT %d, %d", $query_inicio, $startRow_inicio, $maxRows_inicio);

$inicio = mysql_query($query_limit_inicio, $conexao) or die(mysql_error());

$row_inicio = mysql_fetch_assoc($inicio);

$id = $row_inicio['id'];

if (isset($_GET['totalRows_inicio'])) {

$totalRows_inicio = $_GET['totalRows_inicio'];

} else {

$all_inicio = mysql_query($query_inicio);

$totalRows_inicio = mysql_num_rows($all_inicio);

}

$totalPages_inicio = ceil($totalRows_inicio/$maxRows_inicio)-1;

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns=http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>R&aacute;dio Explosion</title>

<style type="text/css">

<!--

.style2 {font-size: 14px}

-->

</style>

</head>

<body>

<div align="center">

<table width="500" border="0" cellspacing="0" cellpadding="0">

<tr>

<td>

<?php do { ?>

<table width="500" border="0" cellspacing="0" cellpadding="0">

<tr>

<td width="138"><span class="style2"><?php $tdatetime = explode(' ',$row_inicio['data]); $tdata = explode('-',$tdatetime[0]); echo $tdata[2].'/'.$tdata[1].'/'.$tdata[0].' '.$tdatetime[1]; ?></span></td>

<td width=362"><span class="style2">- <a href = "<?php

/* ao declarar uma varriavel você deve concatenar a mesma ao html impresso de preferencia */

echo "noticia2.php?id=".$id; ?>"><?php echo $row_inicio['titulo]; ?></a></span></td>

</tr>

</table>

<?php } while ($row_inicio = mysql_fetch_assoc($inicio)); ?>

</td>

</tr>

</table>

</div>

</body>

</html>

<?php

mysql_free_result($inicio);

?>

noticia2.php

<?php require_once('Connections/conexao.php'); ?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {

case "text":

$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

break;

case "long":

case "int":

$theValue = ($theValue != "") ? intval($theValue) : "NULL";

break;

case "double":

$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";

break;

case "date":

$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

break;

case "defined":

$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

break;

}

return $theValue;

}

}

$id = $_GET['id'];

mysql_select_db($database_conexao, $conexao);

$query_data = "SELECT * FROM tbnoticias WHERE ID = '$id'";

$data = mysql_query($query_data, $conexao) or die(mysql_error());

$row_data = mysql_fetch_assoc($data);

$totalRows_data = mysql_num_rows($data);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns=http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>WEB NEWS - R&aacute;dio Explsoion</title>

<style type="text/css">

<!--

body {

background-color: #333333;

}

-->

</style>

</head>

<body>

<div align="center">

<table width="600" border="0" cellspacing="0" cellpadding="0">

<tr>

<td><form id="formnoticia" name="formnoticia" method="get" action="">

<table width="100%" border="1" cellpadding="2" cellspacing="2" bgcolor="#F0F0F0">

<tr>

<td height="28" colspan="2" bgcolor="#999999"><div align="center"><strong>NEWS - R&aacute;dio Explosion</strong></div></td>

</tr>

<tr>

<td width="28%" height="28"><?php $tdatetime = explode(' ',$row_data['data]); $tdata = explode('-',$tdatetime[0]); echo $tdata[2].'/'.$tdata[1].'/'.$tdata[0].' '.$tdatetime[1]; ?></td>

<td width=72%"><?php print $row_data['titulo]; ?></td>

</tr>

<tr>

<td height="28" colspan="2"><?php print $row_data['conteudo']; ?></td>

</tr>

</table>

</form>

</td>

</tr>

</table>

</div>

</body>

</html>

<?php

mysql_free_result($data);

?>

O problema mesmo agora é so o ID mesmo

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,1k
    • Posts
      651,8k
×
×
  • Criar Novo...