loadObjectList();$cat_options = array();$cat_options[] = "Jobs";foreach ( $categories as $c ) { $selected = ''; if ( $ws_id && $d->cat_id == $c->id ) { $selected = "selected='selected'"; } $cat_options[] = "id}' {$selected} >{$c->title}";}?> Category  but now I dunno how to submit the category and save the article with the selected category. Can please someone help me?Thanks."> submit article: issue with categories - Forums

Forums

submit article: issue with categories

mayhem 25 Apr, 2013
Hi,

I created a form to submit an article but I dunno how to send the category.

I used a "custom element PHP/HTML" as Element and I put it before the "Submit Article" to display all the categories. Here is the code:

<?php
$db =& JFactory::getDBO();
$query = "
   SELECT `id`, `title`, `alias`
      FROM `#__categories`
      WHERE `published` = 1 AND `access` = 1
      ORDER BY `title`
";
$db->setQuery($query);
$categories = $db->loadObjectList();
$cat_options = array();
$cat_options[] = "<option value='' >Jobs</option>";
foreach ( $categories as $c ) {
   $selected = '';
   if ( $ws_id && $d->cat_id == $c->id ) {
      $selected = "selected='selected'";
   }
   $cat_options[] = "<option value='{$c->id}' {$selected} >{$c->title}</option>";
}

?>

<div class="form_item">
  <div class="form_element cf_select">
    <label class="cf_label" style="width: 150px;">Category</label>
    <select class="cf_select" title="" id="cat_id" name="cat_id" >
    <?php echo implode("/n", $cat_options); ?>
    </select>
  </div>
  <div class="cfclear"> </div>
</div>


but now I dunno how to submit the category and save the article with the selected category. Can please someone help me?

Thanks.
GreyHead 27 Apr, 2013
Hi mayhem ,

Unfortunately the Submit Article action doesn't support the Category as a variable :-( You can only set it as a fixed value from the action Configuration.

To do this you'd need to hand-code the article save.

Bob
mayhem 27 Apr, 2013
Hi Bob,

but I don't get it what you mean. In my array, the [cat_id] is set to 16 which it is a real id of a real category.

Data Array: 
Array
(
    [option] => com_chronoforms
    [tmpl] => component
    [chronoform] => Submit
    [event] => submit
    [Itemid] => 
    [input_text_8] => myemail@email.com
    [Title] => ee
    [Author] => ee
    [Full] => ee
    [metadesc] => ee
    [metakey] => ee
    [featured] => 1
    [cat_id] => 16
    [input_submit_4] => Submit
    [5b21fb99bb737bad508436a35fda52f8] => 1
)
Validation Errors: 
Array
(
)
GreyHead 28 Apr, 2013
Hi mayhem,

Yes, but the Submit Article action doesn't pay any attention to that. It only uses the Category preset in the action configuration.

There is a case for a much better Submit Article action - maybe one day.

Bob
mayhem 28 Apr, 2013
Hi Bob,

so maybe I am not so confident with that and I don't understand if there is a way to do it.

Is it the simplest way to change directly the value in the DB or other way is possible?

Cheers.
GreyHead 29 Apr, 2013
Hi Mayhem,

It's possible to do it by running the Submit Article action then using a Custom Code action after that to update the category. If you add a debugger temporarily you'll see the new article id in the $form->data (probably as $form->data['cf_id'] and yo can use this to identify the record to update.

Bob
mayhem 29 Apr, 2013
Hi Bob,

I was thinking to update directly the DB because later I'll need to update an external DB.

This is my custom code before "submit event".

<?php
$hostname = 'host.com';
$username = 'user';
$password = 'password';
$dbname = 'dbname';
$categoryid = '17';
$itemid = '110';

$db = new PDO("mysql:host=$hostname;dbname=$database",$username,$password);
$update_stmt = $db->prepare(' 
            UPDATE
               #__content
            SET
               catid='$categoryid'
            WHERE
               id='$itemid'
        ');

$update_stmt->execute(); 
$update_stmt->close();
?>


but an error appear when I'll submit. In detail:
Parse error: syntax error, unexpected T_VARIABLE in components/com_chronoforms/form_actions/custom_code/custom_code.php(19) : eval()'d code on line 13

Could you please help me?

Thanks.
GreyHead 29 Apr, 2013
Hi mayhem,

Your quotes round/in the query string are wrong in a couple of ways. This may be better:
$update_stmt = $db->prepare("
            UPDATE
               `#__content`
            SET
               `catid` = '{$categoryid}'
            WHERE
               `id` = '{$itemid}' ;
        ");

Bob
mayhem 30 Apr, 2013
Hi Bob,

I replaced my old code with yours but the DB still doesn't affect the changes.

Indeed, the field 'catid' still be 17 in the 'id' 110.

Do you know why?

By the way, I had to delete the last statement

$update_stmt->close();


because an error appears saying "Fatal error: Call to undefined method PDOStatement::close()"

Thanks.
GreyHead 30 Apr, 2013
Hi mayhem,

I've no idea what the PDO code is - nothing that I recognise. I assumed that you had some reason for using that.

There are hundreds of examples of the Joomla! database code in the forums and FAQs.

Bob
mayhem 30 Apr, 2013
Hi Bob,

just to be usefull for other users. The following is working solution:

<?php
//DB Connection
$Config = new JConfig();

//$option['driver']   = $Config->dbtype; 
$option['host']     = $Config->host;     // Database host name
$option['user']     = $Config->user;     // User for database 
$option['password'] = $Config->password; // Password for database 
$option['dbname'] = $Config->db;       // Database name
//$option['prefix']   = $Config->dbprefix; // Database prefix

$db = & JDatabase::getInstance($option);

$categoryid = '17';
$itemid = '110';

$db =& JFactory::getDBO();
$query = "UPDATE
               `#__content`
            SET
               `catid` = '{$categoryid}'
            WHERE
               `id` = '{$itemid}' " ;
$db->setQuery($query);
$db->query();
?>
inocampo 11 Jan, 2014
Thank you mayhen for:

$Config = new JConfig();

Work very good.
This topic is locked and no more replies can be posted.