Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 26, 2023 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/ccf38fee6b89c3504b438457f28b630b to your computer and use it in GitHub Desktop.
Save code-boxx/ccf38fee6b89c3504b438457f28b630b to your computer and use it in GitHub Desktop.
PHP save & load TinyMCE content into MYSQL

SAVE & LOAD TINYMCE CONTENT IN PHP MYSQL

https://code-boxx.com/save-load-tinymce-content-php-mysql/

NOTES

  1. Create test a database and import 1-contents.sql.
  2. Change the database settings in 2-lib.php to your own.
  3. Launch 3-save.php for the save content demo, 4-show.php to fetch and display.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

CREATE TABLE `contents` (
`content_id` bigint(20) NOT NULL,
`content_text` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `contents`
ADD PRIMARY KEY (`content_id`);
ALTER TABLE `contents`
MODIFY `content_id` bigint(20) NOT NULL AUTO_INCREMENT;
<?php
class Content {
// (A) CONSTRUCTOR - CONNECT TO DATABASE
public $pdo = null;
public $stmt = null;
public $error = "";
function __construct () {
$this->pdo = new PDO(
"mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET,
DB_USER, DB_PASSWORD, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
}
// (B) DESTRUCTOR - CLOSE DATABASE CONNECTION
function __destruct () {
if ($this->stmt!==null) { $this->stmt = null; }
if ($this->pdo!==null) { $this->pdo = null; }
}
// (C) EXECUTE SQL QUERY
function query ($sql, $data=null) : void {
$this->stmt = $this->pdo->prepare($sql);
$this->stmt->execute($data);
}
// (D) SAVE CONTENT
function save ($id, $content) {
$this->query("REPLACE INTO `contents` (`content_id`, `content_text`) VALUES (?,?)", [$id, $content]);
return true;
}
// (E) GET CONTENT
function get ($id) {
$this->query("SELECT `content_text` FROM `contents` WHERE `content_id`=?", [$id]);
return $this->stmt->fetchColumn();
}
}
// (F) DATABASE SETTINGS - CHANGE TO YOUR OWN!
define("DB_HOST", "localhost");
define("DB_NAME", "test");
define("DB_CHARSET", "utf8mb4");
define("DB_USER", "root");
define("DB_PASSWORD", "");
// (G) NEW CONTENT OBJECT
$_CONTENT = new Content();
<!DOCTYPE html>
<html>
<head>
<title>TinyMCE PHP MySQL Demo</title>
<meta charset="utf-8">
</head>
<body>
<?php
// (A) INIT
// (A1) LOAD LIBRARY, FIXED CONTENT ID FOR DEMO
require "2-lib.php";
$id = 99;
// (A2) SAVE CONTENT ON SUBMIT
if (isset($_POST["content"])) {
echo $_CONTENT->save($id, $_POST["content"])
? "<div>SAVED</div>"
: "<div>ERROR</div>" ;
}
// (B) HTML EDITOR ?>
<!-- (B1) HTML FORM -->
<form method="post">
<textarea id="mceDEMO" name="content"><?=$_CONTENT->get($id)?></textarea>
</form>
<!-- (B2) TINY MCE -->
<!-- https://cdnjs.com/libraries/tinymce -->
<!-- https://www.tiny.cloud/docs/configure/ -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/tinymce/6.2.0/tinymce.min.js"></script>
<script>
tinymce.init({
selector : "#mceDEMO",
plugins : "save, textcolor, lists",
menubar : false,
toolbar: "save | styles | bold italic | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist"
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>TINYMCE Show Contents</title>
<meta charset="utf-8">
<style>* { font-family: Arial, sans-serif; }</style>
</head>
<body><?php
require "2-lib.php";
echo $_CONTENT->get(99);
?></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment