Very Basic CMS help - displaying

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Very Basic CMS help - displaying

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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());
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Post 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>
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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']; 
}
?>
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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']))
...
Post Reply