0

I'll start with explaining what my end goal is as there may be better solutions to it.

I have a function called updateUser which accepts the following parameters:

function updateUser($password = NULL, $name = NULL, $lastname = NULL, $age = NULL, $email = NULL, $relation = NULL)

As you can see I've set them to NULL, so if they aren't passed through the function they become blank. Right? (I hope I got that right)

The problem is that each of those arguments (which are passed) contains info for a user that's going to be sent in to a database, and I only want to include the passed variables in the query as I don't want to set the ones that are not passed to (blank) in the database.

And so I came up with the idea to take all the passed arguments and shove them into an array. And then loop through every item in the array and generate a string like:

$string = "$password, $email";

4 Answers 4

1

I guess it would be more flexible if you just pass one array containing the property/value pairs that should be updated:

function updateUser($props) {
    $names = array('password', 'name', 'lastname', 'age', 'email', 'relation');
    $arr = array();
    for ($names as $name) {
        if (isset($props[$names])) {
            $arr = "$name = '".mysql_real_escape_string($props[$names]).'"';
        }
    }
    if (!empty($arr)) {
        $query = "UPDATE … SET ".implode(',', $arr);
    }
}

Then you call this function with an array with the properties that should be updated:

updateUser(array('name'=>'User A', 'password'=>'secret'))
Sign up to request clarification or add additional context in comments.

Comments

1

Using the function call_user_func() (php.net docs), you can call your database INSERT or UPDATE commands depending on what data is passed to the function.

For example:

function updateUser($password = NULL, $name = NULL, $lastname = NULL,
    $age = NULL, $email = NULL, $relation = NULL)
{
    $array = array();

    // Create an array with indices of passed parameters:
    if($password !== NULL)  // !== to check for NULL and not empty string.
        $array["password"] = $password;
    if($name !== NULL)
        $array["name"] = $name;
    // etc...

    call_user_func("addToDb", $array);
}

function addToDb($detailsArray)
{
    foreach($detailsArray as $detail)
    {
        // Add $detail's key with $detail's value to database.
    }
}

2 Comments

this is wrong usage of call_user_func_array(). call_user_func_array() takes each element in the second parameter (which should be an array) and uses it as a separate parameter, i.e. call_user_func_array('foobar', array('a', 'b', 'c')); would be the equivalent of call_user_func('foobar', 'a', 'b', 'c'); or simply foobar('a', 'b', 'c'); (see call_user_func())
After a bit of struggling with it i finally got it working. When i first looked at it i was like "wtf? How would that possibly work?" but then i read through it and it became a bit more logic. It works great, thanks.
0

Gets all arguments and filter variables away which are not a string.

$arguments = func_get_args();
array_filter($arguments, 'is_string');
//$arguments will be an array with numeric indices

1 Comment

I found a better solution to it, but i'll keep this in mind. I searched the PHP archive for a function like that for an eternity, lol. Thanks for the post!
0

I think the problem is that you might not call the function correctly. When you do it like this:

updateUser('pass', 'user', '', '', '[email protected]');

than the values of $lastname and $age will not be null, but an empty string. But if you call it like this:

updateUser('pass', 'user', null, null, '[email protected]');

they will remain null.

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.