Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple code layout question

Status
Not open for further replies.

newphpbie

Programmer
Oct 17, 2003
110
GB
ok, lets say I have a simple user input form which they can enter some information and input it to a table.

Is it better to have the form code before the SQL code or after?? I mean, is it better to have the brains of the code before or after the cosmetic features??

I know the code normally runs in order from top to bottom but I've seen examples where the bulk of the workings code comes before the input form(<--cosmetic feature)....

Does anyone understand what I'm getting at? Are there any tutorials about the layout of good code or any set rules I should abide by??

TIA.

Tony
 
I use functions to separate my code out. Using if/then control structures to control the flow of the code, it make debugging, code re-use and organization much easier

Code:
<? 
if ($_POST['submit']){
   //call the process form function
   [b]process_form();[/b]
}else{
   //call the form function
   [b]show_form();[/b]
}

[b]function show_form()[/b]
{
  //html and php code to create the form
  
}
[b]function process_form()[/b]
{
   //php code to accept the form data
   
   //verfiy the data
        //call the form code again if incorrect data there
   //else
        //continue with the processing
        //update the db
   //send confirmation or redirect to next page
}
?>


Bastien

Any one have a techie job in Toronto, I need to work...being laid off sucks!
 
sql on top, with a if to check if the form var has been set.

page.php:

<?
if($var) { //add it }
?>
<input name=&quot;var&quot;>

jamesp0tter,
jamespotter@netcabo.pt

p.s.: sorry for my (sometimes) bad english :p
 
More in reponse to your question, it does not really matter if the form code is first or the bulk of the processing code comes first. What is important is that you have a control structure to cause the script to run correctlybased on what is required (ie if the submit button is pressed you want the form to be processed and the db updated)

What I showed was my preferred way to handle to multifunction pages like what you described in your question. In structuring the page in this manner, i can even pull out whoel functions and place them into a common file that can be included and called from any page in the site. This way I can re-use code simply.

HTH

Bastien

Any one have a techie job in Toronto, I need to work...being laid off sucks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top