Jump to content

TinyMCE for a PHP website


Danno

Recommended Posts

Hi folks,

 

I am very new to PHP, MySQL etc and am in the middle of designing my first application in PHP.

I want to install TinyMCE so I can easily edit pages from the admin backend. I have TinyMCE showing in the editindex.php page but am stuck on what I need to do in order to get it to send data to MySQL and for MySQL to send the data to the section on the index.php page where I want the data to be displayed.

Hope I am making sense here.

 

If anyone can please walk me through it, I would very much appreciate it and will credit you in the finished product, which I hope to release under the GPL.

 

Thanks in advance.

Danno

Link to comment
Share on other sites

Sounds like you are having issues with a general PHP/MySQL problem, not really anything directly to do with TinyMCE. All TinyMCE does is convert textareas into a WYSIWYG editor -- it doesn't handle the data management part.

 

To send data to the database, you'd most likely need to use $_POST to get the contents of the textarea, and then a MySQL insert statement to insert the data into the database. If you are updating existing content, you'd need to use a MySQL update statement.

http://www.tizag.com/phpT/postget.php

http://www.tizag.com/mysqlTutorial/mysqlinsert.php

http://www.tizag.com/mysqlTutorial/mysqlupdate.php

 

To get data from the database, you'd need to use a mySQL select statement.

http://www.tizag.com/mysqlTutorial/mysqlselect.php

Link to comment
Share on other sites

You might also want to look at a brief tutorial/example I posted: http://www.killersites.com/forums/topic/2282/basic-php-system-vieweditdeleteadd-records/

 

It may not necessarily be exactly what you are trying to do, but it may give you a starting point to work from as you experiment with php/mySQL. It gives you some basic code showing how to add/edit/view/delete records in a database.

Link to comment
Share on other sites

The part that's confusing me at the moment is creating a table in the database that will contain the info for the pages.

Like, I know that CREATE TABLE tells MySQL that I want to create a new table etc.

What's killing me is what I need to do to create rows for each page. Or, do I need to create a table for each page?

 

Like, for say, index.php:

 

CREATE TABLE `content` (
 `indexpage` text NOT NULL,
 `contactpage` text NOT NULL
);

 

Or:

 

CREATE TABLE `indexpage` (
 `content` text NOT NULL
);

 

As I said, I am new to this lol :D

Link to comment
Share on other sites

Ok here is what I am at.

 

I want to create a website that will make it easier for those who have no webdesign knowledge at all, to have their own website where they can update the content on each page using WYSIWYG.

So, say they want to update their index.php page, they login to admin and go to editindex.php where it will display TinyMCE with the content in the editor. They make the changes they want, click save, and the changes are visible in index.php.

 

I know I am jumping right in for someone with very little knowledge of PHP and MySQL but I know with a little tuition I can do this.

Link to comment
Share on other sites

First off, realize that there are a lot of preexisting tools for this, so you don't necessarily have to reinvent the wheel. For example, Wordpress, ExpressionEngine, CMSMadeSimple, MODx, Concrete5, etc etc... And of course there are "lite" CMS's such as CushyCMS, Unify, etc that don't really have the full power of a CMS but allows simple content editing.

http://php.opensourcecms.com/scripts/show.php?catid=1&cat=CMS%20/%20Portals

 

That said... In regards to your original question... You'll want one table to hold all your content, rather than separate tables for each page. You'll probably want to have a couple columns in that table... a unique id column (probably using MySQL's auto increment feature -- google it if you don't know what that is), a page_id column, so you can specify which page the content belongs to, probably a date column showing when the content was last edited, and finally the content itself. This would allow you to potentially add multiple pieces of content per page. You could have one content area for the main content, another for sidebar content, etc.

 

You probably also want a "pages" table, that indicates which pages exist in your site. It will need a unique id column (that would match the same page_id in the content table), the page name, and any other data you need to store. Having a pages table will help allow your system to be dynamic, so you can add/remove pages as necessary. You don't expect every person to need the exact same pages, do you?

 

To summarize:

 

Content table:

-- id column (unique, auto increment)

-- page_id column (matches the id column in the pages table)

-- last_updated (last edit)

-- content

 

Pages table:

-- id (unique, auto increment)

-- title

-- additional columns as necessary

 

To display a specific piece of content, you'd need to know the unique id of the content. You would then use a MySQL select statement to retrieve that specific row in the table.

Link to comment
Share on other sites

One other thing... Since it sounds like you are getting started with PHP and MySQL, you might want to look at "how to build a cms" tutorials. I'm not saying you should base your system on a tutorial, but it might be a good idea for you to read over a couple and get a general sense of what's involved and what others have done.

 

http://css-tricks.com/php-for-beginners-building-your-first-simple-cms/

http://www.devshed.com/c/a/Administration/Building-a-Barebones-Content-Management-System-an-Introduction/

google "build a content management system"

 

This isn't a cms, but I'd definitely recommend looking at this tutorial: http://css-tricks.com/examples/WebAppFromScratch/

Link to comment
Share on other sites

Thanks a million mate.

 

You mentioned about WP, Joomla! etc.

I agree. There are loads and loads of ready-made applications out there that people can use and are easy to install. I personally use Joomla! and WP for my sites.

However, most of the packages available are bloated with features.

My plan is to create something that is severely lightweight. What I have done so far, which is the layout and design, is something like 60kb and that's including Readme and License files etc. Takes about 3 seconds to upload and extract the zip file in cPanel.

Now, that being said, there are lots of systems out there that are just like that. But, I guess by doing this one, I get to learn and advance and people interested in using it, get an easy to use website for free. :D

 

