Jump to content

having your functions in its on file


dazzclub

Recommended Posts

As a newbie to php and mysql, i have generally placed all php code inline with my html. I'm on my next project  i think i will have the functions all in a seperate file, such as functions.php

 

How many of you guys do that, do you find this more productive inregards to reusing the same function on serveral pages or even future projects.

 

this is my question, how do you guys approach, say a part where the user has to input some data and then submit it to the database via form?

 

I tend to have the script that performs the insert in the same page only because i find it easier to put a thank you note on the same page after the form is being submitted.

 

kind regards

Dazzclub

Link to comment
Share on other sites

depends.  I tend to keep functions in their own file for the sake of readability, but I will also avoid having one huge library file.  I have different files based on different needs... aka one file for database functions, one for table manipulation, one for user authentication, ect....  I'll also usually have a separate one for the random ad hoc functions on a per site basis that holds the functions that are unique to that site (that I probably won't use else where).  Just make sure you document your code well, so that whomever has to debug it in the future doesn't have to hunt through hundreds of lines of libraries to find a single function.

Link to comment
Share on other sites

At the most abstract level, you'll want to research MVC and there's plenty of topics on this in the Application Design board.

 

Read up on it just a little bit and then, since you're new, hit a middle ground.

 

Create a base db.php:

<?php
class MyDB {
  function connect() {
    // connects to database
  }

  function select( $sql ) {
    // runs a select query
  }

  function insert( $insert ) {
    // runs an insert query
  }

  // etc...
}
?>

 

You should have a config.php that establishes a DB connection, among other things:

<?php
  // Database init
  require_once 'MyDB.php';
  MyDB::connect();
?>

 

Then say your site has users, have DB access functions in users.php:

<?php
class Users {
  function add( $username, $pass, $otherVar ) {
    $insertSql = 'INSERT INTO `users` ...';
    return MyDB::insert( $insertSql );
  }

  function authenticate( $username, $pass ) {
    $select = 'SELECT COUNT(*) AS `n` FROM `users` WHERE ...';
    return MyDB::select( $select );
  }
}
?>

 

You can create an html.php for common XHTML elements you constantly render, or a session.php, etc.  Also, don't mix functionality.  Don't put all of your user-related functions in user.php.  Put the user database ones in user_db.php and the user XHTML ones in user_display.php or something like that.

 

That would be a good beginning step for code organization.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.