0

I have a code that separates first from last names, that I am using on a form that could have up to ten names. Right now I have simply replicated the code multiple times, manually updating the number, but figure there has to be a way to loop this function.

The basic code is:

if (!empty($_POST['name2'])) {
    $name2 = ucwords(strtolower($_POST['name2']));
    $parser = new HumanNameParser_Parser($name2);
    $fname2 = $parser->getFirst();
    $lname2 = $parser->getLast();
}

As the fname and lname variables are used elsewhere I need to stick with the naming.

I was thinking to use a For:

for ($i=2; $i <= 6; $i++)
{
    if (!empty($_POST['name($i)'])) {
        $name($i) = ucwords(strtolower($_POST['name($i)']));
        $parser = new HumanNameParser_Parser($name($i));
        $fname($i) = $parser->getFirst();
        $lname($i) = $parser->getLast();
    }
    else
    {
        break;
    }
}

But I am having a tough time figuring what I should actually use for the ($i) in my example. I tried .$i and [$i] but keep getting errors. And not sure if the way I should code it in the $_POST would be different than the variable $name.

4 Answers 4

1

for the $_POST variable you can use $_POST['name' . $i] and for the name variable you can just replace it with ${'name' . $i};

so it will be like this

for ($i=2; $i <= 6; $i++)
{
if (!empty($_POST['name' . $i])) {
    ${'name' . $i} = ucwords(strtolower($_POST['name' . $i]));
    $parser = new HumanNameParser_Parser(${'name' . $i});
    ${'fname' . $i} = $parser->getFirst();
    ${'lname' . $i} = $parser->getLast();
  }  
else 
  { 
    break;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Could you try this?:

for ($i=2; $i <= 6; $i++)
{
if (!empty($_POST["name$i"])) {
    $name[$i] = ucwords(strtolower($_POST["name$i"]));
    $parser = new HumanNameParser_Parser($name[$i]);
    $fname[$i] = $parser->getFirst();
    $lname[$i] = $parser->getLast();
  }  
else 
  { 
    break;
  }
}

1 Comment

Thanks. For some reason the nameparser threw fatal errors using this way to define variables.
0

You can use $_POST['name'.$i] to get that index. I would suggest something like this:

$people = array();
for( $i=2; $i<=6; $i++) {
    if( empty($_POST['name'.$i])) continue;
    $name = ucwords(strtolower($_POST['name'.$i]));
    $parser = new HumanNameParser_Parser($name);
    $people[$i] = array("first"=>$parser->getFirst(),"last"=>$parser->getLast());
}

This will result in a $people array, which is more usable than variable variables.

Comments

0

Something like this?

$name = array();
$fname = array();
$lname = array();
for ($i=2; $i <= 6; $i++)
{
    if (!empty($_POST['name_'.$i])) {
        $name[$i] = ucwords(strtolower($_POST['name_'.$i]));
        $parser = new HumanNameParser_Parser($name[$i]);
        $fname[$i] = $parser->getFirst();
        $lname[$i] = $parser->getLast();
    }  
    else 
    { 
        break;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.