Joomla! and Wordpress are fantastic apps and there are many more out there that are just as good. Silverstripe, for one, has great potential. But I want to contribute to the Open Source world and by doing something like this, I can sit back, hold my head up and say "I did that!". :D

Link to comment
Share on other sites

But I want to contribute to the Open Source world and by doing something like this, I can sit back, hold my head up and say "I did that!"

Fair enough. I know that feeling too. Besides, if whoever came up with Wordpress (or similar systems) had said "I'll just use something prebuilt" then maybe they would have never happened.

Link to comment
Share on other sites

Fair enough. I know that feeling too. Besides, if whoever came up with Wordpress (or similar systems) had said "I'll just use something prebuilt" then maybe they would have never happened.

 

Exactly.

I forked apps before. Easy as pie to do. But I could never say it was mine. I'm over that crap now. Wanna do something of my own. But, need as much tuition as possible to get started and so far, you've put me in a good direction.

Link to comment
Share on other sites

TinyMCE just replaces this text area with something more advanced and with some features. Am I right?

Correct.

 

After taking a very brief glance at the code, I believe this line is the issue:

 

$sql="INSERT INTO articles (pagetitle, content) VALUES ('$_POST[pagetitle]','$_POST[content]')";

 

Try adding single quotes around the value inside the [] like so:

 

$sql="INSERT INTO articles (pagetitle, content) VALUES ('$_POST['pagetitle']','$_POST['content']')";

 

I haven't tested it myself, but that's the first thing I'd test. I'm pretty sure that the single quotes within $_POST[] are required.

Link to comment
Share on other sites

I changed go.php to the following:

 

<?php
include '../includes/config.php';
$mysql = array();    // create array of mysql escaped values
$mysql['pagetitle'] = mysql_real_escape_string( $_POST['pagetitle'] );
$mysql['content'] = mysql_real_escape_string($_POST['content'] );
$sql = "INSERT INTO articles ( pagetitle, content ) VALUES ( '{$mysql['pagetitle, content']}' )";   //insert your comment to db
mysql_query( $sql );
?>

 

It gives a blank page, which tells me I am doing something right. But it's not sending the info to the database.

 

Maybe I am misunderstanding this.

This is meant to insert the content into the pagetitle and content fields in my db right?

Link to comment
Share on other sites

Making Progress!!!

 

I changed go.php again to:

<?php
include '../includes/config.php';
$insert = "INSERT INTO articles (pagetitle, content)
VALUES ('".$_POST['pagetitle']."', '".$_POST['content']."')";
$add_data = mysql_query($insert);
?>

 

It sends the content data to the relevent field in MySQL. However, it seems the pagetitle is not being sent or inserted.

Just need to sort that young man out and then get the code to display. Which will not be too hard.

Link to comment
Share on other sites

 

include '../includes/config.php';
$insert = "INSERT INTO articles (pagetitle, content)
VALUES ('".$_POST['pagetitle']."', '".$_POST['content']."')";
$add_data = mysql_query($insert);
?>

 

Don't tell me you are just puting in $_POST[] variables without checking them, as that is the biggest no-no you can do in terms of php security. Read up on SQL injections as your code has nothing that even tries to prevent them.

Link to comment
Share on other sites

 

<?php
include '../includes/config.php';
$insert = "INSERT INTO articles (pagetitle, content)
VALUES ('".$_POST['pagetitle']."', '".$_POST['content']."')";
$add_data = mysql_query($insert);
?>

 

Don't tell me you are just puting in $_POST[] variables without checking them' date=' as that is the biggest no-no you can do in terms of php security. Read up on SQL injections as your code has nothing that even tries to prevent them.[/quote']

I'm just putting it together at the mini mate. Will add the security later once I have everything working. Thanks for that. :)

 

Update. Got the pagetitle working now too.

Link to comment
Share on other sites

All working :D

I send the data from the form to go.php, which in turn sends it to MySQL. I then use the following code to get the table data and display it on the page:

 

<?php
$sql = "select * from articles";
$result = mysql_query ($sql);

while ($row = mysql_fetch_array($result))
{
$field1= $row["content"];
$field2= $row["pagetitle"];

echo "$field2
";
echo "$field1
";

}
?> 

 

The next things I need to do:

 

1. Find how to keep the edited text in the editor so I can come back at a later date and update it.

2. As mentioned by krillz, I gotta make everything secure.

 

I am posting everything here, and will also post on my personal blog later so others may find it helpful.

Link to comment
Share on other sites

Ok I am stuck again.

Now, when I submit new text, what I want it to do is replace the current content. Instead, it adds to it. Be great if I was doing a Blog, but I aint. :D

 

Any ideas?

I searched Google but can only find Delete or Update. Neither work.

Link to comment
Share on other sites

Well, let me step you through it then. Here's the important line from the example:

 

$result = mysql_query("UPDATE example SET age='22' WHERE age='21'"); 

 

The basic syntax is this:

 

Update [table_name] SET [values] WHERE [condition]

 

In this example, they are setting the "age" column in the database wherever the value in the age column equals 21. So, if there were two or more rows in the "example" table where the age column equaled 21, this query would update both of them to 22. For more info on WHERE take a look at http://www.tizag.com/mysqlTutorial/mysqlwhere.php

 

Another example... Here's the update query from the view/add/edit/delete tutorial I have in the PHP section of the KillerSites forum:

 

mysql_query("UPDATE players SET firstname='$firstname', lastname='$lastname' WHERE id='$id'");

 

In this case, I am setting the firstname and lastname fields in the database where the id field equals a certain value. That's basically what you'll need to do, update the applicable fields in the database with your new content, with the "WHERE" section of the code specifying the id of the content you want to update.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...