How to Add Article Categories to Your CMS

  You are currently not logged in. You can view the forums, but cannot post messages. Log In | Register

25-Jan-12 00:00
This is a forum topic for discussing the article "How to Add Article Categories to Your CMS":

http://www.elated.com/articles/add-article-categories-to-your-cms/

Learn how to modify a PHP and MySQL content management system to include support for article categories. Full CMS code download included.
25-Jan-12 19:44
thanks again, matt. you was done another great tutorial.
26-Jan-12 00:09
I already have code for an article that uses multiple categories so I wasn't expecting much but then I saw the Category and Article classes and now I'll be looking to add that type of code for mine.
26-Jan-12 17:19
@Matt you are the man, another wonderful neatly coded class. It is really nice.I hope others will love it too, cuz it is just easy and beautiful.
27-Jan-12 00:29
@chotikarn @Selfcoded @mubuuba Thanks for the kind words

Cheers,
Matt

--
Matt Doyle, Elated
28-Jan-12 14:25
Hi Matt, Hope you doing good.
I have tried to add image to the cms in the front page , meaning that a small picture could be added to the news headlines. For example,
[Here Matt's picture] Matt's Explanation of creating this CMS.
you see here picture with news on the right side, if pic is available can be added the news or interview on the front-page.

I have created this class with three properties

class Image
{
public $id = null;
public $path = null;
public $article_Id = null;

/*
* Constructor
* @params $id primary key in database to be incremented.
* @params $path path of the upload image
* @params $article_id, foriegn key to be gotten from the article
*/
public function __construct($pic = array())
{
if (isset($pic['id']))
$this->id = (int)$pic['id'];

if(isset($pic['img']))
$this->path = $pic['path'];

if(isset($pic[article_Id]))
$this->article_Id = (int)$pic['article_Id'];


}

i have created the corrosponding database to this class.
I am finding it little bit unnecessary to program too much as i have tried, you may know easier way. Please tell me what i need to do to add images to articles when they are available

Is it a good to do it like that or could there be an easier way to do it.
I just want that i could add picture to the interviews and news when theres pic for it and in viewArtical page, there can be more than one picture added to the page, but the front page only one picture.

I know you already did a good job. I would like that you explain me the easiest way or the best way to do it.

Matt thanks a lot.
Mubo
28-Jan-12 20:53
@mubuuba

i'm looking for something like that too. i'm googling for days but there is no one suit to this CMS.
so, your code is a good starting.

hope matt have idea for this image upload for each articles.
30-Jan-12 00:57
@mubuuba @chotikarn: The most straightforward way would be to create an imageFilename text field in your articles table (and Article class and article edit form). Upload the image to an uploads folder on your server (via FTP) then enter the image's file name in the article edit form. When displaying the article, generate an <img> element in the article markup. Append the value of imageFilename to the URL of the uploads folder to get the src attribute for the <img> element (eg "http://example.com/uploads/myimage.jpg").

However, if you want the administrator to be able to upload an image via the CMS when they add or edit an article then things get more complicated. See here for a bit more info:

http://www.elated.com/forums/topic/5114/#post20643

I might write a tutorial on this if there's enough demand

Matt

--
Matt Doyle, Elated
31-Jan-12 09:31
@Matt.
Hi to Matt and all Elated Community.
would you be expanding your cms, like adding commenting class, so that visitors can comment on Articles, or image uploading by cms administrators?

Thanks a lot.
Cheers.
01-Feb-12 03:37
Hi a real noob in php here:

For the Article Category when I'm adding/editing an article I get two notices saying:
Notice: Undefined variable: category in ..xampp\htdocs\cms\templates\admin\editArticle.php on line 33

Notice: Trying to get property of non-object in ..xampp\htdocs\cms\templates\admin\editArticle.php on line 33
selected>(none)

What's it saying? I mean when I choose a category then there's no problem so is it just telling me to that I really really have to select a category? or is there something wrong?

Another thing is that when I publish any article the publication date shows : "1 Jan 1970" what am I doing wrong :/

[Edited by hocfictum on 01-Feb-12 03:37]
01-Feb-12 13:39
Well, i'am trying to create a image upload within the script for days (still not succeed), so a tutorial would really help me!

It would be nice to attach a file to the Article anyway...
01-Feb-12 17:34
Hi
I tried to add Cyrillic articles and categories but it's not working.
It's not inserting Cyrillic data into my database.
How to solve this? please hurry.
Sorry for my bad english.

--
hello there
02-Feb-12 01:38
Hullo, I fixed the second issue (date) but the notices are still bugging me -
Can you also point me in the right direction if I wanted to create a user(limited) who isn't allowed to add categories?
02-Feb-12 14:14
@hocfictum

i'm facing same problem like you.

maybe our database does not support foreign key or transaction.
02-Feb-12 22:05
@hocfictum: Oops, that's a bug in the code.


<option value="0"<?php echo !$category->id ? " selected" : ""?>>(none)</option>


It should be:


<option value="0"<?php echo !$results['article']->categoryId ? " selected" : ""?>>(none)</option>


I'll fix up the tutorial!

Thanks
Matt

--
Matt Doyle, Elated
02-Feb-12 22:08
@mooho058: See:

http://www.elated.com/forums/topic/5114/#post21052
http://www.elated.com/forums/topic/5114/#post21056

You need to adjust your regular expression(s) to include Cyrillic characters.

--
Matt Doyle, Elated
02-Feb-12 22:35
@hocfictum: "Can you also point me in the right direction if I wanted to create a user(limited) who isn't allowed to add categories?"

If you're sure you're only going to want 2 admin users then you could create a new "user" by hacking config.php:


define( "SUBADMIN_USERNAME", "subadmin" );
define( "SUBADMIN_PASSWORD", "anotherpass" );


Then modify login() in admin.php to allow either user to login:


if ( $_POST['username'] == ADMIN_USERNAME && $_POST['password'] == ADMIN_PASSWORD || $_POST['username'] == SUBADMIN_USERNAME && $_POST['password'] == SUBADMIN_PASSWORD ) {

...etc...


Then hack the newCategory()/editCategory()/deleteCategory() functions to check that $_SESSION['username'] == ADMIN_USERNAME. If it doesn't, display an error message and exit.

If you think that you might want more admins later, with more fine-grained control, then the "proper" way to do it is to create a new admins table in your database, and store the admin usernames/passwords in there. You'd probably want to create an Admin class too, to handle storing/retrieving administrators, as well as "meta-admin" functions that let you add/edit/delete admins. But that's a fair bit more work.

--
Matt Doyle, Elated
23-Feb-12 17:45
Hello i just want to know how can i create a categories menu???
Thanks in advance
Also i would like to add pagination to this, could you point me in the right direction???

[Edited by metalsniper63 on 23-Feb-12 17:48]
27-Feb-12 22:13
@metalsniper63: Not sure what you mean by a "categories menu". If you mean navigation links to the different category archive pages then I'd just hard-code them in an unordered list in templates/include/header.php, and style the list appropriately.

I may write a tutorial on pagination at some point. In the meantime, see here:

http://www.elated.com/forums/topic/5114/#post22300

--
Matt Doyle, Elated
29-Feb-12 00:00
This is going great. Features I hope you will talk about in the future are :
Session timeout, log out after a certain amount of time
Improvements to the editor
Clean Urls

Of course, I am trying to work some of these out on my own, but I am sure you could do this things better.

Love the tutorials, please keep them coming.
01-Mar-12 05:11
@csturner: Thanks for the suggestions

--
Matt Doyle, Elated
03-Mar-12 09:38
Also, how do you add comments for each article?

--
Cheers!
Aditya
03-Mar-12 10:25
When I edit an article from my dashboard, in publication date, i'm having to give a date ahead.

Example- i need to give 4 feb 2012 to display 3 feb 2012...

--
Cheers!
Aditya
03-Mar-12 10:27
Also, when i edit article, some wierd \\\\\\\\ start appearing...Please help...

--
Cheers!
Aditya
05-Mar-12 00:29
@adityasaky: You'll find answers to all of your questions by searching through the topic for the original tutorial:

http://www.elated.com/forums/topic/5114/

--
Matt Doyle, Elated
05-Mar-12 13:26
Was just thinking, another useful feature would be allowing customers to post to testimonials. ie, Have a special post page which allows posting under a specific categoryId, with complete sanitation of the input. This no doubt, becomes very dangerous.
26-Mar-12 21:47
@Matt
hi matt, hope u doing good.
i went through the forum , to check wheather my question was answered before.

Our CMS get the news articles first. order by date DESC.
but what about if publish 10 articles a day and i want that the news article displayed at the top of the page by time. For example i published 10 articles today, date is same. But different hours and i want to see the news articles by time (hour) displayed at the top.

As we have already got date field in our database, i wonder wheather i could modify or need another additional field which holds my timestamp.
headlines.
>Microsoft anounces IE 9 date published and time.
>Microsoft anounces IE 10 date published and time.
>Microsoft anounces IE 11 date published and time.
Thanks
04-Apr-12 13:49
Hello there,
I need someones brilliant knowledge and mind to help me out!

The thing is, I want to display all articles and categories as it is in this tutorial, but i want to exclude the articles and categories with the ID 1 and ID 2 (both modified to the same ID in DB).

Beginner as I am, i tried this in Article.php:

public static function getList( $numRows=1000000, $categoryId=null, $order="publicationDate DESC" ) {
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$categoryClause = $categoryId ? "WHERE categoryId = :categoryId" : "";
$sql = "SELECT SQL_CALC_FOUND_ROWS *, UNIX_TIMESTAMP(publicationDate) AS publicationDate
FROM articles $categoryClause [b]WHERE (id > 2)[/b]
ORDER BY " . mysql_escape_string($order) . " LIMIT :numRows";


It works in the homepage.php and Article.php but not when i'm accessing a category, like 'images'.

What did I do wrong this time?
04-Apr-12 13:51
From the top of my head your code seems fine. but remember that categories are handled by categories.php in classes, you should put that code in there
04-Apr-12 20:23
@metalsniper63

Hmm.. Yes sir, you are totally correct with the 'categories.php'-thingy.

But,
Instead of manipulating the DB in the wrong way, and have a inflexible website with a "not so good idea after all", I decided to just create a new function like Categories, but call it Info. And it works

The only thing now is that I can not get data to display without the foreach{}.

I've got the code:

function viewInfo() {
$results = array();
$data = Info::getList();
$results['staticinfo'] = $data['results'];
$results['pageTitle'] = "Info | Kanske";
require( TEMPLATE_PATH . "/viewInfo.php" );}

in index.php.

Its working, but as i'm a real beginner (this is my absolute first PHP project), I have a hard time gettin' my head around it, hehe.

Well, if a smart person with allot of knowledge sees this, give me a hint or something, caus I'm stuck xD

Best regards,
JJ
05-Apr-12 06:38
@mubuuba: Modify your publicationDate database column to be a datetime type instead of date:

http://dev.mysql.com/doc/refman/5.5/en/datetime.html

Then modify the calls to the PHP date() function inside the templates (homepage.php etc) to also display the time in the format you want, as well as the date:

http://php.net/manual/en/function.date.php

--
Matt Doyle, Elated
05-Apr-12 07:21
@jj: I don't totally understand what you want to do, but your code looks like it will run OK. Maybe you could post the problem along with your complete code in a new topic, and we can take a closer look:

http://www.elated.com/forums/authoring-and-programming/topic/new/

--
Matt Doyle, Elated
16-Apr-12 21:05
Hi Matt,
Thank you very much for the tutorials.

I have several days trying to upload images from editArticle but I could not. It seems that I can not change the enctype to multipart / form-data.

I hope you can help me with this.
Greetings and Thank You!
04-May-12 06:05
@hch: This is how you do it:


<form enctype="multipart/form-data" ...>


--
Matt Doyle, Elated
12-Jun-12 12:22
Hi, how can you make it so that each article can have multiple categories EG sports, News. and Computing,news. So that both articles would appear in news but only then appear in the sub category they belong, I need something that can have up to 5 categories. Any help appreciated.

Ian
15-Jun-12 04:51
@snookian: Many ways to do that. If you're happy with a maximum of 5 cats per article then I'd probably just add 5 fields to your articles table (category1Id, category2Id, category3Id, category4Id, category5Id). Otherwise you'll need an "articleCategories" link table to link each article with its categories in the categories table, which becomes a bit more complex.

--
Matt Doyle, Elated
27-Jun-12 03:16
can you explain with xampp?please...
04-Dec-12 09:11
Hi, i've been implementing this tutorial into mysite and all the php is working fine, however I have two versions of my site one for mobile one for desktop so if a mobile browser is detected it will go to the mobile verison of my site now ive modified all the template files to suite my mobile design problem now is the desktop version is using the same template files im new to templating with php how would I go about sorting this out?
18-Dec-12 18:50
@lyall: I'd probably edit config.php and replace the TEMPLATE_PATH constant with TEMPLATE_PATH_DESKTOP and TEMPLATE_PATH_MOBILE. Then add a simple getTemplatePath() utility function to config.php that detects the browser type and returns the appropriate template path. Then replace all instances of TEMPLATE_PATH in the code with getTemplatePath().

--
Matt Doyle, Elated
30-Apr-13 09:15
@matt, Please help! Im super stumped. I want the description for each category to appear once you have selected the category.
So i want a input box under each category drop down list like so:



<li>
<label for="categoryId">Article Category</label>
<select name="categoryId">
<option value="0"<?php echo !$results['article']->categoryId ? " selected" : ""?>>(none)</option>
<?php foreach ( $results['categories'] as $category ) { ?>
<option value="<?php echo $category->id?>"<?php echo ( $category->id == $results['article']->categoryId ) ? " selected" : ""?>><?php echo htmlspecialchars( $category->name )?></option>
<?php } ?>
</select>
</li>

<li>
<label for="description">Description</label>
<input type="text" name="description" id="description" value="<?php echo $results['category']->description ?>" />
</li>



And then when you select reviews for example the description i put for reviews will appear in the description field. Any help getting me towards my goal will be great.

(I want/need this in the new article page/form)

Ian

[Edited by snookian on 30-Apr-13 09:49]
13-Jun-13 09:06
Hello Sir I tried combining the tutorial for image upload and category upload but is not working. Having debugged it, I discovered that the object has not extension property. Please tell me what to do
01-Jul-14 00:11
I'm looking for this for my simple project. It's so helpful, how to add some tags on article.
thanks in advance
01-Jul-14 04:50
"tags" meaning what exactly?

--
Chris.
So long, and thanks for all the fish.
http://webmaster-talk.eu/
02-Jul-14 11:12
This is a fantastic tutorial! I was completely stuck before I found this.

Does anyone know how I can store the last inserted article Id into another table? As in what this tutorial does with the category Id, but in reverse?

I've tried running a transaction in the insert function of the Article class but this causes an error with the image uploader from one of the other tutorials.

Any help would be appreciated. Thank you!
02-Jul-14 17:51
http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html

--
Chris.
So long, and thanks for all the fish.
http://webmaster-talk.eu/
02-Jul-14 22:42
Thank you chrishirst, I figured it out.

Can someone please tell my how to insert a time field (time only) into the database? I have been beating my head against the wall.
03-Jul-14 03:59
Hi chris,

Thanks for question. Like categories but it's different. Tags are similar to categories, but they are generally used to describe your post in more detail. You can see in WordPress that usually uses tags in post.
03-Jul-14 17:29
"Can someone please tell my how to insert a time field (time only) into the database"?

Use a timestamp field type and just insert the time parts, bearing in mind that when you read it back, the date will be 01/01/1970 (the UNIX Epoch)

--
Chris.
So long, and thanks for all the fish.
http://webmaster-talk.eu/
03-Jul-14 17:35
OK, so those kind of 'tags'

So basically it's EXACTLY the same as "categories" it's only the way you display them that differs.

--
Chris.
So long, and thanks for all the fish.
http://webmaster-talk.eu/
13-Apr-15 08:43
Good day! How can I add in the admin, category names in the file listArticles.php.
Example: article name, category name.
$article->categoryId displays only the category id
13-Apr-15 09:49
Found a solution:
<?php echo htmlspecialchars( $results['categories'][$article->categoryId]->name )?>
displays the name of the category
07-May-15 03:50
Hi all,
It's always good to have a professional developer around to support and make your work easy.
If you want help regarding your website design and functionality in any regard, contact us.
Spam REMOVED

[Edited by chrishirst on 07-May-15 04:14]
04-May-16 11:44
When i open edit article. I am not getting categories name in Article Category , only (none) shown, So when ever i edit any article it's category changes to no category after saving the article.

More over second Question is when i click any category name under title of any article it shows all articles of all categories not of the particular category.


<?php include "templates/include/header.php" ?>
<?php include "templates/admin/include/header.php" ?>



<div id="adminHeader">
<h2>Bedi News Admin</h2>
<p>You are logged in as <b><?php echo htmlspecialchars( $_SESSION['username']) ?></b>. <a href="admin.php?action=logout"?>Log out</a></p>
</div>

<h1><?php echo $results['pageTitle']?></h1>

<form action="admin.php?action=<?php echo $results['formAction']?>" method="post" enctype="multipart/form-data" onsubmit="closeKeepAlive()">
<input type="hidden" name="articleId" value="<?php echo $results['article']->id ?>"/>

<?php if ( isset( $results['errorMessage'] ) ) { ?>
<div class="errorMessage"><?php echo $results['errorMessage'] ?></div>
<?php } ?>

<ul>

<li>
<label for="title">Article Title</label>
<input type="text" name="title" id="title" placeholder="Name of the article" required autofocus maxlength="255" value="<?php echo $results['article']->title ?>" />
</li>

<li>
<label for="summary">Article Summary</label>
<textarea name="summary" id="summary" placeholder="Brief description of the article" required maxlength="1000" style="height: 5em;"><?php echo $results['article']->summary ?></textarea>
</li>

<li>
<label for="content">Article Content</label>
<textarea name="content" id="content" placeholder="The HTML content of the article" required maxlength="100000" style="height: 30em;"><?php echo $results['article']->content ?></textarea>
</li>

<li>
[b]<label for="categoryId">Article Category</label>
<select name="categoryId">
<option value="0"<?php echo !$results['article']->categoryId ? " selected" : ""?>>(none)</option>
<?php foreach ( $results['categories'] as $category ) { ?>
<option value="<?php echo $category->id?>"<?php echo ( $category->id == $results['article']->categoryId ) ? " selected" : ""?>><?php echo htmlspecialchars( $category->name )?></option>
<?php } ?>
</select>[/b]
</li>



<li>
<label for="publicationDate">Publication Date</label>
<input type="date" name="publicationDate" id="publicationDate" placeholder="YYYY-MM-DD" required maxlength="10" value="<?php echo $results['article']->publicationDate ? date( "Y-m-d", $results['article']->publicationDate ) : "" ?>" />
</li>

<?php if ( $results['article'] && $imagePath = $results['article']->getImagePath() ) { ?>
<li>
<label>Current Image</label>
<img id="articleImage" src="<?php echo $imagePath ?>" alt="Article Image" />
</li>

<li>
<label></label>
<input type="checkbox" name="deleteImage" id="deleteImage" value="yes"/ > <label for="deleteImage">Delete</label>
</li>
<?php } ?>

<li>
<label for="image">New Image</label>
<input type="file" name="image" id="image" placeholder="Choose an image to upload" maxlength="255" />
</li>

</ul>

<div class="buttons">
<input type="submit" name="saveChanges" value="Save Changes" />
<input type="submit" formnovalidate name="cancel" value="Cancel" />
</div>

</form>

<?php if ( $results['article']->id ) { ?>
<p><a href="admin.php?action=deleteArticle&amp;articleId=<?php echo $results['article']->id ?>" onclick="return confirm('Delete This Article?')">Delete This Article</a></p>
<?php } ?>

<?php include "templates/include/footer.php" ?>


[Edited by jatin333 on 05-May-16 05:16]

--
jatinbedi
05-May-16 11:16
Thanks it is solved Now every thing works fine

--
jatinbedi
20-Oct-16 20:00
Hi... followed the tutorials (love them, by the way) and I am now trying to figure out how to do a list of the article categories for a side menu, similiar to the list of categories in the admin area... i've got the menu showing the category names with a clickable url to that archive/category, however, it is throwing a php notice (PHP Notice: Undefined property: Category::$categoryId) and I can't seem to figure out why.

The following code is what i have added to the templates/homepage.php file to create the list of categories...


<h2>Article Categories</h2>
<ul>
<?php foreach ( $results['categories'] as $category ) { ?>
<li><a href=".?action=article_category&amp;categoryId=<?php echo $category->categoryId?>"><?php echo $category->name?></a></li>
<?php } ?>
</ul>


Is there perhaps something that i need to add to the index.php file or some silly little something that i'm missing.

Thanks for any and all help with this... <3
21-Oct-16 07:28
"Is there perhaps something that i need to add to the index.php file or some silly little something that i'm missing."

Yes there are, and the required changes are detailed in the original article;

http://www.elated.com/articles/add-article-categories-to-your-cms/

Scroll down to step 4:

--
Chris.
So long, and thanks for all the fish.
http://webmaster-talk.eu/
05-Nov-16 18:01
Thank you so very much, Chris. I managed to find time to spend on this project and went back over step 4 of original article, as you suggested, and figured it out.

Thanks again for the awesome tutorials and for pointing me in the right direction... <3
16-Apr-17 13:31
Hi,

Its great tutorials, but i have one query, the query below :-

If i want to retrive/display data from specific category, then what i have to do or what i have to do changes in code please help.

Please revert on my mail ASAP.

--
Devashish
Web Developer
18-Apr-17 08:05
"If i want to retrive/display data from specific category,"


Specify the category ID you want to display in the URL parameters

--
Chris.
So long, and thanks for all the fish.
http://webmaster-talk.eu/

 
New posts
Old posts

Follow Elated