Jump to content

I wonder..


jwilson122

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :/

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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.

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.