Page 1 of 1

Very Basic CMS help - displaying

Posted: Mon Jul 17, 2006 11:15 am
by blade_922
Hey there,

I have set up the page which writes to my mysql database, id, title,content

All went well there.

I also managed to display each article title fine and link it to index.php?id=x where x = the id of the article.

Now when i click one of these nothing happens, how would i make it so when i click one of the articles the content of the article comes up.

Here is my index.php page so far

Code: Select all

<?php 
$self = 'http://www.gtcave.com/test/cms/'; 

$content=mysql_query("select * from news ORDER BY `id` DESC LIMIT 5") or die(mysql_error()); 
while ($donnee = mysql_fetch_array($content)) 
{ 

$id= $donnee['id'];  
$title=$donnee['title'];

echo "<tr>"; 
 
echo '  <td><a href="' . $self . 'index.php?id=' . $id. '">' . $title .'</a></td>' . "\n" ; 

echo "</tr></br>"; 


} 


?>

K so that displays the article list. What i want to know is how to make it when you click one of the article links, how to make the actual article come up? I just want real basic way on how to do it.

please help if ya know how,
regards

Posted: Mon Jul 17, 2006 11:17 am
by Burrito
you'll need to select the article based on the ID (from your link). After you have it selected use:

Code: Select all

$row = mysql_fetch_assoc($result);
you can then use the $row array to display whatever elements of the article you want on the page.

Posted: Mon Jul 17, 2006 11:31 am
by blade_922
hey im kind of new to php n stuff, so bear with me, im not used to the php/mysql lingo yet.

K so how exactly dya select the article based on the ID from the link? i have never done that before.

and once i have that selected that you say i should use the row array to make whatever i want to display.

Code: Select all

$row = mysql_fetch_assoc($result);
k i think i get that part, jus need help with the first part.

Posted: Mon Jul 17, 2006 11:36 am
by Burrito

Code: Select all

echo '<td><a href="' . $self . 'index.php?id=' . $id. '">' . $title .'</a></td>' . "\n" ;
there you're passing the id from the row. so on the index.php page, simply do a select from that id as follows:

Code: Select all

$result = mysql_query("SELECT * FROM `news` WHERE `id` = ".$_GET['id'])
   or die(mysql_error());

Posted: Mon Jul 17, 2006 11:51 am
by blade_922
ooh, k hey thanks man, it worked,

on the index.php page it says

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

any idea whats wrong there?
Here is all my code now.

Code: Select all

<?php 
$self = 'http://www.gtcave.com/test/cms/'; 

$content=mysql_query("select * from news ORDER BY `id` DESC LIMIT 5") or die(mysql_error()); 
while ($donnee = mysql_fetch_array($content)) 
{ 

$id= $donnee['id'];  
$title=$donnee['title'];

echo "<tr>"; 
 
echo '  <td class="thisClass"><a href="' . $self . 'index.php?id=' . $id. '">' . $title ."</a></td>" . "\n" ; 

echo '</tr></br>'; 


} 


?>

<?php

$result = mysql_query("SELECT * FROM `news` WHERE `id` = ".$_GET['id']) 
   or die(mysql_error()); 

$row = mysql_fetch_assoc($result);


echo  $row['content'];  

?>

</br>
</br>
</br>

Posted: Mon Jul 17, 2006 11:54 am
by Burrito
the part that selects based on the ID shoudln't be run unless the ID is set:

ex:

Code: Select all

<?php
if(isset($_GET['id']))
{
   $result = mysql_query("SELECT * FROM `news` WHERE `id` = ".$_GET['id'])
      or die(mysql_error());

   $row = mysql_fetch_assoc($result);


   echo  $row['content']; 
}
?>

Posted: Mon Jul 17, 2006 11:58 am
by blade_922
cool, working thanks.
when i click an article it still displays the links of the articles at the top, how do i make them not display when viewing the article.

Posted: Mon Jul 17, 2006 12:07 pm
by Burrito
just do the same thing for the top block of code that you did for the new block but use NOT isset instead:

ex:

Code: Select all

if(!isset($_GET['id']))
...