dazzclub Posted May 12, 2008 Share Posted May 12, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/105332-having-your-functions-in-its-on-file/ Share on other sites More sharing options...
mbeals Posted May 12, 2008 Share Posted May 12, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/105332-having-your-functions-in-its-on-file/#findComment-539494 Share on other sites More sharing options...
roopurt18 Posted May 12, 2008 Share Posted May 12, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/105332-having-your-functions-in-its-on-file/#findComment-539500 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.