Add Authors 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

09-Apr-12 12:29
Hello there!

I apologize for my bad English and my beginner skills in php
And i really hope that someone can help me out!

I'm right now adding an Author-field to "Add Article Categories to Your CMS". It's goin' good! But now I'm stuck with the index.php-file and/or Article.php

I cant get my head around what to do when adding all the variables and functions to the index-file an Article-file.

Basically, I have multiplied all the 'categories'-functions in every file and its no problem with the db-connection when editing and saving the content.
But it is problem with the author-dropdown list in "editArticle.php", so I can not see any created author, therefor not assign author to articles. It is also problem with the "title" in the archive.php, caus now I need to see if it is an author or a category. I can neither see any article assigned to the author in the archive.php-file, nothing is echoing. Epic sadface.

What can I do?

My code in the index.php looks like this:

function archive() {
$results = array();
$categoryId = ( isset( $_GET['categoryId'] ) && $_GET['categoryId'] ) ? (int)$_GET['categoryId'] : null;
$results['category'] = Category::getById( $categoryId );
$data = Article::getList( 100000, $results['category'] ? $results['category']->id : null );
$results['articles'] = $data['results'];
$results['totalRows'] = $data['totalRows'];
$data = Category::getList();
$results['categories'] = array();
foreach ( $data['results'] as $category ) $results['categories'][$category->id] = $category;

$authorId = ( isset( $_GET['authorId'] ) && $_GET['authorId'] ) ? (int)$_GET['authorId'] : null;
$results['author'] = Author::getById( $authorId );
$data = Article::getList( 100000, $results['author'] ? $results['author']->id : null );
$results['articles'] = $data['results'];
$results['totalRows'] = $data['totalRows'];
$data = Author::getList();
$results['authors'] = array();
foreach ( $data['results'] as $author ) $results['authors'][$author->id] = $author;

$results['pageHeading'] = $results['author'] ? $results['author']->name : "Article Archive";
$results['pageHeading'] = $results['category'] ? $results['category']->name : "Article Archive";
$results['pageTitle'] = $results['pageHeading'] . " | Your Blog";
require( TEMPLATE_PATH . "/archive.php" );
}

function viewArticle() {
if ( !isset($_GET["articleId"]) || !$_GET["articleId"] ) {
homepage();
return;
}

$results = array();
$results['article'] = Article::getById( (int)$_GET["articleId"] );
$results['category'] = Category::getById( $results['article']->categoryId );
$results['author'] = Author::getById( $results['article']->authorId );
$results['pageTitle'] = $results['article']->title . " | Your Blog";
require( TEMPLATE_PATH . "/viewArticle.php" );
}

function homepage() {
$results = array();
$data = Article::getList( HOMEPAGE_NUM_ARTICLES );
$results['articles'] = $data['results'];
$results['totalRows'] = $data['totalRows'];
$data = Category::getList();
$results['categories'] = array();
foreach ( $data['results'] as $category ) $results['categories'][$category->id] = $category;
$data = Author::getList();
$results['authors'] = array();
foreach ( $data['results'] as $author ) $results['authors'][$author->id] = $author;
$results['pageTitle'] = "Your Blog";
require( TEMPLATE_PATH . "/homepage.php" );
}



And the Article.php:


<?php

/**
* Class to handle articles
*/

