jwilson122 Posted January 10, 2011 Share Posted January 10, 2011 I know I am probably posting this in the wrong place, SORRY! lol Not sure where to post this.. but anyways. I am wondering if I am approaching coding php the correct way or not.. About how long would YOU put into making a full register script with address yada yada? Like.. It takes me days, but I'm never happy with the way it looks lol Any suggestions or something? lol Thanks! Quote Link to comment Share on other sites More sharing options...
btherl Posted January 10, 2011 Share Posted January 10, 2011 It would take me between an hour and a day, depending on the exact spec. As for suggestions, start with something simpler and build up from there. Eg start with a script taking one input and displaying it. Then change it so it stores that one input into the database instead. Then have it check for duplicates before storing. Then add a second input item. Then add the rest of the input items. Quote Link to comment Share on other sites More sharing options...
jwilson122 Posted January 10, 2011 Author Share Posted January 10, 2011 It would take me between an hour and a day, depending on the exact spec. As for suggestions, start with something simpler and build up from there. Eg start with a script taking one input and displaying it. Then change it so it stores that one input into the database instead. Then have it check for duplicates before storing. Then add a second input item. Then add the rest of the input items. hmm k Well, I know php fairly well now.. I use a class like $db->q("yada yada"); etc. But, my main struggle is the security in fields. Such as, sanitizing user input :/ I know how to do like.. if ($_POST['username'] == '') or (empty($_POST['username'])) and I'd make sure that username doesn't already exist.. But its like.. all the things together looks messy to me :/ Quote Link to comment Share on other sites More sharing options...
btherl Posted January 10, 2011 Share Posted January 10, 2011 You can make a function, eg "validate_username()". Then put all the messiness inside there. Creating a function (or object, structure, etc etc) to hide the messiness is one of the most effective ways to simplify a program and make it easy to understand and get working properly. Quote Link to comment Share on other sites More sharing options...
jwilson122 Posted January 10, 2011 Author Share Posted January 10, 2011 You can make a function, eg "validate_username()". Then put all the messiness inside there. Creating a function (or object, structure, etc etc) to hide the messiness is one of the most effective ways to simplify a program and make it easy to understand and get working properly. So you mean create a function for everything pretty much? lol like.. validate_username(), validate_email() etc? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted January 10, 2011 Share Posted January 10, 2011 ... or not, leaving all the messiness on one file. Quote Link to comment Share on other sites More sharing options...
btherl Posted January 10, 2011 Share Posted January 10, 2011 Yep, exactly. Then your main code looks a bit like this: if (! validate_username($username)) { # Validation error, do something here } if (! validate_email($email)) { # Validation error, do something here } which is much easier to read than if (empty($email) || !preg_match('|[a-zA-Z0-9]+unreadable*regexp^goes?here|', $email)) { # Validation error } Then down below you have function validate_email($email) { if (empty($email)) return false; if (!preg_match('|messy regexp for email validation, found from google|', $email)) return false; # Email looks valid return true; } Quote Link to comment Share on other sites More sharing options...
jwilson122 Posted January 10, 2011 Author Share Posted January 10, 2011 Yep, exactly. Then your main code looks a bit like this: if (! validate_username($username)) { # Validation error, do something here } if (! validate_email($email)) { # Validation error, do something here } which is much easier to read than if (empty($email) || !preg_match('|[a-zA-Z0-9]+unreadable*regexp^goes?here|', $email)) { # Validation error } Then down below you have function validate_email($email) { if (empty($email)) return false; if (!preg_match('|messy regexp for email validation, found from google|', $email)) return false; # Email looks valid return true; } ah k cool. So question. How would you do errors? I usually just echo then out, create a var like.. $errors = true; under the echo'd message, then do.. if ($errors == false) create user.. yada yada. But I'm sure there are easier better ways.. Quote Link to comment Share on other sites More sharing options...
btherl Posted January 11, 2011 Share Posted January 11, 2011 That's good enough. I always keep seperation between code and display, so I would store the errors in a variable and then display all of them at once, along with all the other output. But it's basically the same idea as what you are doing. Quote Link to comment 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.