Log in

View Full Version : Simple CMS



Medyman
07-23-2007, 06:06 PM
Hey all...

I'm trying to build a simple CMS system to easily update a "recent updates/news" section on a website I'm building.

This is the first time I'm doing something like this, so please bear with me.

I followed a tutorial at php-mysql-tutorials.com and learned a lot.

I have it working as the tutorial says. I wanted to tweak it a little, however. So I don't know how I would do this.


Here is the PHP. This lists all of the available articles in the DB and then makes a list.


<?php
include 'config.php';
include 'opendb.php';

// if no id is specified, list the available articles
if(!isset($_GET['id']))
{
$self = $_SERVER['PHP_SELF'];

$query = "SELECT id, title FROM news ORDER BY id";
$result = mysql_query($query) or die('Error : ' . mysql_error());

// create the article list
$content = '<ol>';
while($row = mysql_fetch_array($result, MYSQL_NUM))
{
list($id, $title) = $row;
$content .= "<li><a href=\"$self?id=$id\">$title</a></li>\r\n";
}

$content .= '</ol>';

$title = 'Available Articles';
} else {
// get the article info from database
$query = "SELECT title, content FROM news WHERE id=".$_GET['id'];
$result = mysql_query($query) or die('Error : ' . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);

$title = $row['title'];
$content = $row['content'];
}

include 'closedb.php';
?>

<html>
<head>
<title>
<?php echo $title; ?>
</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">

// ... some css here to make the page look nicer

</style>
</head>
<body>
<table width="600" border="0" align="center" cellpadding="10" cellspacing="1" bgcolor="#336699">
<tr>
<td bgcolor="#FFFFFF">
<h1 align="center"><?php echo $title; ?></h1>
<?php
echo $content;

// when displaying an article show a link
// to see the article list
if(isset($_GET['id']))
{
?>
<p>&nbsp;</p>
<p align="center"><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Article List</a></p>
<?php
}
?>
</td>
</tr>
</table>
</body>
</html>




What I want to do is show ALL of the articles. So, how would i do this without the whole "id" bit.

The second part would be that I need a way to delete an entry for the mySQL table (i haven't attempted this yet, might be able to figure it out on my own)

Thanks!

djr33
07-23-2007, 06:45 PM
http://php-mysql-tutorial.com
All you need to know is there.

For showing all entries, just don't use a WHERE, and then you get all of the results returned. Loop through them to display.

To delete, use the DELETE command.

Both are covered in detail in the tutorial.

Medyman
07-23-2007, 07:58 PM
http://php-mysql-tutorial.com
All you need to know is there.

For showing all entries, just don't use a WHERE, and then you get all of the results returned. Loop through them to display.

To delete, use the DELETE command.

Both are covered in detail in the tutorial.

I've been to that tutorial and the reason for me posting here is because that didn't answer all of my questions.

I tried editing this:


$query = "SELECT title, content FROM news WHERE id=".$_GET['id'];

to this:


$query = "SELECT title, content FROM news;

but that gives an error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\article1.php on line 30

Line 30:

$title = $row['title'];



Edit:
Nevermind, figured it out.


<?php
include 'config.php';
include 'opendb.php';

$query = "SELECT title, content FROM news";
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result))
{
echo "<div class=border style='text-align:left'><div style='margin:5px;background-color:f9f7f8;'> {$row['title']} <br>" .
"{$row['content']} <br><br></div>";
}
include 'closedb.php';
?>

Sliight
07-24-2007, 08:12 AM
Gotta love the old " character eh? :)