You are not logged in.

#1 2010-07-14 10:46:21

Zariel
Member
Registered: 2008-10-07
Posts: 446

PHP Web Dev

Hey, so I want to do some web dev in php. I know PHP pretty well along with sql and have a good understanding of html/javascript. But what im struggling with is putting it all together. I dont like reusing code where I can help it, for example the layout of the website is the same across pages but the content is different, how would be the best way to just have a design.php included and set the content up from either within in JS or php.

Im just looking for simplicity and clean code so its easily manageable. Any links will be greatly appreciated along with some pointers.

Offline

#2 2010-07-14 10:52:38

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,839

Re: PHP Web Dev

Not really an Arch question - you may get some answers here, but there must be resources out there that are more specific to your question.

Offline

#3 2010-07-14 11:01:59

rusty99
Member
Registered: 2009-03-18
Posts: 253

Re: PHP Web Dev

For design.php look into <?php include(""); ?> and/or <? php require(""); ?> for templating across pages.

Offline

#4 2010-07-14 11:07:13

ablepharus
Member
From: Berlin
Registered: 2010-05-23
Posts: 129

Re: PHP Web Dev

That is one reason why i switched to tntnet it's really smart. You mostly have your design.ecpp where you define how it looks and where it has to place which content (title, real content etc.) . Then you have the real pages, you just write the 'content' wink and define title -> a new site to your website. And that all, in html and c++.

Offline

#5 2010-07-14 11:23:17

lswest
Member
From: Munich, Germany
Registered: 2008-06-14
Posts: 456
Website

Re: PHP Web Dev

My solution is to create a .php file with the html layout, and set a variable to each corresponding page (i.e. "index.php?section=EN/About"), and code the content into PHP if statements that dynamically load the text (either via text files and include statements, or hard-coded into each statement with the corresponding HTML tags).  I do it all in one index.php file, but you could easily split it up a bit.  I also swap out my menu css styles like that for IE VS Firefox, so the navigation menu appears right in both browser.

If you're interested, I can share the PHP I use for assigning the variables. *Edit* the only thing I left out now is my styles.php file, if you want to have a look at that for the browser-check code, just ask.

Also, I find Stack Overflow is a great place to ask questions or look for answers.

