0

I had something very different and was having issues with an array_map code I had written. I asked for help and someone was nice enough to suggest he code below. However it seems to have a couple of errors and since I didn't write it and am not as familiar with array_walk I could use some help getting it to work.

The issue - instead of providing me the actual counts of each object in the DB, it is counting the types instead. As an example, if Type_2 has a qty of 3, it is displaying as 1 since there is only one column with Type_2.

$bl_type = $wpdb->get_results("SELECT Type FROM mytablenamehere");

$bl_type = [
  (object)['Type' => 'Type_0'],
  (object)['Type' => 'Type_1'],
  (object)['Type' => 'Type_1'],
  (object)['Type' => 'Type_1'],
  (object)['Type' => 'Type_2'],
];

$types = [
  (object)['Type' => 'Type_1'],
  (object)['Type' => 'Type_2'],
  (object)['Type' => 'Type_3'],
  (object)['Type' => 'Type_4'],
];

$counts = array_count_values(array_column($bl_type, 'Type'));
array_walk($types, function($item) use ($counts) {
    $item->Frequency = $counts[$item->Type] ?? 0;
});

echo '<table id="count"><tr>';
foreach($types as $item) {
    echo '<th>', $item->Type, '</th>';
}
echo '</tr><tr>';
foreach($types as $item) {
    echo '<td>', $item->Frequency, '</td>';
}
echo '</tr></table>';

An example output of the array:

array(4) { [0]=> object(stdClass)#2657 (1) { ["Type"]=> string(8) "Type_0" } [1]=> object(stdClass)#2658 (1) { ["Type"]=> string(7) "Type_1" } [2]=> object(stdClass)#2659 (1) { ["Type"]=> string(7) "Type_1" } [3]=> object(stdClass)#2660 (1) { ["Type"]=> string(7) "Type_1" } [4]=> object(stdClass)#2661 (1) { ["Type"]=> string(8) "Type_2" }

13
  • The error is "unexpected?" can you give the full error. Also with the sample data, what would you expect the output to look like. Commented Jan 23, 2021 at 18:38
  • clarified . It is telling me the ? (question mark) is unexpcted Commented Jan 23, 2021 at 18:38
  • That suggests you're on an ancient version of PHP that doesn't have null-coalescing operator. Commented Jan 23, 2021 at 18:41
  • using PHP v 7. The error is coming out of DW since I cant see errors properly on the site so it might not be an issue in the wild but the counting problem certainly is not working on the site itself Commented Jan 23, 2021 at 18:42
  • And what's your question about this code? "A couple of errors" occur, so could you share them? Commented Jan 23, 2021 at 18:46

1 Answer 1

1

With the code shown, you are not using the data from your database at all:

$bl_type = $wpdb->get_results("SELECT Type FROM mytablenamehere");

$bl_type = [ // this assignment here is the problem
  (object)['Type' => 'Type_0'],
  (object)['Type' => 'Type_1'],
  (object)['Type' => 'Type_1'],
  (object)['Type' => 'Type_1'],
  (object)['Type' => 'Type_2'],
];

The second assignment $bl_type = [...] overwrites the contents of the variable $bl_type which was holding your database query results before. Therefore you will always only get the same results no matter what your database has stored.

Thus, just remove the second assignment and only keep this line:

$bl_type = $wpdb->get_results("SELECT Type FROM mytablenamehere");
Sign up to request clarification or add additional context in comments.

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.