473,569 Members | 2,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Best" coding practice?

Wm
As an amateur wannabe-pro programmer, I am trying to learn not only how to
use PHP but how to do it *efficiently*. (Trust me, you don't wanna see some
of my stuff!!!) I'm noticing a number of my pages have a mixture of HTML
and PHP, very interspersed. Example: A form with 20 fields where I echo a
variable in each field to show existing data. My question is really about
the best practice to do this, as I have seen some interesting use of
includes/etc.

Is it "better" to replicate my form in HTML and insert <?PHP echo $variable;
?> in each tag?
Or would it be better to somehow put all my PHP up front and echo the HTML
at one time?

BTW, I have this same debate on a couple of pages where I do mySQL queries
2-3 times on the same page... Advice, suggestions and recommendations are
greatly appreciated!

Thanx,
Wm


Jul 17 '05 #1
9 4502
"Wm" <LA*******@hotm ail.com> schreef in bericht
news:Ea******** **************@ news.easynews.c om...
As an amateur wannabe-pro programmer, I am trying to learn not only how to
use PHP but how to do it *efficiently*. (Trust me, you don't wanna see some of my stuff!!!) I'm noticing a number of my pages have a mixture of HTML
and PHP, very interspersed. Example: A form with 20 fields where I echo a
variable in each field to show existing data. My question is really about
the best practice to do this, as I have seen some interesting use of
includes/etc.

Is it "better" to replicate my form in HTML and insert <?PHP echo $variable; ?> in each tag?
Or would it be better to somehow put all my PHP up front and echo the HTML
at one time?
Personally i prefer to have one big block of php code, and echoes to print
HTML.
BTW, I have this same debate on a couple of pages where I do mySQL queries
2-3 times on the same page... Advice, suggestions and recommendations are
greatly appreciated!


I seperated my database query stuff from the rest of the code by putting
that code in classes and in seperate php files. I then include_once those
files. The major reason why i decided to work like this, is that when i
decide to change some things in the structure of my database, most of my
code won't have to change. Only those classes using that particular bit of
database.

Floris

Jul 17 '05 #2
You should really look into templating.. There's a nice templating class
called phpLibs; I personally use the phpBB forums template class, works
incredibly well and it's very easy to use.. Keeping your code and html
separate makes it much easier to read and modify..

Regards,
Tim
"Wm" <LA*******@hotm ail.com> wrote in message
news:Ea******** **************@ news.easynews.c om...
As an amateur wannabe-pro programmer, I am trying to learn not only how to
use PHP but how to do it *efficiently*. (Trust me, you don't wanna see some of my stuff!!!) I'm noticing a number of my pages have a mixture of HTML
and PHP, very interspersed. Example: A form with 20 fields where I echo a
variable in each field to show existing data. My question is really about
the best practice to do this, as I have seen some interesting use of
includes/etc.

Is it "better" to replicate my form in HTML and insert <?PHP echo $variable; ?> in each tag?
Or would it be better to somehow put all my PHP up front and echo the HTML
at one time?

BTW, I have this same debate on a couple of pages where I do mySQL queries
2-3 times on the same page... Advice, suggestions and recommendations are
greatly appreciated!

Thanx,
Wm

Jul 17 '05 #3
The goal is not to seperate HTML and PHP, it's to seperate display logic
from 'buisness logic'. Using a loop inside your HTML is not a bad thing,
as long as you don't fetch your data in there.

Adding a templating engine above PHP is not always necessary. PHP can be
used as a templating engine itself if you don't use every feature of the
language, and it can be quite simple, even for a non-programmer.

timmeh wrote:
You should really look into templating.. There's a nice templating class
called phpLibs; I personally use the phpBB forums template class, works
incredibly well and it's very easy to use.. Keeping your code and html
separate makes it much easier to read and modify..

Regards,
Tim
"Wm" <LA*******@hotm ail.com> wrote in message
news:Ea******** **************@ news.easynews.c om...
As an amateur wannabe-pro programmer, I am trying to learn not only how to
use PHP but how to do it *efficiently*. (Trust me, you don't wanna see


some
of my stuff!!!) I'm noticing a number of my pages have a mixture of HTML
and PHP, very interspersed. Example: A form with 20 fields where I echo a
variable in each field to show existing data. My question is really about
the best practice to do this, as I have seen some interesting use of
includes/etc.

Is it "better" to replicate my form in HTML and insert <?PHP echo


$variable;
?> in each tag?
Or would it be better to somehow put all my PHP up front and echo the HTML
at one time?

BTW, I have this same debate on a couple of pages where I do mySQL queries
2-3 times on the same page... Advice, suggestions and recommendations are
greatly appreciated!

Thanx,
Wm



Jul 17 '05 #4
"Wm" <LA*******@hotm ail.com> wrote in message
news:Ea******** **************@ news.easynews.c om...
As an amateur wannabe-pro programmer, I am trying to learn not only how to
use PHP but how to do it *efficiently*. (Trust me, you don't wanna see some of my stuff!!!) I'm noticing a number of my pages have a mixture of HTML
and PHP, very interspersed. Example: A form with 20 fields where I echo a
variable in each field to show existing data. My question is really about
the best practice to do this, as I have seen some interesting use of
includes/etc.


Hi,

take a look at TemplateTamer, it is IDE and template engine, that let's you
keep html and php in completely separate files. On related wiki you will
find several examples.

rush
--
http://www.templatetamer.com/

Jul 17 '05 #5
"Louis-Philippe Huberdeau" <lp*********@sy mpatico.ca> wrote in message
news:Vm******** ***********@new s20.bellglobal. com...
The goal is not to seperate HTML and PHP, it's to seperate display logic
from 'buisness logic'. Using a loop inside your HTML is not a bad thing,
as long as you don't fetch your data in there.


Well, there are at least 2 way to look at that problem.

My prefered approach is to use template engine to separate html and php, and
MVC to separate presentation logic and bussines logic. So that are 2
separate problems, and stuffing too much into template in order to handle
whole presentation layer makes it fat (or dirty if you will), for my taste.
I prefer template to remain just an resource with markers, no form of
controls structures in it, and control structures should be in separate php
file.

But I guess it is also a matter of personal preferences, and one should use
approah that fits him the best.

rush
--
http://www.templatetamer.com/


Jul 17 '05 #6
I always use herdoc string output embedded into PHP, like this

<?php

.... code
.... code
.... code

print <<<BLOCKNAME

<html><head><ti tle>
<img src="thisone">
<img src="{$fromthed atabase}">

Hello, {$name}, this is my lovely website.

BLOCKNAME;

You get all the advantages of double qoutes, but none of the hassle of
escaping quotes in HTML. Its the best form of string output when
creating large areas of HTML to be sent.

Jul 17 '05 #7
On Fri, 24 Oct 2003 09:30:29 +0100, Dave Bell <dr**@kent.ac.u k> wrote:
I always use herdoc string output embedded into PHP, like this


Wow - never seen that before.
Is it like printqq in Perl?
--
Games, Gizmos, Gifts and Toys from 'I want one of those'
<http://www.bizorg.co.u k/shopping/>
Jul 17 '05 #8
EXactly ! Thats my favourite feture too.

sanjay

"Dave Bell" <dr**@kent.ac.u k> wrote in message
news:bn******** **@athena.ukc.a c.uk...
| I always use herdoc string output embedded into PHP, like this
|
| <?php
|
| ... code
| ... code
| ... code
|
| print <<<BLOCKNAME
|
| <html><head><ti tle>
| <img src="thisone">
| <img src="{$fromthed atabase}">
|
| Hello, {$name}, this is my lovely website.
|
| BLOCKNAME;
|
| You get all the advantages of double qoutes, but none of the hassle of
| escaping quotes in HTML. Its the best form of string output when
| creating large areas of HTML to be sent.
|
Jul 17 '05 #9
Wm wrote:
As an amateur wannabe-pro programmer, I am trying to learn not only
how to
use PHP but how to do it *efficiently*. (Trust me, you don't wanna
see some
of my stuff!!!) I'm noticing a number of my pages have a mixture of
HTML and PHP, very interspersed. Example: A form with 20 fields where
I echo a
variable in each field to show existing data. My question is really
about the best practice to do this, as I have seen some interesting
use of includes/etc.

Is it "better" to replicate my form in HTML and insert <?PHP echo
$variable; ?> in each tag?
Or would it be better to somehow put all my PHP up front and echo the
HTML at one time?

BTW, I have this same debate on a couple of pages where I do mySQL
queries 2-3 times on the same page... Advice, suggestions and
recommendations are greatly appreciated!

Thanx,
Wm

I don't mix php and html. Whenn generating an html form, I load a
single variable with all the html code. Then print $var. All the
database stuff is done with functions.

--
11:15am up 12 days, 10:31, 1 user, load average: 1.00, 1.01, 1.04
103 processes: 101 sleeping, 2 running, 0 zombie, 0 stopped
CPU states: 2.0% user, 0.7% system, 1.0% nice, 0.2% idle
To email me, change .com to .ca Linux Counter Registration #126647

Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2457
by: Aaron Davies | last post by:
I'm developing a collaborative whiteboard, in which all objects (shapes, clip art icons, etc.) are synchronized between all participants in a session. It's working well, but I'm running into a problem: if two people try to drag the same object at the same time, nothing prevents them from doing so, and whichever one them lets go first will...
2
2185
by: Svein Erik Storkaas | last post by:
I am about to add security to a web project for the first time, and i'm wondering what the easiest, yet a good way to do this? The page is just for personal use, so it does not need to be "ultra" secure. Is it ok just to store 'usrName' and 'psWrd' in an Access db, and manage it from there? If so, how do i control if the user really IS...
5
2848
by: Achim Domma | last post by:
Hi, I have to convert a string to its "best possible" ascii representation. It's clear to me that this is not possible or sense full for all unicode characters. But for most European characters it should be possible. For example: "Müller" should become "Muller" and "é" should become "e".
8
7158
by: elias.farah | last post by:
Hello Everyone, I'm having some very weird behavior on a couple of Access forms. (Not all forms, just some of them). The forms have been working for years, under Access XP/2003 etc, and last week upgraded from Windows XP/Office 2003 to Vista x64/Office 2007. Under Access 2007, a couple of forms are now taking 60 seconds to
9
6240
by: =?Utf-8?B?QW1tZXI=?= | last post by:
I've read many incomplete opinions about the "Best Practice" for securely accessing SQL but what I really need to find the "Best Practice" that fits my applications needs. Currently (alpha stage) I am Using a .Net DSN-Less SQLConnection method in my client program (vb.net) and sending uid/pwd across the network. The client only calls upon...
2
7638
by: hotflash | last post by:
Hi All, I found the best pure ASP code to upload a file to either server and/or MS Access Database. It works fine for me however, there is one thing that I don't like and have tried to fix but don't have any luck is to do a form validation. This script requires the files: db-file-to-disk.asp and _upload.asp. There is a DESCRIPTION field in...
2
1431
Haitashi
by: Haitashi | last post by:
I have a database that currently contains different albums. The way I had the database was that there is a "description" field in which I placed all the songs. When I access my site I see in information of a CD and a paragraph that contains all the data I put in the "description" field. I am thinking of adding an "advanced search" feature to the...
1
2007
by: =?ISO-8859-1?Q?Andr=E9?= | last post by:
Hi everyone, I'd be interested in hearing suggestions as to the "best" way to drive a Python program step by step from another application. Details: --------- I have implemented a "Robot" that can be programmed by a user to perform certain actions. (see Reeborg below for a simple javascript-
4
3129
by: lumpybanana247 | last post by:
Now, I understand this could have several answers, but my simple question is: What is the best free C++ compiler for Windows? For a long time, I have been using MinGW (using Bloodshed DevC++) on Windows XP. I find it great. But, assuming that i hadn't put much time into learning about that compiler that i presently use, what would everyone...
0
7628
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8145
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7694
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6317
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5245
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2128
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1236
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.