简体   繁体   English

基于数据库查询的不同按钮颜色

[英]Different button colors based on database query

I have a web form that allows people to sign up for a class. 我有一个网络表格,允许人们报名参加课程。 At the bottom of the form is a submit button that says "Sign Up". 表单底部是一个提交按钮,上面写着“注册”。 In my PHP code, I am checking the number of people that have registered for the class. 在我的PHP代码中,我正在检查已注册该类的人数。 If the count is equal to a specific number, say 60 registrants, I want to change the button to be red and the text is changed to "Class is Full". 如果计数等于一个特定数字,例如60个注册者,我想将按钮更改为红色,然后将文本更改为“类别已满”。 How do I do this with a CSS if I can only define one button color? 如果我只能定义一种按钮颜色,如何使用CSS做到这一点?

This is my CSS: 这是我的CSS:

 button { padding: 19px 39px 18px 39px; color: #FFF; background-color: #4bc970; font-size: 18px; text-align: center; font-style: normal; border-radius: 5px; width: 100%; border: 1px solid #3ac162; border-width: 1px 1px 3px; box-shadow: 0 -1px 0 rgba(255,255,255,0.1) inset; margin-bottom: 10px; } button1 { padding: 19px 39px 18px 39px; color: #FFF; background-color: #ff0000; font-size: 18px; text-align: center; font-style: normal; border-radius: 5px; width: 100%; border: 1px solid #3ac162; border-width: 1px 1px 3px; box-shadow: 0 -1px 0 rgba(255,255,255,0.1) inset; margin-bottom: 10px; } 

In my PHP code, I have this: 在我的PHP代码中,我有:

<?php
$count = mysql_query("select count(*) from students;");
if ($count < 60){
    echo'<button type="submit" name="signup">Sign Up';
}else{
    echo'<button1 type="submit" name="">Class is Full';
}
?>

I know I can't use 'button1' in CSS, but how do I do it so it turns red and says class is full when $count = 60? 我知道我不能在CSS中使用“ button1”,但是我该怎么办,当$ count = 60时,它变成红色并表示类已满?

You need to fetch , http://php.net/manual/en/function.mysql-fetch-row.php , the result of your query. 您需要fetch http://php.net/manual/en/function.mysql-fetch-row.php (查询结果)。 Then you will have the value. 然后,您将具有价值。

Note that you are using a deprecated/removed driver as well. 请注意,您也正在使用不推荐使用/已删除的驱动程序。

Warning This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. 警告此扩展在PHP 5.5.0中已弃用,在PHP 7.0.0中已被删除。 Instead, the MySQLi or PDO_MySQL extension should be used. 相反,应使用MySQLi或PDO_MySQL扩展。

So code would be: 因此代码将是:

<?php
$count = mysql_query("select count(*) from students;");
$array = mysql_fetch_row($count);
if ($array[0] < 60){
    echo'<button type="submit" name="signup">Sign Up';
}else{
    echo'<button1 type="submit" name="">Class is Full';
}
?>

You also should use id s or class s to assign your CSS properties. 您还应该使用idclass来分配CSS属性。

...or with the id approach: ...或采用id方法:

<?php
$count = mysql_query("select count(*) from students;");
$array = mysql_fetch_row($count);
if ($array[0] < 60){
    echo'<button type="submit" id="aval" name="signup">Sign Up';
}else{
    echo'<button1 type="submit" name="" id="full">Class is Full';
}
?>

Then the CSS assignments become: 然后,CSS分配变为:

#aval {

and

#full {

or replace # with . 或将#替换为. if you choose to use a class . 如果您选择使用class If you can have multiple occurrences use classes. 如果可以多次出现,请使用类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM