Forums

Create article with post data

kikookik 07 May, 2013
Hello everyone;
I have a chronoform form that post the post data into a file called JOOMLA_DIR/scripts/req.php.
The problem is that i don't know how to create an article into a specific category id, lets suppose it 49. My joomla version is 2.5.
This is the code i tested, so i did remove the $_POST variables only to make the code more easy to analyse.
Thank you for your help.

<?php
define('_JEXEC', 1);
define('JPATH_BASE', realpath(dirname(__FILE__)));
require_once(JPATH_BASE.'/includes/defines.php');
require_once(JPATH_BASE.'/includes/framework.php');
require_once(JPATH_BASE.'/libraries/joomla/factory.php');
if (!defined('DS')) {	
	define('DS', DIRECTORY_SEPARATOR);
}
require_once(JPATH_BASE.'/libraries/joomla/application/component/helper.php');
jimport('joomla.application.component.modeladmin');
require_once(JPATH_COMPONENT_ADMINISTRATOR.'/helpers/content.php');
require_once(JPATH_ADMINISTRATOR.'/components/com_content/models/article.php');

/*
$new_article = new ContentModelArticle();

$data = array(
    'catid' => 49,
    'title' => 'SOME TITLE',
    'introtext' => 'SOME TEXT',
    'fulltext' => 'SOME TEXT',
    'state' => 1,
);
$new_article->save($data);*/
$table = JTable::getInstance('content', 'JTable');

$data = array(
    'catid' => 49,
    'title' => 'SOME TITLE',
    'introtext' => 'SOME TEXT',
    'fulltext' => 'SOME TEXT',
    'state' => 1,
);

// Bind data
if (!$table->bind($data))
{
    $this->setError($table->getError());
    return false;
}

// Check the data.
if (!$table->check())
{
    $this->setError($table->getError());
    return false;
}

// Store the data.
if (!$table->store())
{
    $this->setError($table->getError());
    return false;
}
?>
GreyHead 08 May, 2013
Hi kikookik,

This doesn't look like a ChronoForms question. I don't understand what you are trying to do, why you are using this code or what the req.php file does???

Bob
kikookik 08 May, 2013
Thank you for your reply. I am talking about creating a fresh new Joomla article withing data of post request.
This is already answered in this post : here
But for some reason, this procedure isn't working with my Joomla 2.5. The article isn't created.


<?php
define('_JEXEC', 1);
define('JPATH_BASE', realpath(dirname(__FILE__)));
require_once(JPATH_BASE.'/includes/defines.php');
require_once(JPATH_BASE.'/includes/framework.php');
require_once(JPATH_BASE.'/libraries/joomla/factory.php');

$db	= JFactory::getDBO();
$query = "
      INSERT INTO `#__content`
      SET `title` = '{My title}',
      `alias` = 'article-56',
      `fulltext` = 'body',
      `state` = '1',
      `catid` = '49',
      `created` = '".date("Y-m-d H:i:s")."',
  ";

$db     ->setQuery($query);
$db     ->query();
?>

This code didn't create any article in the category id=49 why ?
GreyHead 08 May, 2013
Hi kikookik,

These two lines shouldn't have spaces in them:

$db->setQuery($query);
$db->query();

Why not use the Submit Article action?

Bob
kikookik 08 May, 2013
Because, i want to create articles based on a predefined values, not random data that user insert if using edit button in the frontpage. So for the code it still not working. This is an update :

<?php
define('_JEXEC', 1);
define('JPATH_BASE', realpath(dirname(__FILE__)));
require_once(JPATH_BASE.'/includes/defines.php');
require_once(JPATH_BASE.'/includes/framework.php');
require_once(JPATH_BASE.'/libraries/joomla/factory.php');

$db	= &JFactory::getDBO();
$query	= $db->getQuery(true);
$columns = array('title', 'alias', 'introtext', 'state', 'fulltext', 'catid');
$values	 = array('Some text', 'last-form-article-458', 'this is', 0, 'A descri about, so start here.', 49);
$query->insert($db->quoteName('#__content'))->columns($db->quoteName($columns))->values(implode(',', $values));
$db->setQuery($query);
$db->query();
?>
GreyHead 08 May, 2013
Hi kiookik,

Sorry I can't debug your custom code for you :-( I don't know what $query->insert() does; that's not a function that I am familiar with.

I suggest that you add debug code to output the query so you can see exactly what you are building.

Bob
This topic is locked and no more replies can be posted.