For a WordPress plugin, I am using a simple $wpdb
query like:
global $wpdb;
$table = $wpdb->wp_table_name = $wpdb->prefix . "wp_table_name";
$the_query = $wpdb->get_results(
"SELECT *
FROM $table;
");
It gives array like (using var_dump()
):
array
0 =>
object(stdClass)[342]
public 'id' => string '1' (length=1)
public 'name' => string 'The Name' (length=8)
public 'status' => string '1' (length=1)
1 =>
object(stdClass)[341]
public 'id' => string '2' (length=1)
public 'name' => string 'The Name 2' (length=10)
public 'status' => string '0' (length=1)
On the view list I want to show a simple counter like:
Total: 2; Active: 1; Inactive: 1
For the total, I used a simple count()
:
<?php echo count($the_query); ?>
It's working fine. Now I want to show the other two numbers, but [if possible] WITHOUT ANY LOOP.
I guess using many loops can slow down the page. I searched many, they are suggesting in_array()
or array_key_exists()
. But they seems search for the index, they can't check whether it's ==
1 or 0.