Author Topic: Help writing my 1st PHP Class  (Read 1037 times)

0 Members and 1 Guest are viewing this topic.

Offline TomTeesTopic starter

  • Enthusiast
  • Posts: 187
  • Gender: Male
    • View Profile
Re: Help writing my 1st PHP Class
« Reply #15 on: August 20, 2010, 02:27:20 PM »
I guess start at the beginning then,

so you want to make a web page, do you know HTMl, & CSS? to be able to structure a static page?

** Sorry to clutter the forum up with my personal issues!  **   :D

Yes, I used to know HTML and CSS pretty good, but I have forgotten most things.

So I am going to be *good* and try to read a tutorial on creating a Web-Form using XHTML and CSS right now.  (Let's hope I can whip through this before you all leave for the weekend?!)


Quote
(sorry if this question is dumb, but do you have php, mysql on your PC i use a program called easyPhp to act as my localhost)

I have a MacBook.
I am setting up a brand new directory to work in.
I use MAMP and NetBeans for development.
Just got a test index.php file working in NetBeans, so my re-config of directories must have succeeded?!

I will try to throw together a Web-Form in the next hour or two...  (Been busy with job recruiters and interviews since 7:30 a.m. which i a GOOD thing!)

Don't leave, because I need your help!!  :)



TomTees

Helping my wife start a T-shirt business.

Offline Psycho

  • Guru
  • Freak!
  • *
  • Posts: 8,261
    • View Profile
Re: Help writing my 1st PHP Class
« Reply #16 on: August 20, 2010, 03:21:00 PM »
OK, I'm not sure what you have been reading, but it must not have been very good.

My point is that "Go read the manual" never helped anyone do anything of value.

Really? Have you ever read the PHP manual? I learn a ton from it. Even when I think I know everything about a function, if I read the PHP manual for that function (especially the notes) I sometimes learn a lot of useful information.

But, that is besides the point because no one said to "Go read the manual". I suggested you go read a Tutorial. There is a big difference between a manual and a tutorial. A good tutorial will include a realistic problem needing to be solved and then explain how the process in question can be used to solve the problem. I read the tutorials for OOP on this site quite a while back but recall them giving good examples and context of the functionality.

Anyway, mnoving this post to the "Application Design" forum as it seems the most appropriate for this subject. Good luck with getting the hand-holding you need.
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

Online KevinM1

  • Global Moderator
  • Fanatic
  • *
  • Posts: 4,138
  • Gender: Male
    • View Profile
Re: Help writing my 1st PHP Class
« Reply #17 on: August 20, 2010, 03:51:19 PM »
To the OP, I suggest writing your markup with the HTML 5 doctype instead of the XHTML doctype.  It'll save you a line or two of ugly, ugly code.  It also has the benefit of forcing browsers to still render in standards mode, despite being a somewhat exotic doctype at the moment.  If you're not familiar with what I'm talking about, something like:

Code: [Select]
<!doctype html>
<html lang="en-us">
   <head>
      <title>Example</title>
      <!-- CSS link -->
   </head>

   <body>
      <!-- Stuff goes here -->
   </body>

</html>
Don't go to w3schools.  Here's why
Every time a PHP coder uses 'global' a kitten dies.

Offline TomTeesTopic starter

  • Enthusiast
  • Posts: 187
  • Gender: Male
    • View Profile
Re: Help writing my 1st PHP Class
« Reply #18 on: August 20, 2010, 03:56:28 PM »
To the OP, I suggest writing your markup with the HTML 5 doctype instead of the XHTML doctype.  It'll save you a line or two of ugly, ugly code.  It also has the benefit of forcing browsers to still render in standards mode, despite being a somewhat exotic doctype at the moment.  If you're not familiar with what I'm talking about, something like:

Code: [Select]
<!doctype html>
<html lang="en-us">
   <head>
      <title>Example</title>
      <!-- CSS link -->
   </head>

   <body>
      <!-- Stuff goes here -->
   </body>

</html>

How should I handle my file type?

I think I want a .php file, right?

How do I intermingle the HTML and PHP?

To me, it is a PHP file first with the HTML just used to create the form.

Any thoughts?



TomTees

Helping my wife start a T-shirt business.

Online KevinM1

  • Global Moderator
  • Fanatic
  • *
  • Posts: 4,138
  • Gender: Male
    • View Profile
Re: Help writing my 1st PHP Class
« Reply #19 on: August 20, 2010, 04:03:09 PM »
To the OP, I suggest writing your markup with the HTML 5 doctype instead of the XHTML doctype.  It'll save you a line or two of ugly, ugly code.  It also has the benefit of forcing browsers to still render in standards mode, despite being a somewhat exotic doctype at the moment.  If you're not familiar with what I'm talking about, something like:

Code: [Select]
<!doctype html>
<html lang="en-us">
   <head>
      <title>Example</title>
      <!-- CSS link -->
   </head>

   <body>
      <!-- Stuff goes here -->
   </body>

</html>

How should I handle my file type?

I think I want a .php file, right?

How do I intermingle the HTML and PHP?

To me, it is a PHP file first with the HTML just used to create the form.

Any thoughts?

Yeah, it should be a .php file.

Intermixing the two is easy.  Simply use PHP tags (<?php ?>) to denote where you want to do script processing.  Example:

Code: [Select]
<?php echo "I'm appearing even before the HTML is rendered!"?>

<!doctype html>
<html lang="en-us">
   <head>
      <title>Example 2</title>
   </head>

   <body>
      <p>
         I'm written by the HMTL!
      </p>
   </body>

</html>

<?php echo "And I come after everything else!"?>

Where you put the PHP for your form depends on how you want to handle form processing.  Am I right in assuming you want a sticky form?
Don't go to w3schools.  Here's why
Every time a PHP coder uses 'global' a kitten dies.

Offline TomTeesTopic starter

  • Enthusiast
  • Posts: 187
  • Gender: Male
    • View Profile
Re: Help writing my 1st PHP Class
« Reply #20 on: August 20, 2010, 04:16:16 PM »
Yeah, it should be a .php file.

Intermixing the two is easy.  Simply use PHP tags (<?php ?>) to denote where you want to do script processing.  Example:

Code: [Select]
<?php echo "I'm appearing even before the HTML is rendered!"?>

<!doctype html>
<html lang="en-us">
   <head>
      <title>Example 2</title>
   </head>

   <body>
      <p>
         I'm written by the HMTL!
      </p>
   </body>

</html>

<?php echo "And I come after everything else!"?>

But can you do this...

Code: [Select]
<?php>

<!doctype html>
<html lang="en-us">
   <head>
      <title>Example 2</title>
   </head>

   <body>
      <p>
         I'm written by the HMTL!
      </p>
   </body>

</html>

?>
<html>

I mean if it is a PHP file, should everything be a sub-set of the PHP tags?


Quote
Where you put the PHP for your form depends on how you want to handle form processing.  Am I right in assuming you want a sticky form?

Sticky will be nice later on.

CRAP!  I have a really good book that explains a lot of this a long ways away in storage.  (Wish I had grabbed that book last time.)



TomTees

Helping my wife start a T-shirt business.

Online KevinM1

  • Global Moderator
  • Fanatic
  • *
  • Posts: 4,138
  • Gender: Male
    • View Profile
Re: Help writing my 1st PHP Class
« Reply #21 on: August 20, 2010, 04:24:32 PM »
To be honest, I'm not sure what you're asking. 
Don't go to w3schools.  Here's why
Every time a PHP coder uses 'global' a kitten dies.

Offline TomTeesTopic starter

  • Enthusiast
  • Posts: 187
  • Gender: Male
    • View Profile
Re: Help writing my 1st PHP Class
« Reply #22 on: August 20, 2010, 04:33:57 PM »
To be honest, I'm not sure what you're asking.

Sorry.

Okay, I know that you can put PHP inside of HTML.

I am wondering if you can completely wrap HTML inside of you PHP?

It just seems weird to me to have a ".php" file and yet it is PHP inside of HTML.

I dunno.

Am I making any more sense?



TomTees

Helping my wife start a T-shirt business.

Online KevinM1

  • Global Moderator
  • Fanatic
  • *
  • Posts: 4,138
  • Gender: Male
    • View Profile
Re: Help writing my 1st PHP Class
« Reply #23 on: August 20, 2010, 04:50:04 PM »
To be honest, I'm not sure what you're asking.

Sorry.

Okay, I know that you can put PHP inside of HTML.

I am wondering if you can completely wrap HTML inside of you PHP?

It just seems weird to me to have a ".php" file and yet it is PHP inside of HTML.

I dunno.

Am I making any more sense?

I see what you're saying, but you're looking at it the wrong way.  .php files are, well, PHP files.  This means they run on the server.  The PHP tags denote where you want to execute logic.  Code not within those tags is being output to the screen by, essentially, an invisible echo() call.  So, something like:

Code: [Select]
<!doctype html>
<html lang="en-us">
   <head>
      <title>Example 3</title>
   </head>

   <body>
      <?php
         
for($i 0$i 10; ++$i)
         {
            echo 
$i "<br />";
         }
      
?>

   </body>

</html>

Is actually like:
echo '<!doctype html>\n<html lang="en-us">\n\t<head>\n\t\t<title>\n\t\t\tExample 3\n\t\t</title>\n\t</head>\n\n\t<body>';

for(
$i 0$i 10; ++$i)
{
   echo 
'\n\t\t$i<br />';
}

echo 
'\n\t</body>\n\n</html>';


Behind the scenes.

The PHP tags tell the PHP interpreter where you want to break from the default, invisible echo and perform logic.  This means that wrapping HTML in PHP tags wouldn't work, as the interpreter treats anything within the tags as language constructs.  You'd get a ton of errors even trying it.

Also, just because PHP has tags, that doesn't mean that it has anything to do with HTML's Document Object Model (DOM).  These tags aren't elements.  If you view the rendered source code of a PHP file (like, say, this very forum... right-click, then left-click 'View Source'), you wouldn't see any PHP at all.
Don't go to w3schools.  Here's why
Every time a PHP coder uses 'global' a kitten dies.

Offline TomTeesTopic starter

  • Enthusiast
  • Posts: 187
  • Gender: Male
    • View Profile
Re: Help writing my 1st PHP Class
« Reply #24 on: August 20, 2010, 04:54:41 PM »
So I don't need a "PHP Header" like you need to define an HTML block?

And for my simple registration form, I guess most if not all of my file will be HTML?

Is it a sin if you had 100% HTML in a file called MyFile.php?

(I was just taught in the past to make all your files ".php" for consistency...)


TomTees

Helping my wife start a T-shirt business.

Online KevinM1

  • Global Moderator
  • Fanatic
  • *
  • Posts: 4,138
  • Gender: Male
    • View Profile
Re: Help writing my 1st PHP Class
« Reply #25 on: August 20, 2010, 05:13:37 PM »
So I don't need a "PHP Header" like you need to define an HTML block?

And for my simple registration form, I guess most if not all of my file will be HTML?

Is it a sin if you had 100% HTML in a file called MyFile.php?

(I was just taught in the past to make all your files ".php" for consistency...)

Nope, you don't need any kind of starting tag/header/preprocessor call/etc. at the beginning.  There's a small caveat with HTTP header redirects, but you're not even close to needing to worry about that.

For your form file, unless you're going to add PHP to it later, I'd just keep it as a straight HTML file.  Why make the server do more work than it needs to do to render the page?  I wouldn't be worried about file extension consistency.  It's much better to be consistent with actual file names and your directory structure.
Don't go to w3schools.  Here's why
Every time a PHP coder uses 'global' a kitten dies.

Offline Psycho

  • Guru
  • Freak!
  • *
  • Posts: 8,261
    • View Profile
Re: Help writing my 1st PHP Class
« Reply #26 on: August 20, 2010, 05:23:58 PM »
To add to Nightslyr's response, HTML tags are just that - HTML tage which tell the browser how to render the page. The browser has nothing to do with PHP code and doesn't know how to process it. The idea behind PH (and other server-side code) is that the server will process the server-side code and output a complete HTML page to send to the browser.

Here is an example php page (date.php):
Code: [Select]
<html>
<body>
The date today is <?php echo date('F d, Y'); ?>
</body>
</html>

When a user requests the page date.php the page will first be processed by the server (html files normally go strait to the user). The PHP processor will look for any PHP codeblocks and process the code. In this instance, the processor will echo (i.e. print) the current date where indicated. The completed page (consisting of only HTML content) is then sent to the user. The final HTML code would look like this:
Code: [Select]
<html>
<body>
The date today is August 20, 2010
</body>
</html>

The user who views the page would only see that content if they were to "view source" on the displayed page.
« Last Edit: August 20, 2010, 05:25:21 PM by mjdamato »
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

Offline TomTeesTopic starter

  • Enthusiast
  • Posts: 187
  • Gender: Male
    • View Profile
Re: Help writing my 1st PHP Class
« Reply #27 on: August 20, 2010, 05:31:42 PM »
Thanks Nightslyr and mjdamato for your responses.

I think I'm going to unfortunatelt go break down and re-buy the book I have on HTML and CSS in storage.  (I'm a book guy and it looks like I need to sit down and re-read all of this stuff again to start sounding intelligent.  UGH!)

So maybe after I get these super basic questions down better, I can pick up with getting help on tying my web pages to back-end classes that do processing.

To be continued...



TomTees


Helping my wife start a T-shirt business.