Developing my website using PHP and MySQL
My website is currently all static HTML pages, and I've been looking into developing it into a dynamic PHP website with all the information stored in a MySQL database. It's for a Taekwondo club that I train at, and I'm just wondering how I'd go about organising the information.
For example,
The basic PHP site that I've built to learn it with is set up using pages and subjects, and the pages are listed accordingly - 'new_page.php', 'edit_page.php', 'new_subject.php', 'edit_subject.php' etc. There is one 'index.php page', and the number changes for each page that's loaded. For example, as 'About Us' is position 2, the page URL is 'index.php?page=2' etc.
Now I want to go one step further and add a few more levels. For example, one page will be labeled 'Profiles' and will contain Profiles and information of the club's Black Belts. However, I don't see how I'd be able to do this using just the one 'index.php' page?
Other similar websites I've looked at are broken up onto different pages - 'about.php', 'news.php', 'classes.php' etc., and these pages are then expanded - i.e. 'profiles.php?id=1' or 'news.php?id=3'.
But, how would I then set up an edit_page.php and link this to every page to edit each page?
pr0gr4mm3r posted this at 14:05 — 9th July 2008.
He has: 1,502 posts
Joined: Sep 2006
On the websites you mention, each file probably represents a MySQL table. For example, the index file would use the table that holds the generic pages, the profiles.php page uses the MySQL table that stores the profiles, etc.
For each type of data (pages, profiles, news articles, etc), you will need a different database table, different script to show them, and a different script to edit them. You will need edit_page.php, edit_profile.php, and edit_news.php.
Welcome to the forums, by the way. Hope you come back often.
What kind of programming history do you have? Consider introducing yourself and telling us a little bit about you.
decibel.places posted this at 14:39 — 9th July 2008.
He has: 1,494 posts
Joined: Jun 2008
I am working on a site that appears to be based on Mambo and/or Joomla but has long since forked into a custom situation.
It uses an index.php to require various PHP & JavaScript files with libraries including functions for email, connection to the db etc and a config.php page with a bunch of variables including MySQL connection info
It uses an "option" parameter to include a separate PHP file that is used to include a PHP template file with the HTML code for the structure of the page, for example:
index.php?option=search&sort=newest
would include the search.php file that includes the templates/search.php file and then performs a database operation on the members returning the newest members (the code for the db operations are in the search.php file, not the template file)
but other pages are static eg
index.php?option=tshirt_contests
includes the tshirt_contests.php file that includes the templates/tshirt_contests.php file
I have seen this sort of structure where the templates are named as tpl eg tshirt_contests.tpl.php (yeah, you see this in Drupal too)
I find it a little tedious to add two files for every new page, a top PHP file that just contains an include statement for the template file.
However, sometimes the top PHP file also performs some operations.
It all depends on your structure and what you do with the parameters in the URL...
greg posted this at 17:03 — 9th July 2008.
He has: 1,581 posts
Joined: Nov 2005
pr0gr4mm3r has covered the MYSQL option and how you would go about it.
But it's very difficult to give a true solution as we don't know exactly what data you want to edit in those files, or how you need to do it.
You would usually just open the file you want to change the data in and change it manually, as you do when you create it.
The usual reason you would use an onsite script to edit page contents is if someone wanted to change content who cant or doesn't know how to edit code files themselves.
I wouldn't recommend running your entire site on one PHP file (the index.php) with simply an id for what content to serve within it or even from include files or the DB.
It's perfectly fine to have an index.php, about_us.php, contact_us.php and so on.
The GET info that gets the information from your url (address bar) and allows you to use that info is going to be a tough one to be able to edit if it's all in one file (index.php)
Which is why having different pages would be better (IMO)
So do you have something like an "Edit this page" link on pages?
If yes then you could use something like the following:
(it isnt true PHP, it's just to give you the idea)
on homepage.php/index.php
<a href="edit_page.php?page=1">Edit this page</a>
on about_us.php
<a href="edit_page.php?page=2">Edit this page</a>
on contact_us.php
<a href="edit_page.php?page=3">Edit this page</a>
index.php?page=
$get = $_GET['page']
if $get = 1 (include homepage.php)
else if $get = 2 (include about_us.php)
else if $get = 3 (include contact_us.php)
etc
Then the edit page could also use the same method
edit_page.php?page=
$get= $_GET['page']
if $get = 1 (open homepage.php for editing)
else if $get = 2 (open include about_us.php for editing)
else if $get = 3 (open include contact_us.php for editing)
But I would prefer to use seperate pages altogether.
homepage.php/index.php
about_us.php
contact_us.php
You could even use those pages and then have the textual content in an include file...
/includes/homepage.php
/includes/about_us.php
/includes/contact_us.php
Then in about_us.php just
include /includes/about_us.php
where you want the content to be.So your edit_page.php can still use the edit links and GET info as above, but the pages being served would be their own names.
There are various ways to do this, but it depends on the info you are wanting to edit.
As for serving the content, I do strongly recommend sticking to using different file names for the different things.
This is better for search engines, better for visitors to know where they are and to bookmark etc and MUCH easier to code.
opdavies posted this at 09:02 — 10th July 2008.
He has: 14 posts
Joined: Jul 2008
I would prefer to use seperate pages - index.php, about_us.php, profiles.php etc., but I'm just trying to work out how I'd manage to code it all so that I can do an edit_page.php file and edit the content online.
I've got a fair understanding of HTML and CSS, and I've just worked though a PHP/MySQL training DVD, but as I mentioned above, it only has one index.php file and the value changes depending on which page is being loaded, but I don't think that this would give me the level of functionality that I need.
greg posted this at 11:06 — 11th July 2008.
He has: 1,581 posts
Joined: Nov 2005
The URL page value (page=1) must represent data somewhere, within in the index.php file will be something like
<?php
if ($_GET[page] == 1)
{do stuff for id 1}
elseif ($_GET[page] == 2)
{do stuff for 2}
?>
The "do stuff" will either be the data in that file itself, i.e. echo out the content of the page for that id, or an include file which has each page data stored for the relevant id or the data will be retrieved from a database.
So you want to be taking each "do stuff" for each id and putting them into separate pages.
As you are putting it into separate pages and removing the URL get data (page=), you should consider this as almost starting from scratch, as apposed to you just re-arranging a few bits.
Editing content from within a file can be a tricky task, especially as you are just learning PHP. A HTML, and PHP, file with web page contents in it is arranged in a certain way, with line breaks, paragraphs, divs, arrangement on the page from CSS and so on.
So simply taking all that and allowing editing of it within an online editor will mean you also need to know where to put the divs, line breaks and other CSS etc.
So then if you load all the HTML tags and CSS references that are in the HTML, as well as the text, into an online editor to edit the contents, then you might as well just stick to editing it in your editor you use to make the file in the first place.
There isn't any real advanage if you have to do that.
Usually, editing website page content within an onsite editor is just a block of text somewhere, or perhaps an image. Such as to change "Image of the month", or insert the "Latest news report" etc.
And this is usually done by a programmer for the site owner, the site owner not wanting to open a file in a editor and start playing with the code even just to edit text.
But I can't really advise on the best approach for you unless you describe what data exactly you want to edit on site, and if you can give us a link to it.
Is it just text you will want to edit? Or do you want to edit links/images/buttons/navigation etc?
kazimmerman posted this at 12:16 — 11th July 2008.
He has: 698 posts
Joined: Jul 2005
<?php
if ($_GET[page] == 1)
{do stuff for id 1}
elseif ($_GET[page] == 2)
{do stuff for 2}
?>
This would be the basic setup, but instead of using quite a long list of elseifs, I would suggest you just handle it with a switch() like this:
<?php
switch ($_GET['page']) {
case 1:
{do stuff for id 1}
break;
case 2:
{do stuff for id 2}
break;
case ...:
{do stuff for id ...}
break;
default:
//An unspecified page has been chosen; send to an error page
header("location: error404.php");
break;
}
?>
Kurtis
greg posted this at 19:14 — 11th July 2008.
He has: 1,581 posts
Joined: Nov 2005
Yeah, it's not the way I would do it, it was just an example of what oliverdavies might find in index.php (as I didn't know what the code is)
opdavies posted this at 10:22 — 13th July 2008.
He has: 14 posts
Joined: Jul 2008
The site URL is http://www.ponthirtaekwondo.co.uk/. I've since cleaned up the HTML and CSS a bit whilst I've been looking into the PHP side of things, so I'm not bothered about that.
The majority of the information is going to be text, although there are quite a few images as well, especially on the Profiles pages, and I think it would be a nice touch to allow these to be changed as well.
Also, is there a way to set up different levels of users to the site? Obviously, guests who can browse the site, and Administrators who can edit the main content, but could I add users who can upload and edit their own Profile for example?
decibel.places posted this at 14:14 — 13th July 2008.
He has: 1,494 posts
Joined: Jun 2008
You have two choices:
#1
Import the site into a community portal, I would recommend Drupal (in use here at TWF)
Drupal has membership and profiles, customizable roles/permissions, and much more to offer
Some Drupal modules that help with this:
Import HTML and Migrator some more import modules
#2
Or set it up yourself on the existing site, try searching HotScripts or SourceForge for free script(s) to adapt
greg posted this at 15:50 — 14th July 2008.
He has: 1,581 posts
Joined: Nov 2005
One option is a registration and login script to assign people a specific username, and then you can use the usernames to determine what 'level' they have.
So superusers can edit images, text blah etc (whatever you want them to do) then administrators can do everything etc etc.
You can edit the DB to change the column in the table that hold the username type (superuser/admin etc) or make a control panel that can change the users authority type. That would also just change the field in the DB table that determined their authority type.
Then the script would do something like...
<?php
// do something in an include file in all pages to determine if logged in (check their cookie/session or whatever)
// and if logged in get their authority type
// $logged_in will = yes or no depending on their status
// $user_auth will = one of the auths you have, admin, superuser or whatever
if ($logged_in == "no")
{just show them the viewable contents and no links to editing stuff}
if ($logged_in = "yes" && $user_auth == "admin")
{let them into the file delete option and other admin stuff}
if ($logged_in = "yes" && $user_auth == "super")
{let them do what supers can do}
?>
That's just the basic idea. I cant put exact code as I don't know exactly what and where you allow things to certain users.
Of course, be careful WHO you give access to file uploading. If you run all the required security checks then you should be fine, but allowing someone to edit files that are run on your server should have considerable thought towards it being a potential security risk (even with security checks).
makmakpoli posted this at 09:38 — 30th July 2008.
They have: 23 posts
Joined: Jul 2008
Hey guys I am Marcus.
Can anyone help me edit my blog?
Actually I envy my coworker's blog design.
Will you help me?
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.