在这个PHP应用程序中实现用户首选项的最佳方法是什么?

时间:2021-07-24 20:09:48

I have a smaller web app that republishes content from one social source to another. Users have, let's say 5 options for how to filter that content. Their prefs are stored in my user DB as 1 or 0 markers.

我有一个较小的Web应用程序,可以将内容从一个社交源重新发布到另一个社交源。用户可以说有5个选项来过滤该内容。它们的首选项作为1或0标记存储在我的用户数据库中。

When I do my major "publish" action hourly, what's the best way to break up my code to implement these preferences? To make it more clear, my current implementation is something like this:

当我每小时进行一次主要的“发布”操作时,分解我的代码以实现这些首选项的最佳方法是什么?为了更清楚,我目前的实现是这样的:

mysql_query(SELECT * FROM users);
foreach ($user as $row){
    get_json_data($userID);
    if ($pref1 == 1){
        /code that enacts preference 1, adds results to array $filtereddata
    }
    if ($pre2 == 1 ){
        /code that filters out preference 2, adds results to $filtereddata
    {   
    postfinalarray($filtereddata);
}

Obviously this is mock code, but that's the general flow I've been using. Is there a better way to implement customizing a function with user's preferences? Should I design the function to accept these preferences as parameters? Will that save processing time or be more maintainable?

显然这是模拟代码,但这是我一直在使用的一般流程。有没有更好的方法来实现用户的偏好定制功能?我应该设计函数来接受这些首选项作为参数吗?这会节省处理时间还是更易于维护?

Sorry if this is too general, please ask questions so I can clarify.

对不起,如果这太笼统了,请提出问题,以便我澄清一下。

2 个解决方案

#1


Well of course there are many possibilities to improve this kind of code. Just imagine, if you would add another preference you would have to rewrite your whole code. I always try to use the object oriented way so creating a user class always makes sense:

当然,有很多可能性来改进这种代码。试想一下,如果要添加其他首选项,则必须重写整个代码。我总是尝试使用面向对象的方式,因此创建用户类总是有意义的:

<?php

class User
{
    private $_data;

    public function __construct($userdata, $preferences)
    {
        $this->_data = $userdata;
    }

    public function getPreferencesData($preferences)
    {
        $result = array();
        $prefs = $this->_data['preferences'];
        foreach($preferences as $key => $value)
        {
            if($value == "1")
                $result[] = $this->_data[$key];
        }
        return $result;
    }
}

$mypreferences = array("name" => 1, "birthdate" => 0); // show e.g. name and birthdate
$mydata = array("name" => "Gaius Julius Caesar", "birthdate" => "July 13th -100");
$testuser = new User($mydata);
print_r($test->getPreferencesData($mypreferences));

?>

Then you can easily adapt to whatefer preferences the user has chosen without having to change your code.

然后,您可以轻松地适应用户选择的whatefer首选项,而无需更改代码。

#2


From the limited info you've provided, your method seems fine.

根据您提供的有限信息,您的方法似乎很好。

I would do something slightly different probably. I would have a table of your 5 prefs (or would just hard code them if they're very unlikely to change). I would create a cross-reference table linking the prefs and users. Then you can do something like:

我可能会做一些略微不同的事情。我会有一个你的5个prefs的表(或者如果它们不太可能改变的话就会硬编码)。我将创建一个链接prefs和用户的交叉引用表。然后你可以这样做:

foreach($users as $user)
{
    $filtereddata = array();
    foreach($user->getPreferences() as $pref)
    {
        $filtereddata += $this->getFilteredData($user, $pref);
    }
}

This is more overhead than what you're using, but more easily extensible if/when you add preferences/features.

这比您正在使用的开销更大,但是如果/当您添加首选项/功能时更容易扩展。

#1


Well of course there are many possibilities to improve this kind of code. Just imagine, if you would add another preference you would have to rewrite your whole code. I always try to use the object oriented way so creating a user class always makes sense:

当然,有很多可能性来改进这种代码。试想一下,如果要添加其他首选项,则必须重写整个代码。我总是尝试使用面向对象的方式,因此创建用户类总是有意义的:

<?php

class User
{
    private $_data;

    public function __construct($userdata, $preferences)
    {
        $this->_data = $userdata;
    }

    public function getPreferencesData($preferences)
    {
        $result = array();
        $prefs = $this->_data['preferences'];
        foreach($preferences as $key => $value)
        {
            if($value == "1")
                $result[] = $this->_data[$key];
        }
        return $result;
    }
}

$mypreferences = array("name" => 1, "birthdate" => 0); // show e.g. name and birthdate
$mydata = array("name" => "Gaius Julius Caesar", "birthdate" => "July 13th -100");
$testuser = new User($mydata);
print_r($test->getPreferencesData($mypreferences));

?>

Then you can easily adapt to whatefer preferences the user has chosen without having to change your code.

然后,您可以轻松地适应用户选择的whatefer首选项,而无需更改代码。

#2


From the limited info you've provided, your method seems fine.

根据您提供的有限信息,您的方法似乎很好。

I would do something slightly different probably. I would have a table of your 5 prefs (or would just hard code them if they're very unlikely to change). I would create a cross-reference table linking the prefs and users. Then you can do something like:

我可能会做一些略微不同的事情。我会有一个你的5个prefs的表(或者如果它们不太可能改变的话就会硬编码)。我将创建一个链接prefs和用户的交叉引用表。然后你可以这样做:

foreach($users as $user)
{
    $filtereddata = array();
    foreach($user->getPreferences() as $pref)
    {
        $filtereddata += $this->getFilteredData($user, $pref);
    }
}

This is more overhead than what you're using, but more easily extensible if/when you add preferences/features.

这比您正在使用的开销更大,但是如果/当您添加首选项/功能时更容易扩展。