需要在 CodeIgniter 3 中使用 MySQL table 创建图表
Need to create a chart using a MySQL table in CodeIgniter 3
我需要使用以下 table 创建一个动态 table。不管是来自 ChartJS 还是 Google Chart。我需要将 Y 轴的“血型”作为标签,对于 X 轴,我需要为“isAvailable”等于 1 的每种血型取行数。
Table
我试过上面的方法,它获取数据到视图但是需要为每种血型重写代码所以它不是很有效。我想知道有没有更好的方法?
控制器
public function bloodTypesChart()
{
$query = $this->db->query("SELECT COUNT(PacketID) as count,(BloodType) as blood_type FROM packets WHERE (isAvailable) = '1'");
$packetCount = $this->db->count_all_results();
$record = $query->result();
$chartData = [];
foreach($record as $row) {
$chartData['label'][] = 'O+';
$chartData['data'][] = $packetCount;
}
$chartData['chart_data'] = json_encode($chartData);
$this->load->view('insight',$chartData);
}
这可能不是最有效的解决方案,但一定能帮您完成工作。
public function bloodTypesChart()
{
$totalPackets = $this->db->count_all_results();
$chartData = [];
$blood_types = $this->db->query("SELECT (BloodType) as blood_type FROM packets WHERE (isAvailable) = '1' GROUP BY blood_type")
->result_array();
foreach($blood_types as $bt)
{
$record = $this->db->query("SELECT COUNT(PacketID) as count FROM packets WHERE BloodType = '{$bt['blood_type']}'")
->result_array();
foreach($record as $row) {
$chartData['label'][] = $bt['blood_type'];
$chartData['data'][] = $row['count'];
}
}
$chartData['chart_data'] = json_encode($chartData);
$this->load->view('insight',$chartData);
}
推荐:您也可以尝试这样的操作
$record = $this->db->from('packets')
->select('count(PacketID) count, BloodType blood_type')
->group_by('blood_type')
->get()
->result_array();
foreach($record as $row) {
$chartData['label'][] = $row['blood_type'];
$chartData['data'][] = $row['count'];
}
当然,您仍然可以使用 query() 方法并手动编写查询。
我还没有测试过这些中的任何一个,但它应该能让你知道下一步该去哪里。
这是一个更好、更简单的解决方案。避免使用 php 循环获取数据。
public function bloodTypesChart()
{
$query=" SELECT BloodType as blood_type, COUNT(PacketID) as mycount
FROM packets
WHERE isAvailable = 1
GROUP BY blood_type
";
$chartData = [];
$blood_types = $this->db->query($query)->result_array();
foreach($blood_types as $row)
{
$chartData['label'][] = $row['blood_type'];
$chartData['data'][] = $row['mycount'];
}
$chartData['chart_data'] = json_encode($chartData);
$this->load->view('insight',$chartData);
}
我需要使用以下 table 创建一个动态 table。不管是来自 ChartJS 还是 Google Chart。我需要将 Y 轴的“血型”作为标签,对于 X 轴,我需要为“isAvailable”等于 1 的每种血型取行数。
Table
我试过上面的方法,它获取数据到视图但是需要为每种血型重写代码所以它不是很有效。我想知道有没有更好的方法?
控制器
public function bloodTypesChart()
{
$query = $this->db->query("SELECT COUNT(PacketID) as count,(BloodType) as blood_type FROM packets WHERE (isAvailable) = '1'");
$packetCount = $this->db->count_all_results();
$record = $query->result();
$chartData = [];
foreach($record as $row) {
$chartData['label'][] = 'O+';
$chartData['data'][] = $packetCount;
}
$chartData['chart_data'] = json_encode($chartData);
$this->load->view('insight',$chartData);
}
这可能不是最有效的解决方案,但一定能帮您完成工作。
public function bloodTypesChart()
{
$totalPackets = $this->db->count_all_results();
$chartData = [];
$blood_types = $this->db->query("SELECT (BloodType) as blood_type FROM packets WHERE (isAvailable) = '1' GROUP BY blood_type")
->result_array();
foreach($blood_types as $bt)
{
$record = $this->db->query("SELECT COUNT(PacketID) as count FROM packets WHERE BloodType = '{$bt['blood_type']}'")
->result_array();
foreach($record as $row) {
$chartData['label'][] = $bt['blood_type'];
$chartData['data'][] = $row['count'];
}
}
$chartData['chart_data'] = json_encode($chartData);
$this->load->view('insight',$chartData);
}
推荐:您也可以尝试这样的操作
$record = $this->db->from('packets')
->select('count(PacketID) count, BloodType blood_type')
->group_by('blood_type')
->get()
->result_array();
foreach($record as $row) {
$chartData['label'][] = $row['blood_type'];
$chartData['data'][] = $row['count'];
}
当然,您仍然可以使用 query() 方法并手动编写查询。
我还没有测试过这些中的任何一个,但它应该能让你知道下一步该去哪里。
这是一个更好、更简单的解决方案。避免使用 php 循环获取数据。
public function bloodTypesChart()
{
$query=" SELECT BloodType as blood_type, COUNT(PacketID) as mycount
FROM packets
WHERE isAvailable = 1
GROUP BY blood_type
";
$chartData = [];
$blood_types = $this->db->query($query)->result_array();
foreach($blood_types as $row)
{
$chartData['label'][] = $row['blood_type'];
$chartData['data'][] = $row['mycount'];
}
$chartData['chart_data'] = json_encode($chartData);
$this->load->view('insight',$chartData);
}