我想转换有关图形统计的本机php代码,该图表填充了每个班级的学生总数,我使用chart.js进行统计,请帮助我将代码转换为CodeIgniter代码专家,谢谢
我曾经尝试将所有文件夹和代码放入控制器,但是什么也没显示。而且我不知道如何将其转换为CI。
这是我的koneksi.php代码:
<?php
$koneksi = mysqli_connect("localhost","root","","grafik_mahasiswa");
?>
这是我的index.php代码:
<!DOCTYPE html>
<html>
<head>
<title>Test and Try</title>
<script type="text/javascript" src="chartjs/Chart.js"></script>
</head>
<body>
<style type="text/css">
body{
font-family: roboto;
}
table{
margin: 0px auto;
}
</style>
<center>
<h2>Try me</h2>
</center>
<?php
include 'koneksi.php';
?>
<div style="width: 800px;margin: 0px auto;">
<canvas id="myChart"></canvas>
</div>
<br/>
<br/>
<table border="1">
<thead>
<tr>
<th>No</th>
<th>Nama Mahasiswa</th>
<th>NIM</th>
<th>Fakultas</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$data = mysqli_query($koneksi,"select * from mahasiswa");
while($d=mysqli_fetch_array($data)){
?>
<tr>
<td><?php echo $no++; ?></td>
<td><?php echo $d['nama']; ?></td>
<td><?php echo $d['nim']; ?></td>
<td><?php echo $d['fakultas']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<script>
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["Teknik", "Fisip", "Ekonomi", "Pertanian"],
datasets: [{
label: '',
data: [
<?php
$jumlah_teknik = mysqli_query($koneksi,"select * from mahasiswa where fakultas='teknik'");
echo mysqli_num_rows($jumlah_teknik);
?>,
<?php
$jumlah_ekonomi = mysqli_query($koneksi,"select * from mahasiswa where fakultas='ekonomi'");
echo mysqli_num_rows($jumlah_ekonomi);
?>,
<?php
$jumlah_fisip = mysqli_query($koneksi,"select * from mahasiswa where fakultas='fisip'");
echo mysqli_num_rows($jumlah_fisip);
?>,
<?php
$jumlah_pertanian = mysqli_query($koneksi,"select * from mahasiswa where fakultas='pertanian'");
echo mysqli_num_rows($jumlah_pertanian);
?>
],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
</script>
</body>
</html>
答案 0 :(得分:1)
请阅读https://www.codeigniter.com/userguide3/database/connecting.html,这可能有助于您开始使用CI。祝你好运!
如果还有其他问题,请尽量具体一些,因为这样可以更容易回答。例如,如何将本机PHP转换为CI的方法非常广泛,而且几行之内很难回答。
答案 1 :(得分:0)
当您安装了codeigniter后,转到config-> database.php并更改现有代码以获取此信息:
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'grafik_mahasiswa';
$db['default']['dbdriver'] = 'mysqli';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
使用所有ur sql和控制器创建模型,以在ur模型和ur视图之间建立链接。
这将为您提供帮助:
https://codeigniter.com/user_guide/tutorial/create_news_items.html