*EDIT* Actually, here it is (I've removed pretty much all the text, and left only the basic frame of my setup):

<?php
error_reporting (E_ALL ^ E_NOTICE);
$section = htmlentities($_GET['section']);
?>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html"; charset="UTF-8" />
    <title>Title</title>
    <link rel="stylesheet" href="styles.php" type="text/css" />
</head>
<!--written and designed by Lucas Westermann-->
<body>
<div id="Container">

<div id="header">

<div align="center" class="company">Header</div>

</div>

<div id="contents"><?php
if($section == "EN/About"){
echo '<div class="body3">About us.</div>';
}
elseif($section=="DE/About"){
echo '<div class="body3">Über uns.</div>';
}
else{
echo '<div class="center">text</div></div>'; <!-- This is the content that becomes the main page --!>
}
?>
</div>
    <div id="panel"><?php
    if($section == "EN"){ <!-- this is in case I ever add a specific sub-homepage for the language, though I could merge it with the elseif below --!>
    include 'panels/english_menu.php';
    }
    elseif($section == "EN/About"){
    include 'panels/english_menu.php';
    }
    elseif($section=="DE"){
    include 'panels/deutsch_menu.php';
    }
    elseif($section=="DE/About"){
    include 'panels/deutsch_menu.php';
    }
    else{
    include 'panels/main_menu.php';
    }
    ?></div>
</div>
</body>
</html>

Note above: The English and German menus appear on all pages that have EN or DE before the section, and this is done with "or" operators in the if statement, I just removed the rest for clarity's sake.

Basic menu:

<div id="nav-menu">
<ul>
<li><img src="./img/top_menu.gif" border="0"></li>
<li><a href="index.php?section=EN/About">English</a></li>
<li><a href="index.php?section=DE/About">Deutsch</a></li>
</ul>
<img src="./img/bottom_menu.gif" border="0">
</div>

Last edited by lswest (2010-07-14 11:44:21)


Lswest <- the first letter of my username is a lowercase "L".
"...the Linux philosophy is "laugh in the face of danger". Oops. Wrong one. "Do it yourself". That's it." - Linus Torvalds

Offline

#6 2010-07-14 11:44:16

litemotiv
Forum Fellow
Registered: 2008-08-01
Posts: 5,026

Re: PHP Web Dev

http://yoursite.com?page=news

<?php
    $page = $_GET["page"];
?>
<html>
<body>
<?php
    switch ($page)
    {
        case 'news':
            echo 'News';
            break;
        default:
            echo 'Home';
    }
?>
</body>
</html>

edit: too late wink

Last edited by litemotiv (2010-07-14 11:44:48)


ᶘ ᵒᴥᵒᶅ

Offline

#7 2010-07-14 13:28:41

Berticus
Member
Registered: 2008-06-11
Posts: 731

Re: PHP Web Dev

I've decided to move away from php, and more towards python/django and ruby/RoR. But back when I did use php, I separated the backend and the template into two or more php files.

For example, I have a php file for the header and maybe another for the footer, since those are universal and would be shared with all pages. Then a third for the body containing the layout and variables of where the content would be placed. Then I have another php file handling backend stuff such as connecting to database, processing forms, reading/writing cookies, etc. The backend stuff would be split by what content they showed. For example, a backend program for displaying post wouldn't be the same as the same program that's used to send an e-mail to the author. So I'd have one for the main site displaying content, and another which allowed users to send e-mail to me. And an uploading program would be different than either.

You wouldn't be re-using code on the different pages, if done correctly, and it would allow you to re-use specific code for other sites.

Offline

#8 2010-07-15 06:51:50

zenlord
Member
From: Belgium
Registered: 2006-05-24
Posts: 1,223
Website

Re: PHP Web Dev

As a self-taught PHP-amateur, I see that my method of working is not too different from what is apparently considered 'good practice' smile

For the front-end, I have 1 index.php in which I include/require external files for all the the non-main sections: head, header, footer, navbar, sidecontent etc. The main section merely contains a switch/select/case (which is supposedly faster than if/else btw).

For the back-end, I have 1 process.php in which I include/require external files for all the classes and 1 constants.php for all my passwords and, well, constants.

I have used TinyButStrong-templates in my last project, and it enabled me to separate all HTML from all PHP without the steep learning curve that other templating systems have. (and it has the added bonus that there is an interface to generate Openoffice documents)

The only thing left is the folder structure: I separate public from secure, so that public contains nothing but content that is to be served to the public.

Zl.

Offline

#9 2010-07-15 08:16:03

lswest
Member
From: Munich, Germany
Registered: 2008-06-14
Posts: 456
Website

Re: PHP Web Dev

@zenlord

If-else may be faster, but I can't see it making a noticeable change.  May be more so when you include large batches of files (I do mainly just echo the content, and each page update takes less than a second).  Next time I make a site (or update this one) I probably will go with a switch statement instead.

You're definitely more organized than I am tongue


Lswest <- the first letter of my username is a lowercase "L".
"...the Linux philosophy is "laugh in the face of danger". Oops. Wrong one. "Do it yourself". That's it." - Linus Torvalds

Offline

#10 2010-07-15 09:45:40

zenlord
Member
From: Belgium
Registered: 2006-05-24
Posts: 1,223
Website

Re: PHP Web Dev

lswest wrote:

You're definitely more organized than I am tongue

I would seriously doubt that - I start of pretty organised, but it always gets chaotic in the end... smile - Please bear in mind that I only recently started using classes... (and I'm still not that good at it) roll

A switch/case is not always possible, f.e. when the cases are variable. Whenever it is possible, I do use it. If not faster, the code is definitely more clean.

Offline

#11 2010-07-15 09:53:59

lswest
Member
From: Munich, Germany
Registered: 2008-06-14
Posts: 456
Website

Re: PHP Web Dev

Yeah, I agree that it looks cleaner, I've just got a habit of using If/else statements in any program I write.


Lswest <- the first letter of my username is a lowercase "L".
"...the Linux philosophy is "laugh in the face of danger". Oops. Wrong one. "Do it yourself". That's it." - Linus Torvalds

Offline

#12 2010-07-15 13:40:17

mrunion
Member
From: Jonesborough, TN
Registered: 2007-01-26
Posts: 1,938
Website

Re: PHP Web Dev

We use Smarty Templates. It's no extra software to install on the server -- just more PHP stuff. But it makes life much easier!


Matt

"It is very difficult to educate the educated."

Offline

Board footer

Powered by FluxBB