class Article
{
// Properties

/**
* @var int The article ID from the database
*/
public $id = null;

/**
* @var int When the article is to be / was first published
*/
public $publicationDate = null;

/**
* @var int The article category ID
*/
public $authorId = null;


public $categoryId = null;

/**
* @var string Full title of the article
*/
public $title = null;

/**
* @var string A short summary of the article
*/
public $summary = null;

/**
* @var string The HTML content of the article
*/
public $content = null;



/**
* Sets the object's properties using the values in the supplied array
*
* @param assoc The property values
*/

public function __construct( $data=array() ) {
if ( isset( $data['id'] ) ) $this->id = (int) $data['id'];
if ( isset( $data['publicationDate'] ) ) $this->publicationDate = (int) $data['publicationDate'];
if ( isset( $data['authorId'] ) ) $this->authorId = (int) $data['authorId'];
if ( isset( $data['categoryId'] ) ) $this->categoryId = (int) $data['categoryId'];
if ( isset( $data['title'] ) ) $this->title = preg_replace ( "[^\,\'\@\?\!\$ a-zA-Z0-9äöüßÄÖÜ()]", "", $data['title'] );
if ( isset( $data['summary'] ) ) $this->summary = preg_replace ( "[^\,\'\@\?\!\$ a-zA-Z0-9äöüßÄÖÜ()]", "", $data['summary'] );
if ( isset( $data['content'] ) ) $this->content = $data['content'];
}


/**
* Sets the object's properties using the edit form post values in the supplied array
*
* @param assoc The form post values
*/

public function storeFormValues ( $params ) {

// Store all the parameters
$this->__construct( $params );

// Parse and store the publication date
if ( isset($params['publicationDate']) ) {
$publicationDate = explode ( '-', $params['publicationDate'] );

if ( count($publicationDate) == 3 ) {
list ( $y, $m, $d ) = $publicationDate;
$this->publicationDate = mktime ( 0, 0, 0, $m, $d, $y );
}
}
}


/**
* Returns an Article object matching the given article ID
*
* @param int The article ID
* @return Article|false The article object, or false if the record was not found or there was a problem
*/

public static function getById( $id ) {
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "SELECT *, UNIX_TIMESTAMP(publicationDate) AS publicationDate FROM articles WHERE id = :id";
$st = $conn->prepare( $sql );
$st->bindValue( ":id", $id, PDO::PARAM_INT );
$st->execute();
$row = $st->fetch();
$conn = null;
if ( $row ) return new Article( $row );
}


/**
* Returns all (or a range of) Article objects in the DB
*
* @param int Optional The number of rows to return (default=all)
* @param int Optional Return just articles in the category with this ID
* @param string Optional column by which to order the articles (default="publicationDate DESC")
* @return Array|false A two-element array : results => array, a list of Article objects; totalRows => Total number of articles
*/

public static function getList( $numRows=1000000, $categoryId=null, $authorId=null, $order="publicationDate DESC" ) {
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$categoryClause = $categoryId ? "WHERE categoryId = :categoryId" : "";

$authorClause = $authorId ? "AND authorId = :authorId" : "";

$sql = "SELECT SQL_CALC_FOUND_ROWS *, UNIX_TIMESTAMP(publicationDate) AS publicationDate
FROM articles $categoryClause $authorClause
ORDER BY " . mysql_escape_string($order) . " LIMIT :numRows";

$st = $conn->prepare( $sql );
$st->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
if ( $categoryId ) $st->bindValue( ":categoryId", $categoryId, PDO::PARAM_INT );
if ( $authorId ) $st->bindValue( ":authorId", $authorId, PDO::PARAM_INT );
$st->execute();
$list = array();

while ( $row = $st->fetch() ) {
$article = new Article( $row );
$list[] = $article;
}

// Now get the total number of articles that matched the criteria
$sql = "SELECT FOUND_ROWS() AS totalRows";
$totalRows = $conn->query( $sql )->fetch();
$conn = null;
return ( array ( "results" => $list, "totalRows" => $totalRows[0] ) );
}


/**
* Inserts the current Article object into the database, and sets its ID property.
*/

public function insert() {

// Does the Article object already have an ID?
if ( !is_null( $this->id ) ) trigger_error ( "Article::insert(): Attempt to insert an Article object that already has its ID property set (to $this->id).", E_USER_ERROR );

// Insert the Article
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "INSERT INTO articles ( publicationDate, authorId, categoryId, title, summary, content ) VALUES ( FROM_UNIXTIME(:publicationDate), :authorId, :categoryId, :title, :summary, :content )";
$st = $conn->prepare ( $sql );
$st->bindValue( ":publicationDate", $this->publicationDate, PDO::PARAM_INT );
$st->bindValue( ":authorId", $this->authorId, PDO::PARAM_INT );
$st->bindValue( ":categoryId", $this->categoryId, PDO::PARAM_INT );
$st->bindValue( ":title", $this->title, PDO::PARAM_STR );
$st->bindValue( ":summary", $this->summary, PDO::PARAM_STR );
$st->bindValue( ":content", $this->content, PDO::PARAM_STR );
$st->execute();
$this->id = $conn->lastInsertId();
$conn = null;
}


/**
* Updates the current Article object in the database.
*/

public function update() {

// Does the Article object have an ID?
if ( is_null( $this->id ) ) trigger_error ( "Article::update(): Attempt to update an Article object that does not have its ID property set.", E_USER_ERROR );

// Update the Article
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "UPDATE articles SET publicationDate=FROM_UNIXTIME(:publicationDate), authorId=:authorId, categoryId=:categoryId, title=:title, summary=:summary, content=:content WHERE id = :id";
$st = $conn->prepare ( $sql );
$st->bindValue( ":publicationDate", $this->publicationDate, PDO::PARAM_INT );
$st->bindValue( ":authorId", $this->authorId, PDO::PARAM_INT );
$st->bindValue( ":categoryId", $this->categoryId, PDO::PARAM_INT );
$st->bindValue( ":title", $this->title, PDO::PARAM_STR );
$st->bindValue( ":summary", $this->summary, PDO::PARAM_STR );
$st->bindValue( ":content", $this->content, PDO::PARAM_STR );
$st->bindValue( ":id", $this->id, PDO::PARAM_INT );
$st->execute();
$conn = null;
}


/**
* Deletes the current Article object from the database.
*/

public function delete() {

// Does the Article object have an ID?
if ( is_null( $this->id ) ) trigger_error ( "Article::delete(): Attempt to delete an Article object that does not have its ID property set.", E_USER_ERROR );

// Delete the Article
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$st = $conn->prepare ( "DELETE FROM articles WHERE id = :id LIMIT 1" );
$st->bindValue( ":id", $this->id, PDO::PARAM_INT );
$st->execute();
$conn = null;
}

}
?>


Best regards!
/JJ
19-Apr-12 14:44
No smart one that can give me a hint of what to do?

I'm sure there are someone out there that have done this
27-Apr-12 18:38
@jj: Sorry, I didn't totally understand the question. But it looks like your Article::getList() method expects a $categoryId argument, which you're not passing in your archive() function.

--
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