计算 Codeigniter 生成的多个表的总行数 -> list_tables()

Count Total Rows from Multiple Tables Which is Generated by Codeigniter -> list_tables()

需要建议来计算我从 Codeigniter 生成的总行数 list_tables

$sqlx = $this->db->list_tables();
foreach ($sqlx as $table) { 
$counteachrow = $this->db->count_all($table);
echo $table; | echo $counteachrow; }

//Result for each table
| Name | Count |
-----------------
| TblA |  25   |
| TblB |  25   |
| TblC |  20   |
----------------
Total = 70 //Expected value

如何从上面给出的代码中得到 Total = 70, 非常感谢

您可以使用此查询:

select SUM(table_rows) as 'Count' from information_schema.tables 
where  table_schema='MyDb' AND TABLE_NAME IN ('table1',...)
$sqlx = $this->db->list_tables();
$html = "<table>
         <tr>
             <th>Table Name</th>
             <th>Rows</th>
         </tr>";

$finalCount = 0;
foreach ($sqlx as $table) {                 
   $counteachrow = $this->db->count_all($table);                    
   $html .= "<tr>
                 <td>".$table."</td>
                 <td>".$counteachrow."</td>
             </tr>";  
   $finalCount += $counteachrow;
}
$html .= "</table><br/>";
echo $html;
echo "Total = ".$finalCount."<br/>";
exit;

您可以通过两种方式完成,首先您必须进行简单的编码,如下所示:

$tables =  $this->db->list_tables();  // given all table from data base
$counted_data = 0;

   foreach ($tables as $key => $value) {
       $val =  $this->db->count_all($value);  // count all data from related table
       $counted_data += $val;  // add to total count
   }

echo $counted_data;  // desire reult you want

希望对你有用。

Hope this Helps num_rows() function

    $result = $this->db->get('TABLE_NAME'); 
    echo $result->num_rows(); // To count result 
    print_r($result->result()); // To get total data