gpt4 book ai didi

php - 根据php中数据库的值以不同颜色突出显示表格行

转载 作者:行者123 更新时间:2023-11-29 22:26:48 26 4
gpt4 key购买 nike

下面的代码有动态下拉列表,用户选择首选的品牌和型号,相应的表格会在网页中生成品牌、型号和年份的结果。现在,代码需要将同一年的所有不同模型分组,并相应地用每年不同的颜色集突出显示它们。预期结果以图像形式附加。

// (___ & !!!) or (ALL & ___) or (ALL & ALL)
if ((!isset($_POST['model']) || ($_POST['brand'] == 'ALL' && empty($_POST['model']))) || ($_POST['brand'] == 'ALL' && $_POST['sip'] == 'ALL')) {
$query1 = 'SELECT DISTINCT brand,model,year FROM `carlist` ORDER BY brand,model,year';
}
// (... & ...) or (... & ALL)
else if (!empty($_POST['brand']) && (empty($_POST['model']) || ($_POST['model'] == 'ALL'))) {
$query1 = 'SELECT DISTINCT brand,model,year FROM `carlist` WHERE brand="'.$_POST['brand'].'" ORDER BY brand,model,year';
}
// (ALL & ...) or (ALL & notALL)
else if ($_POST['brand'] == 'ALL' && (!empty($_POST['model']) && $_POST['model'] != 'ALL')) {
$query1 = 'SELECT DISTINCT brand,model,year FROM `carlist` WHERE model="'.$_POST['model'].'" ORDER BY brand,model,year';
}
else {
$query1 = 'SELECT DISTINCT brand,model,year FROM `carlist` WHERE brand="'.$_POST['brand'].'" AND model="'.$_POST['model'].'" ORDER BY brand,model,year';
}
//echo $_POST['brand'].'<br />'.$_POST['model'];

echo '<table border=1><tr><th>BRAND</th><th>MODEL</th><th>YEAR</th></tr>';
$result1 = mysqli_query($con, $query1);


while($row1 = mysqli_fetch_array($result1)){
//echo'<tr><td>'.$row1['brand'].'</td><td>'.$row1['model'].'</td><td>'.$row1['year'].'</td></tr>';

$query2= 'SELECT DISTINCT brand,model,year, count(*) `number` FROM `carlist` WHERE brand="'.$_POST['brand'].'" GROUP BY `year` HAVING count(*) > 1';
$result2= mysqli_query($con, $query2);
$count=0;


while($row2 = mysqli_fetch_array($result2)){

if ( $row2['year'] == $row1['year']) {
$count=1;
}

//$intersect = array_intersect($row1,$row2);
//echo $intersect[1];


}

$bgcolor = "#FF8C00";
if($count==1){
echo '<tr style="background-color: tomato;" ><td>'.$row1['brand'].'</td><td>'.$row1['model'].'</td><td>'.$row1['year'].'</td></tr>';

}else {

echo'<tr><td>'.$row1['brand'].'</td><td>'.$row1['model'].'</td><td>'.$row1['year'].'</td></tr>';
}

}


echo '</table>';

最佳答案

创建生成随机颜色的函数

function getColor($year){
return '#'.substr(md5($year), 0, 6);
}

在 while 循环中

// Get color for current year
// Same years will be colored with same color
$bgColor = getColor($row1['year']);

// Print table row
echo '<tr style="background-color: '.$bgColor.';"><td>'.$row1['brand'].'</td><td>'.$row1['model'].'</td><td>'.$row1['year'].'</td></tr>';

所以你的while循环将是

while($row1 = mysqli_fetch_array($result1)){

// Delete extra while and if condition

$bgColor = getColor($row1['year']);
echo '<tr style="background-color: '.$bgColor.';"><td>'.$row1['brand'].'</td><td>'.$row1['model'].'</td><td>'.$row1['year'].'</td></tr>';
}

注意:您的查询容易受到 SQL 注入(inject)的攻击。请参阅this answer以及如何预防。

注 2:由于背景颜色是自动生成的,因此可能会出现文本不易阅读的情况。

关于php - 根据php中数据库的值以不同颜色突出显示表格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30207069/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com