1

I have a class:

class user{

    public $login = "userlogin";
    public $pass  = "userpass";

    function __construct() {
        $this->login = $_POST['slogin'];
        $this->pass = $_POST['spass'];
    }
    function display() {
        return $this->login;
    }
}

and I'm writing all its instances to a file :

function dataStore ($obj, $dataFile) {
    $dataTmp .= serialize($obj);
    file_put_contents($dataFile, $dataTmp);
}

if (isset($_POST['submit'])) {

        $newuser = new user();
        $users[ $newuser->login ] = $newuser;       

        dataStore($users, $dataFile);                    
    }

and all this works, now I want to delete some user by giving its name ($class->login), so I wrote:

function deleteUser($name,$array) {
   unset($array[$name]);        
}

if (isset($_POST['submitDelete'])) {

        deleteUser($_POST['sdelete'],$users);
        dataStore($users, $dataFile);    
    }

html :

<div class="box-login" id="tab-login">  
    <form  method="POST" action="" >
        <p> enter username: <input type="text" name="slogin"/></p>
        <p> enter password: <input type="text" name="spass"/></p>
        <p> create new account -> <input type="submit" name="submit" /></p>
     </form>
</div>

<div class="box-deleteUser" id="tab-deleteUser">
    <form  method="POST" action="" >
        <p> <input type="text" name="sdelete" /> <input type="submit" name="submitDelete" value="usuń"/> </p>
     </form>
</div>

but this is not working, why? I can't figure out whats wrong, can you point me somehow?

3
  • 1
    Why do you think it is not working? What error message do you get? Can you show us the content of the $users array? Commented Jun 23, 2016 at 10:41
  • deleteUser() needs to first retrieve $users from the file you've saved it to, then alter this array, then dataStore() it again. Currently you call deleteUser($_POST['sdelete'], $users), but $users is not accessible in this context. Commented Jun 23, 2016 at 10:42
  • no, it is in fact. I just didnt paste this part of code. my fault. Commented Jun 23, 2016 at 11:07

2 Answers 2

1

Looks like you need to return the array from the deleteUser() function, and then use that in your dataStore() function.

edit - plus what Joseph said :)

Sign up to request clarification or add additional context in comments.

Comments

0

so, like Paul Riley said we have to return value, or pass the array by reference to a function (default it is passing by value), to do this, we putting an ampersand(-> '&') before a variable name:

function deleteUser($name,&$array) {   // 
   unset($array[$name]);

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.