如何使用PHP从多个列中选择最小值 [英] How to Select Smallest Value From Multiple Columns with PHP
问题描述
我有一个包含以下内容的表:
I have a table with the following:
6xx 8xx 9xx 11xx 12xx
1 0.01 0.002 0.004 0.001 0.025
2 0.025 0.125 0.002 0.01 0.011
我想从该列中找到最小值",使该列为绿色.
I would like to find the Smallest Value from the column make that column to be green color.
例如,在第一个中,最小值是0.001,所以我希望它是绿色;第二个,在0.002中,最小值是我希望它是绿色.
For example in 1st the smallest value is 0.001 so i want it to be green color, for second 0.002 is smallest value i want it to be green color.
有谁能指导我做到这一点,谢谢
can any one guide me how to make this ,thanks
下面的代码是我如何从数据库中选择它并在表中显示int的代码
below is the code how i selecting it from database and displaying int in a table
<?php
$dbHost = 'localhost'; // usually localhost
$dbUsername = 'xxxx';
$dbPassword = 'xxxx';
$dbDatabase = 'xxxx';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");
$ColumnNames = mysql_query("SELECT column_name
FROM information_schema.COLUMNS
WHERE table_name = 'supplierprice'
AND column_name NOT IN ('supp_price_id',
'region',
'country',
'net_id',
'networkname',
'mcc',
'mnc',
'mnp')")
or die("mysql error");
$columnArray=array();
$i=0;
while($rows=mysql_fetch_array($ColumnNames))
{
$columnArray[]=$rows[0];
echo "<th style='width:67px;' class='. $columnArray[$i] .' >" . $columnArray[$i] . "
</th>";
$i++;
}
?>
foreach($columnArray as $value) {
//$columnArray[]=$rows1[0];
echo '<td style="width:67px;font-weight:'.$text.'" id="CPH_GridView1_xxx" width="0px;" class="'.$value.' '.$rows["net_id"].'"><p>'.$rows[$value].'</p></td>';
}
推荐答案
不确定您的要求.您似乎正在获取表的某些列名称.我假设您想获取这些列的值并将其显示在表格中,突出显示具有最低值的列.
Not entirely sure on your requirements. You appear to be getting some column names for a table. I presume that you want to get the values of these columns and display them in a table, highlighting the one with the lowest value.
如果是这样的话.获取列,循环遍历一次以显示标题.然后,对于每一行,它调用一个带有显示它们的函数.找到最低值的键(如果2个键共享最低值,则使用最低键).它在返回的列周围循环并回显它们,以最低列的样式显示 color:#ff0000; .
If some something like this. Gets the columns, loops through them once to display the headings. For each row it then calls a function with displays them. The key of the lowest value is found (if 2 keys share the lowest value then the lowest key is used). It loops around the columns returned and echos them out, putting out a color:#ff0000; in the style for the lowest value column.
<?php
$dbHost = 'localhost'; // usually localhost
$dbUsername = 'xxxx';
$dbPassword = 'xxxx';
$dbDatabase = 'xxxx';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");
$ColumnNames = mysql_query("SELECT GROUP_CONCAT(CONCAT('`', column_name, '`')) AS some_columns
FROM information_schema.COLUMNS
WHERE table_name = 'supplierprice'
AND column_name NOT IN ('supp_price_id',
'region',
'country',
'net_id',
'networkname',
'mcc',
'mnc',
'mnp')")
or die("mysql error");
if($rows=mysql_fetch_array($ColumnNames))
{
$sql = "SELECT ".$row['some_columns']." FROM supplierprice";
$query_values = mysql_query($sql);
if($rows2=mysql_fetch_assoc($ColumnNames))
{
echo "<tr>";
foreach($rows2 AS $key=>$value)
{
echo "<th style='width:67px;' >".$key." </th>";
}
echo "</tr>";
process_row($rows2);
while($rows2=mysql_fetch_assoc($ColumnNames))
{
process_row($rows2);
}
}
}
function process_row($in_row)
{
$lowest_values_key = min(array_keys($in_row, min($in_row)));
echo "<tr>";
foreach($in_row AS $key=>$value)
{
echo "<td style='width:67px;".(($lowest_values_key == $key) ? 'color:#ff0000;' : '' )."' >".$value." </th>";
}
echo "</tr>";
}
?>
这篇关于如何使用PHP从多个列中选择最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!