0

I have a json object in my php script that was encoded using the json_encode php function.

Here is the object when var_dumped...

string '{"voting_sys":"50","beta_site":"50"}' (length=36)
string '{"voting_sys":"50","beta_site":"50"}' (length=36)

Database structure: Database Structure

My goal is to get the sum of the values in the voting_sys for each user, and in the beta_site...this is going to be used for voting, but on an unknown amount of features/values.

Any ideas? I have tried the following...

$voters = DB::table('votes')->get();
    foreach($voters as $vote){
            $vote_array[$voter->user_id]=json_decode($voter->value, true);
    }
    var_dump($vote_array);

This returns the decoded json object to the array. I would assign the "voting_sys" as the key for the array, and then the integer value to the value of the array, but there will be an unknown number of features. In this example, there are only two features the users can vote on, but there may be more at a later date. I use the feature ID to roll out a new set of features the users can vote on.

I am using Laravel 4.1

[Edit: Result]

    $feature_list = DB::table('features')->where('rev_id', Config::get('app.beta_rev'))->get();
    $feature_array=array();
    foreach ($feature_list as $feature){
        array_push($feature_array, $feature->name);
    }
    foreach($feature_array as $feature){
        $voters = DB::table('votes')
                 ->select(DB::raw('sum(value)'))
                 ->where('feature_name', '=', $feature)
                 ->get();
                 echo $feature.' - ';
                 var_dump($voters);
                 echo '<br />';
    }

which when called, dumps:

voting_sys -
array (size=1)
  0 => 
    object(stdClass)[248]
      public 'sum(value)' => string '149' (length=3)

beta_site -
array (size=1)
  0 => 
    object(stdClass)[249]
      public 'sum(value)' => string '69' (length=2)

Which is exactly correct for the votes I entered. Thanks for the help.

2 Answers 2

1

I would suggest using a slightly different database structure. It is almost never a good idea to serialize / json_encode data in your database. Since you already have a dedicated table for votes it should be simple to change your table from what you curretly have to the following:

id | user_id | feature | value
------------------------------
1  |       2 | sys     |    50
2  |       2 | beta    |    40
3  |       3 | sys     |    50

This would make counting very trivial:

SELECT SUM(value) FROM table WHERE feature = 'sys'
Sign up to request clarification or add additional context in comments.

4 Comments

That shouldn't make a difference. Every Framework/ORM allows for custom querries.
On a side, the features are defined in a seperate database table, so why re-define them here? I use the feature ID within my application config files to roll out new sets of features, and I add the new features in the database, change the config, and users can begin voting on the new features.
I'm not sure what point you're trying to make. Of course my answer is just an idea, not a finished product :) You could add a feature_id col and create a relation to your other database instead of using what I proposed.
I don't even know what point I was trying to make...lol, anyways I will take this into consideration.
1

Use array_sum

array_sum — Calculate the sum of values in an array

$voters = DB::table('votes')->get(); // get the JSON response response 


$jsonDecodedArray = json_decode($voters,true); // decode the JSON

$sum = array_sum($jsonDecodedArray); // Use php array_sum

3 Comments

Wouldn't this just count the number of features? I need to get the sum of the values of the features. I updated the question as that was a bit confusing.
sorry.use array_sum function :)
should that be, json_decode($voters, true); ? to use the array_* functions you need an array not an object. Anyways, with that minor change, it adds the values from each user submission, instead of the amount of votes for each feature...

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.