Jump to content

PHP tutorial 02: DCR


Guest xaff
 Share

Recommended Posts

DCR = dynamic content retrival

 

Since I belive that I've covered enough of the basics, I'll try to explain how to use some of your new knowledge on webpages. This comes in handy if you're a systemfreak like I (sometimes) am. Instead of using a the regular website structure, you can simply store the design template in one file and all the content in their own separate file.

 

index.html  ----

photos.html -----

about.html -------

links.html --------##====> /images/

 

That would be my attempt at displaying how a regular site would be structured, using ASCII. Now if you were to use PHP, you could save space and have a more clean system. (It's not a LOT of space unless you have loads of webpages.)

 

index.php ---> /images/ # images used in the layout.

          |

          | # checks to see what content is requested

          | # by client before it retrives the content

   |

   ?-> /include/photos.php -----?-> /photos/image001.png

   ?-> /include/about.php       ?-> /photos/image002.png

   ?-> /include/links.php



/photos

/include

index.php

 

How does it work?

To use the file 'index.php' as a template file and include all other files from it, we first need to learn how to retrive content. The two most usefull functions for plain including is 'require();' and 'include();'. The difference between them is that if you use 'require();', you cannot include documents from the file you are including, if you use 'include();' you can include further files in the included document.

 

Now, to include a file you simply:

<?php



include('path/to/the/file');



?>

 

That's easy. But that doesn't get us far, as it will allways include the same document. So we will have to tell PHP what to do under different circumstanses. To do that we use 'if'.

 

if(variable operator condition){ commands }

 

If checks if the condition of the variable is condition using the operator, and if it is it executes commands.

 

I'll only be using the "==" operator here, the complete list of operators can be found at the php.net website.

 

<?php



$id = $_GET['id']; # Don't understand what's happening here? See PHP tutorial 00



if($id == "photos"){

include('include/photos.php');

}



else if($id == "about"){

include('include/about.php');

}



else{

echo("

<a href="index.php?id=about">About</a><br> # Reminder: if a variables value is retrived using get, you can set it by a url suffix

<a href="index.php?id=photos">Photos</a><br>

<a href="index.php?id=links">Links</a><br>

");

}



?>

 

Whoa, that was quite a jump ahead. See the new fuctions, 'else if' and 'else'? If you need to define more different actions in case of different conditions, you use 'else if'. (The syntax is exactly the same as 'if') And if PHP should do something in case none of the conditions are true, you use 'else'.

 

That's about it for this time, I'll be writing about a little bit more advanced subjects later on, like a few ways to display photos and links.

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

×
×
  • Create New...