Home > Back-end >  Trouble creating a running list of names from user input into an array in PHP
Trouble creating a running list of names from user input into an array in PHP

Time:10-19

I have a simple input field that takes a string "First Name Last Name" and when the user hits the submit button "Add Name", it should display what was just entered by the user in a textarea that is right below it, but also keep the previous entries until a reset button has been pressed.

I am trying to construct a loop of sorts to allow the user to keep entering names until the "Clear Names" button is pressed, but the request never finishes and times out. I feel like something like this is the solution, so is there something I am overlooking, or am I way off track?

function addName:

<?php

class AddNames {
    function addName() {
        $nameField = $_POST['nameField'];
        if (isset($_POST['addName'])) {
            do {
                $arr = explode(" ", $nameField);
                array_push($arr);
                sort($arr);
            }
            while (!isset($_POST['clearNames']));
        }
        return "$nameField\n";
    }
}
?>

code above HTML header in index.php:

    <?php
    $output = "";
    if(count($_POST) > 0) {
        require_once 'AddNames.php';
        $addName = new AddNames();
        $output = $addName->addName();
    }
    
?>

HTML body:

    <body>
    <div >
        <form method="post" action="#">
            <h1>Add Names</h1>
            <input  type="submit" name="addName" value="Add Name">
            <input  type="submit" name="clearNames" value="Clear Names">
            <div >
                <label for="nameField" >Enter Name</label>
                <input type="text"  id="nameField" name="nameField" placeholder="First & Last Name">
            </div>
            <label for="listOfNames">List of Names</label>
            <div >
                <textarea  id="listOfNames" name="listOfNames" style="height: 500px">
                    <?php echo $output; ?>
                </textarea>
            </div>
        </form>
    </div>
</body>

CodePudding user response:

Consider the following changes to your Class function.

function addName($name) {
  if (len($name) > 0) {
    $arr = explode(" ", $name);
    array_push($arr);
    sort($arr);
    $name = implode(" ", $arr);
  }
  return "$name\n";
}

It appears you are expecting a String and not an Array of Strings. This is why you chould not need a do, while loop.

In your code, it's not clear what you do with $arr, yet I suspect you mean to sort it and then combine it back into a String.

CodePudding user response:

I ended up solving my problem. I decided to rename my variables to make it more clear as to what I was doing, and that really helped. This is my solution to the AddNames class

<?php

class AddNames {
function addName() {
    $nameField = $_POST['nameField']; 
    if (isset($_POST['addName'])) {
        $nameFieldArray = explode(" ", $nameField);
        $firstName = $nameFieldArray[0];
        $lastName = $nameFieldArray[1];
        $name = "$lastName, $firstName";
        $textArea = $_POST['listOfNames'];
        $masterArrayOfNames = explode("\n", $textArea);
        array_push($masterArrayOfNames, $name);
        sort($masterArrayOfNames);
        $listOfNames = implode("\n", $masterArrayOfNames);
    }
    if (isset($_POST['clearNames'])) {
        $nameField = "";
        return $textArea = "";
    }
    return "$listOfNames";
   }
}
  • Related