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
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
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
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
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
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
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
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
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
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
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
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
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
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
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
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
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 20: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 12: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 12: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 19: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 05: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
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
05-Apr-12 06: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
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
16-Apr-12 20: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 05:05
@hch: This is how you do it:


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


--
Matt Doyle, Elated
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
12-Jun-12 11: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 03: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
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
27-Jun-12 02: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
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/

 
New posts
Old posts

Follow Elated