vendredi 8 mai 2015

Write to Joomla database

I have a cool component for joomla that help me register users by creating a url (see the link). joomla extention

What i would like to do is to be able to write on a different table using the same method: This is the code that help with user registration:

//http://ift.tt/1Qt87yo
public function registration()
{
    $name = JRequest::getVar('name');
    $username = JRequest::getVar('username');
    $passwd = JRequest::getString('pass');
    $email = JRequest::getVar('email');


    $data = array(
          "name"=>$name,
          "username"=>$username,
          "password"=>$passwd,
          "password2"=>$passwd,
          "email"=>$email,
          "block"=>1,
          "groups"=>array("2"),
          "sendEmail"=>("1"),
        );

    $user = new JUser;
    //Write to database
    if(!$user->bind($data)) {
        $status = "Could not bind data. Error: " . $user->getError();
    }
    if (!$user->save()) {
      $status = "Could not save user. Error: " . $user->getError();
    }
    else {
      $status = "Success";
    }

    $message = array(
        'message' => $status
        );

    header('Content-Type: application/json');
    echo json_encode ($message);
    jexit();
}

To be more explicit, i would like to be able to write comments in a table called belvw_zoo_comment Is there a way to do that by just modifying the above code? i m thinking of something like this:

     //http://ift.tt/1zRgYWI
   public function comment()
  {
  $author = JRequest::getVar('author');
  $email = JRequest::getVar('email');
  $content = JRequest::getVar('content');


  $data = array(
       "author"=>$author,
       "email"=>$email,
       "content"=>$content,

        );

   $comment = new comment;
   //Write to database
   if(!$comment->bind($data)) {
       $status = "Could not bind data. Error: " . $user->getError();
    }
    if (!$comment->save()) {
      $status = "Could not save user. Error: " . $user->getError();
    }
    else {
     $status = "Success";
    }

    $message = array(
       'message' => $status
       );

    header('Content-Type: application/json');
    echo json_encode ($message);
    jexit();
}

Of course the above code is not working.

Aucun commentaire:

Enregistrer un commentaire