Forum Moderators: coopster
I read some of the threads here about the benefits of PHP over ASP and vice versa. I even visited other sites to understand the benefit of PHP, but I'm just not getting it.
I plan on running a fan site. Would PHP be beneficial for a fan site? From the little that I got from trying to understand PHP, it would be good for business sites for placing product orders, submitting feedback, etc.
What benefit is there in having a dynamic page as opposed to a static one, especially on a fan site in which I plan on posting only news?
If someone can explain it in simple terms (again, I'm new to PHP), I may implement it on my site. If I still don't see the benefit, I'll stick to CSS and JavaScript.
Thanks for any help.
HTML, CSS, Javascript, and Server side script (PHP, ASP, iHTML, etc) are totally different creatures. You should know all 4 of them to create successful websites. Just as an example comparing a woman's human body I would say:
HTML = Skeleton
CSS = Skin
Javascript = Make Up
Server side script = Muscle
So I can't imagine a interactive site built without using HTML, CSS, and Server side script, although you can eliminate the Javascript if you want to.
no worries about getting confused or not seing the benefits of Server-Side Scripting (SSS).
in simple terms PHP is beneficial if you plan on organising data in the same way on many pages.
ie. in your case the news section.
Presumably you will have on page with all the news summary and a different page for each of the news.
Using static pages - if you have 10 news articles - you will end up with 11 pages on your server. Lets call them news_summary.htm (for the summary page) and news_article_1.htm, news_article_2.htm, ..., news_article_10.htm
Using dynamic pages (PHP, ASP, JSP, .NET, ColdFusion ...) you would have 2 pages and one database. Lets call them news_summary.php (for the summary page) and news_article.php.
The way it would work is by having your news summary sending information about which article to display to the news_article.php page the url would look like this: news_article.php?article=1 or news_article.php?article=3
So if many pages will have the same information 'type' (as in news / products / portfolio / directory ...) SSS is the way to go. This would allow you to change the organisation of the data on the page by going through 1 files instead of 10.
--------------------
Another advantage of SSS is the opportunity to use includes or server side includes (SSI).
Most of your sites' pages will have similar 'features': navigation, header and footer.
What SSI allows you to do is split the page into four components (navigation, header, footer and main body) and bring them together as one.
This would enable you to have one file for the navigation, one file for the footer and one for the header and by changing this one file - changes would be implemented throughout your site because your pages are all composed by the 'components' that you updated / changed.
Hope this helps, but if not, please ask some more questions
For example if you want to use a database to store and retrieve information then doing this using simple HTML would not be possible.
Mack.
I've been reading le_gber's reply over and over to try and to understand it before responding to it. It finally clicked, however, a couple questions have arised:
1. Where would I go to learn these commands to implement on my new site? How would I go about establishing this database about which I've been reading? I went to three different sites, but wasn't able to find it; or maybe I did find it, but wasn't smart enough to realize what it was. I don't really foresee the news I'll be posting on my site to be long enough to have a separate summary page, but I'd still like to learn as I could somehow find a way to implement it.
2. As for the SSI, wouldn't CSS accomplish pretty much the same thing? I do enjoy going to only one file to update the entire site rather than going into each file separately. Either way, it wouldn't matter b/c I'd like to be up to speed and learn PHP.
If anyone would be able to provide a link to a site that would provide instructions on these two particular PHP issues, I'd be more than appreciative.
Thank you again.
To learn PHP, go to [w3schools.com...]
You will even find HTML, CSS, etc. to learn from scratch.
SSI is different from Javascript. With SSI (Apache web server must have this option turned on, usually it is) you can insert text files, or even server side scripts from a simple HTML file. But usually the file extension is set to shtml, it all depends how the web server is configured, so you'll have to ask your hosting company.
With SSI, you can insert ANY text file from a HTML document, or even external server side scripts.
But if you use server side scripts like PHP to create web pages, it makes SSI obsolete because you can use PHP's include() command.
The real benefit of php and mySQL is allowing the code to do all the work for you. Instead of needing to create a new .htm page every time to write a new article, you can just have that article loaded into the database (from a web form) and it can then be displayed automatically from the news page as the last news item posted.
The big difference between SSI and CSS is that CSS works only with the display of information... SSI can work with the information itself.
Quick example: Let's say you have a footer on every page of your site that has a basic sitemap on it - listing links to many of your pages. If you want to change the target of one of your links, you have to do it on every page of your site to remain consistant. CSS will allow you to change the way the link looks and how it feels, but only by making the entire footer a SSI will you be able to change the link in one file (the include file) and have it sweep across the entire site in one fell swoop.
All an include does is insert a bit of code from an external file into your page by calling it from a single command:
<?php include "footer.inc.php";?>
In your include file (footer.inc.php in this case), you would put all the code you want to execute... like a footer. Then you put the above include statement at the bottom of every page and the code inside the file will be 'included' in your page just as though it was written there in the first place.
SSI is a piece of cake!
But as I progress, more questions do arise: One of which is how can I enter data (or news, in my case) so that it would display on two different pages simultaneously, and not just one? This is what I have so far:
<form action="file1.php" method="post">
Can I have the data I enter into the form go to file2.php, as well?
The other question I have is how can I have my items scroll down the page as I post new data? What's happening right now is the new newsitem I am posting is simply replacing the previous newsitem.
Thank you all very much!
If that's the case, then your news page will only display the information it is currently being sent through the form... it won't have any 'memory' of what was sent to it previously. Your php page is being created dynamically with the information you're giving it from your form, that's the only info it has. Any previously sent news will be lost and overwritten by the new news item.
Furthermore, anyone else who is looking at that page and hasn't submitted any information through your form won't see anything at all, since they aren't sending the page any information to display.
In order to keep old news items and allow others to see the news you've posted, that news has to be saved somewhere for the page to reference at runtime. This can be a database (as discussed earlier) or even as simple as a text file. This would also take care of displaying news on more than one page since both pages could reference the same information (file or database).
A real simple example:
<?php
/*The first bit of code writes the contents to a file, this is so your news items get saved and can be displayed by the script later. */
//Open the file for reading and writing
$fp = fopen('mynews.txt','w+');
//Check if the file exists.
if(!$fp)
{
die('Couldn't open the data file!');
}
else {
$oldnews = file_get_contents('mynews.txt');
$newnews = 'Date: 07/07/06<br />Put your news item here, exactly as you wish it to be displayed on the webpage. Include any special HTML markup like line breaks and formatting as well. You can populate this variable from the form you already have written too.';
// This appends the new news to the old news and adds some formatting. New news items are put on the top.
$writeme = $newnews . '<br /><hr /><br />' . $oldnews;
//This writes the file ($fp) with the contents($writeme)
fwrite($fp, $writeme);
//Close the file
fclose($fp);
}
?>
<?php
/*This next bit of code does the actual displaying of the data inside of your file. Put this wherever you want the news to display... on any page you want.*/
$news = file_get_contents('mynews.txt');
echo $news;
?>
That should pretty much do it. Note that this code hasn't been tested so it may have a few stray bugs wandering around, but it should at least give you a direction.
You could do the same thing with a database, having each news item be a seperate row with a date, contents, and author columns (just for example). PHP could then pull those contents from the DB everytime the page was loaded and display them.
A file will work for what you want, but if you ever want to do anything more than what this does, a database would be a better option.
i ordered some vidoes php training from ebay i think they work easier & faster then books
The book I learned from is Apache, MySQL, and PHP--Weekend Crash Course by Steven M. Schafer. The book is a little old by now, but I think it still serves its purpose.
Good luck learning, galileo5! I hope to see you around the PHP Forum [webmasterworld.com]; I've learned more there than anywhere else.
So far, I was able to display the news within tables; that took a while, but many hours of reading, reading, and reading, and I finally got it. Took me a while to figure out how to display the news in descending order (so new stuff would go to the top), too.
My questions are now these:
1. How can I get the date to display appropriately? What's happening now is that whenever I submit the form, the current date is displayed even in newsitems submitted in previous days.
For example, let's say the date is July 8. If I submit a newsitem right now, it would correctly display on the resulting page "July 8," but it would also change the dates submitted on July 7 and July 6 to "July 8."
This is the command I'm using:
echo(date("D, j M Y g:i:s"));
Obviously, the problem is that I'm entering this command on the actual news page itself. Where exactly would I enter this code, if it is even the right one?
2. Also, how would I be able to command it so that the news page would display only the most recent 5 newsitems? I would presume the WHERE function would come in handy. Would I be right?
$result = mysql_query("SELECT * FROM news
WHERE id<'====='");
If the command above is correct, what would I enter where I have the equal signs? I am assigning each newsitem an ID number.
I'm probably getting on some of your nerves. I've been going over the PHP manual for a few hours to find the answers to these, but I'm stuck.
Sorry to inconvenience anyone.
Thanks again for your help.
-- Danny.
the command to display only five news item is LIMIT 5 (to be added at the end of the SELECT clause)
the command to display the news in chronological order is ORDER BY articleDate DESC where articleDate is a database column - do not use date as column name in your db becasue it is a reserved word in PHP/MySQL.
therefore your select query would look like this:
$result = mysql_query("SELECT * FROM news ORDER BY articleDate DESC LIMIT 5");
Do you INSERT the date the articles where added in the database? If not the echo date() command you wrote will always only display today's date.
le_gber, thanks for the help yet again. I am now able to limit the amount of items I want displayed on the main page!
I would presume I would enter the date command in the insert_db.php page (the page that reads "success" after you hit submit on the web form)? If so, where exactly would I enter it? This is what I have so far:
$con = mysql_connect("*****","*****","*****");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$sql="INSERT INTO news
(title,articleDate,article)
VALUES
('$_POST[title]','$_POST[articleDate]','$_POST[article]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Success!";
Or would I go into the database itself to enter this command? If so, where would I go? Should the TYPE be "Date"? The default value is 0000-00-00.
Thank you again to everyone!
[edited by: mack at 9:10 pm (utc) on July 9, 2006]
you have many ways of adding the date to your db:
you'd have:
$sql="INSERT INTO news (title,articleDate,article) VALUES ('$_POST[title]','date()','$_POST[article]')";
Hope this helps
When I enter
$sql="INSERT INTO news (title,articleDate,article) VALUES ('$_POST[title]','date()','$_POST[article]')";
The date I'm getting back is still 0000-00-00. And when I try to specify the format of the date with
('$_POST[title]','date('D, j M Y g:i:s')','$_POST[article]')";
this is the error message I get:
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'D, j M Y g:i:s')',
This is how my articleDate column appears in my SQL database:
Field: articleDate
Type: date
Collation:
Attributes:
Null: No
Default: 0000-00-00
Extra:
I didn't think having the date entered automatically would be this difficult!
Thanks again for your patience and help! I truly appreciate it.
-- Danny.
I kinda took this thread to the PHP forum as it has a few things that need to be addressed
here's a good thread from the PHP Library [webmasterworld.com]
Learning PHP - Books, Tutorials and Online Resources [webmasterworld.com]
now onto your query issue
$sql="INSERT INTO news (title,articleDate,article) VALUES ('$_POST[title]','date()','$_POST[article]')";
never, ever do this, when I say this I mean never use data straight from the $_POST array in your queries. You might as well get security pointers now so you don't have serious problems and have to relearn later.
you need to clean any data submitted by anyone before using it. There a few other threads in our library about this
SQL Injection Vulnerability -- What measures Should be Taken Against [webmasterworld.com]
PHP Security [webmasterworld.com]
so, you now have a ton of reading to do ;)
here are some options for your insert
you could use the mysql NOW()
$mytitle = mysql_real_escape_string($_POST[title]);
$myarticle = mysql_real_escape_string($_POST[article]);
$sql="INSERT INTO news (title,articleDate,article) VALUES ('$mytitle',NOW(),'$myarticle')";
though that might not give you the exact date format you are looking for.
you could also build your date first
$mytitle = mysql_real_escape_string($_POST[title]);
$myarticle = mysql_real_escape_string($_POST[article]);
$mydate = date('D, j M Y g:i:s');
$sql="INSERT INTO news (title,articleDate,article) VALUES ('$mytitle','$mydate','$myarticle')";
try those
if (get_magic_quotes_gpc()) {
$_POST['title'] = stripslashes($_POST['title']);
}
$mytitle = mysql_real_escape_string($_POST[title]);
That way if magic quotes is on, the slashes it adds will be stripped before the other function so you don't get double slashes.
Also this makes it a bit easier/quicker:
if (get_magic_quotes_gpc()) {
$_POST = array_map("stripslashes",$_POST);
}
$_POST = array_map("mysql_real_escape_string", $_POST);
That will apply the function mysql_real_escape_string() to all elements of $_POST so you don't need to do each individually, and it will make using $_POST in your INSERT safe if that's always done first.
There is one issue, however, which is that it leaves unwanted slashes after apostrophes.
Now I did attempt to implement the command phazei suggested. I entered it first in the insert_db.php file (which says "success!" after you submit the form) and then in the main page, however, either did nothing, unfortunately.
This site I'm doing is a news site. I will be the sole contributor. How I would like to have it laid out is that the summary of the news would display in the main page. I want to provide a link from a summarized newsitem to the complete article itself. Everyone seems to be adamant about the $_POST command, however, this command doesn't allow for unique links, which would make it impossible for me to link from the summarized news to the complete newsitem.
I went to w3schools before I wanted to ask here. The $_GET command seems to offer a unique link to the complete newsitem itself. Am I wrong? If not, how would I be able to command it in the forms page so that the link provided would call on the ID# of the newsitem (from the database) instead of the text that I enter into the form itself?
For example, I'd like the link to look something like the following:
www.example.com/news_article.php?id=35
instead of the following:
www.example.com/news_article.php?title=THIS_IS_THE_TITLE&article=THIS_IS_THE_ARTICLE
Thank you again to everyone!
"did you invent this metaphor by yourself? I really like it. Could you pls stretch it a bit further and let me know where you put Ajax ;-)
nerd"
Thanks! I never thought somebody would actually admire it. Yes, it just popped up from my head :). I always like to give examples as comparison when explaining something new, so it's easier to be understood.
I would put AJAX as women's facial expression... because you don't have to show the whole body language (page reload) to show interest on sexual desires... tiny expressions with smiles, eyes, mouths, hands etc. will do the job smartly.
what you can do is whenever you pull data out of the database to display it to the user you can use stripslashes [php.net] on the data.
Once the data is pulled you would do something like (using the same variable names we used previously)
echo stripslashes($mytitle);
echo stripslashes($myarticle);
that should remove the slashes that were added
as far as the links go, yes you could garb the id at the same time as you grab the title and the article body and then write that into your link. If you don't have a unique id yet you could add that to your database table using ALTER TABLE [dev.mysql.com] and doing something like
ALTER TABLE tablename ADD COLUMN article_id PRIMARY KEY AUTO_INCREMENT FIRST
then could use links as you showed in your post