Php 创建水平条形图
我想用php、mysql、javascript、css、html和“wamp服务器”、编辑器:“Macromedia Dreamweaver”、浏览器“Mozilla Firefox”在网页上制作一个水平条形图Php 创建水平条形图,php,mysql,Php,Mysql,我想用php、mysql、javascript、css、html和“wamp服务器”、编辑器:“Macromedia Dreamweaver”、浏览器“Mozilla Firefox”在网页上制作一个水平条形图 我想从表中读取数据(semister结果),并通过条形图显示数据,如, 数据库名称exam 表名'sem\u result'包含以下列>regno[主键]、uid、regu日期、考试日期、sem、结果 php代码是:: <?php // Connect to serve
我想从表中读取数据(semister结果),并通过条形图显示数据,如,
- 数据库名称
exam
- 表名
包含以下列>'sem\u result'
regno[主键]、uid、regu日期、考试日期、sem、结果代码>
php代码是::
<?php
// Connect to server
$cn=mysql_connect("localhost", "root", "");
//Connect to Database
mysql_select_db("exam") or die(mysql_error());
//sql query
$sql="SELECT result FROM sem_result WHERE uid=11111";
//collect results
$result=mysql_query($sql,$cn);
//read data if found
$counter=0;
while($rd=mysql_fetch_array($result))
{
$sem_result[$counter]=$rd[0]; //save sem results into array
$counter=$counter+1;
}
//display
echo "<table>
<tr>
<td>sem1</td>
<td width='100px'>
<img src='img/menu_back.png' width='".$sem_result[0]."%' height='15px'/>
</td>
<td>".$sem_result[0]."%</td>
</tr>
<tr>
<td>sem2</td>
<td width='100px'>
<img src='img/menu_back.png' width='".$sem_result[1]."%' height='15px'/>
</td>
<td>".$sem_result[1]."</td>
</tr>
</table>";
//close database
mysql_close($cn);
?>
正如@hakre已经指出的,你不能得到比单个像素或百分比更精确的结果,即你不能得到78.5px或78.5%。。。但是,您仍然有一些没有javascript的选项。首先,我不知道为什么要在绿色栏中使用图像,为什么不使用纯html/css呢?不管怎样,我的建议如下: 获取条形图的总宽度,从0到100,以像素为单位。只要看一下你贴的图片,我就称之为325px:
$total_width = '325';
然后,将每个条的宽度设置为:
$bar_width = round($total_width * ($sem_result[0] / 100));
这样,78.10%的存储值最终将为254px,78.95%将为257px。这仍然不是确切的百分比,但更接近。图形的总宽度越长,计算就越精确。更改表名、列名和progress.png图像。 在那之后使用它 `
部门
渗漏数目
“height=“20px”>%
“height=“20px”>%
全部的
“height=“20px”>
`您所描述的是正确的行为,如在HTML中,浏览器向下舍入到像素(地板)。您可能希望以另一种方式进行舍入。如果是这样,请在将百分比值插入HTML之前对其进行舍入。-请记住,您只能舍入到一个完整像素,也就是说,只有100个步骤。而不是像您用两个十进制数字表示的10000个步骤。您可以使用
highchart
。这是最好的图表之一API。单击教程的链接。好的,但是如何暗示这一点。有没有其他方法来创建此条形图。没有jquery可以吗?
<?php $s="select count(*) as tnum from leakage";
$r=mysql_query($s) or die (mysql_error());
while($rw=mysql_fetch_array($r))
{
echo $tno=$rw['tnum'];
}
?>
<table class="table table-hover">
<thead>
<tr>
<th>DEPARTMENT</th>
<th>No.Of Leakage</th>
</tr>
</thead>
<?php
$sql1="select dept,count(dept) as total from leakage where dept='winding'";
$result1=mysql_query($sql1) or die(mysql_error());
while($row=mysql_fetch_array($result1))
{
$dept=$row['dept'];
$tot=$row['total'];
}
?>
<tr>
<td><?php echo $dept; ?></td>
<td width="100%"><img src="assets/images/progress.png" width="<?php echo (($tot/$tno)*100);?>" height="20px"><?php echo $tot;?>%</td>
</tr>
<?php
$sql2="select dept,count(dept) as total from leakage where dept='spining'";
$result2=mysql_query($sql2) or die(mysql_error());
while($row=mysql_fetch_array($result2))
{
$dept=$row['dept'];
$tot=$row['total'];
}
?>
<tr>
<td><?php echo $dept; ?></td>
<td width="100%"><img src="assets/images/progress.png" width="<?php echo (($tot/$tno)*100);?>" height="20px"><?php echo $tot;?>%</td>
</tr>
<?php
$sql5="select count(dept) as total from leakage";
$result5=mysql_query($sql5) or die(mysql_error());
while($row=mysql_fetch_array($result5))
{
$tot=$row['total'];
}
?>
<tr>
<td>Total</td>
<td width="100%"><img src="assets/images/progress.png" width="<?php echo $tot;?>" height="20px"><?php echo $tot; ?></td>
</tr>
</table>