Forum Moderators: coopster

Message Too Old, No Replies

Why learn PHP?

         

galileo5

1:47 am on Jul 3, 2006 (gmt 0)

10+ Year Member



I'm familiar with HTML, JavaScript, and somewhat familiar with CSS.

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.

santocki

3:47 am on Jul 3, 2006 (gmt 0)

10+ Year Member



Please search for "static and dynamic page" at Google to know the difference between them and search for "PHP or ASP" and you will find plenty of links that explains the difference between the two.

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.

le_gber

8:33 am on Jul 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



galileo5,

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

Animated

10:48 pm on Jul 3, 2006 (gmt 0)

10+ Year Member



in simple term:) if you want anything dynamic on your website then php is the way to go:)

mack

10:53 pm on Jul 3, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



The main benefit of a scripting language such as php, perl or asp is it allows you to do more on the page. HTML is the building block of any webpage, but you can't really do clever things with HTML on it's own. A scripting language allows you to make your pages do smart things.

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.

santocki

12:10 am on Jul 4, 2006 (gmt 0)

10+ Year Member



le_gber's explanation rocks!

I wasn't able to put the information because it would be too much text to put in, but le_gber have put everything simple and clear.

galileo5

4:05 pm on Jul 4, 2006 (gmt 0)

10+ Year Member



Thank you to everyone who contributed to this thread, especially le_gber.

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.

santocki

4:21 pm on Jul 4, 2006 (gmt 0)

10+ Year Member




First of all, you will need the PHP manual at
[php.net...]

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.

santocki

4:32 pm on Jul 4, 2006 (gmt 0)

10+ Year Member



There is a typo, I meant SSI is different from CSS. You can type CSS command within the HTML document, or call (insert) a seperate CSS file, which means ONLY CSS file.

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.

Duskrider

2:55 am on Jul 5, 2006 (gmt 0)

10+ Year Member



Your database is going to be dependent on whatever hosting company you use for your site. Most hosts that have php installed should allow for at least one mySQL database (which is what php is usually coupled with). You may be able to set this up yourself through your control panel, or your host may need to set it up for you.

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.

galileo5

2:30 am on Jul 6, 2006 (gmt 0)

10+ Year Member



santocki and Duskrider (as well as everyone else), thank you for helping me with my project. You are making this easy for me.

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!

santocki

4:02 am on Jul 6, 2006 (gmt 0)

10+ Year Member



Just create another <form> tag for file2.
For example:

<form action="file1.php" method="post">
..
input elements
..
</form>

<form action="file2.php" method="post">
..
input elements
..
</form>

You can create as much as you want.

Duskrider

5:24 am on Jul 6, 2006 (gmt 0)

10+ Year Member



I'm assuming that the information you enter into your webform is simply sent to the next page in the process (file1.php) and NOT saved anywhere, such as a database or file.

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.

le_gber

7:58 am on Jul 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



galileo 5, if I were you I'd invest a weekend to read and try/work on the following book:

'Build Your Own Database Driven Website Using PHP & MySQL' - sitepoint

I found it to be a very, VERY good book for complete beginners (I read it a couple of months ago).

mack

12:40 pm on Jul 6, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Books are great because you get all the stages explained by the same person. The problem arises when you try to learn from various different sources.
Many people have very different ways of doing things. If you learn from one person then you have a good chance of taking it all in.

Mack.

Animated

10:16 pm on Jul 6, 2006 (gmt 0)

10+ Year Member



i am new to PHP, too i ordered some vidoes php training from ebay i think they work easier & faster then books

eelixduppy

12:05 am on Jul 7, 2006 (gmt 0)




i ordered some vidoes php training from ebay i think they work easier & faster then books

But wouldn't it be annoying looking up something in a video? A book has an index and is readily available and portable ;)

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.

Animated

11:27 pm on Jul 7, 2006 (gmt 0)

10+ Year Member



books are portable thats whats great about them but with the DVD training its visual and easier to follow is almost like a class and u look ver the teachers shoulder The books i use is Wileys PHP for Dummies & PHP Bible

misteroriginal

1:33 am on Jul 9, 2006 (gmt 0)

10+ Year Member



I say get an opensource script that fits your needs. You can have your website tonight. Most likely, it will be PHP/MySQL, but not necessarily. Learn whatever serverside language and database over time. It will take time.

galileo5

2:27 am on Jul 9, 2006 (gmt 0)

10+ Year Member



Thanks again to everyone. I did as much as I could on my own.

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.

le_gber

11:11 am on Jul 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



galileo5

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.

galileo5

3:46 pm on Jul 9, 2006 (gmt 0)

10+ Year Member




System: The following message was spliced on to this thread from: http://www.webmasterworld.com/new_web_development/3000135.htm [webmasterworld.com] by mack - 9:09 pm on July 9, 2006 <small>(gmt 0)</small>


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]

le_gber

3:34 pm on Jul 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi galileo5

you have many ways of adding the date to your db:

  • you could use a hidden field in your form called articleDate with a value of <?php date();?>
  • you could have the same in a txt field called articleDate allowing you to modify it
  • you could call the date() function directly in your INSERT command, instead of having:
    $sql="INSERT INTO news (title,articleDate,article) VALUES ('$_POST[title]','$_POST[articleDate]','$_POST[article]')";

    you'd have:

    $sql="INSERT INTO news (title,articleDate,article) VALUES ('$_POST[title]','date()','$_POST[article]')";

    Hope this helps

  • galileo5

    12:48 am on Jul 11, 2006 (gmt 0)

    10+ Year Member



    Still no luck. I went by your third suggestion since that is what I'm most familiar with.

    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.

    jatar_k

    4:37 pm on Jul 11, 2006 (gmt 0)

    WebmasterWorld Administrator 10+ Year Member



    hello galileo5 and Welcome to WebmasterWorld,

    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

    the_nerd

    4:42 pm on Jul 11, 2006 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    santocki,

    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

    phazei

    11:13 pm on Jul 11, 2006 (gmt 0)

    10+ Year Member



    There are some little quirks that are usefull to know.
    There's something called magic_quotes which automatically adds
    the slashes that mysql_real_escape_string() would on some areas. It can be a problem. It either needs to be turned off, or something like this needs to be done:

    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.

    galileo5

    1:56 am on Jul 15, 2006 (gmt 0)

    10+ Year Member



    jatar_k, I would like to give a big thanks to you. The issue with the date was the biggest obstacle I've had yet, and now it works perfectly!

    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!

    santocki

    12:08 am on Jul 16, 2006 (gmt 0)

    10+ Year Member



    answer to nerd's question:

    "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.

    jatar_k

    5:30 pm on Jul 16, 2006 (gmt 0)

    WebmasterWorld Administrator 10+ Year Member



    sorry galileo5, I was a little slow getting back to this

    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