2016-04-21 45 views
0

我有一個web表單,允許人們註冊一個類。表單底部是一個提交按鈕,顯示「註冊」。在我的PHP代碼中,我正在檢查爲該課程註冊的人數。如果計數等於特定的數字,例如60個註冊人,我想將該按鈕更改爲紅色,文本更改爲「Class is Full」。如果我只能定義一個按鈕顏色,如何用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; 
 
}

在我的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'; 
} 
?> 

我知道我不能在CSS中使用 '按鈕1' ,但我該怎麼做,所以它變成了紅色,並說當$ count = 60時課程已滿?

+0

請[停止使用'mysql_ *'函數](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。 [這些擴展](http://php.net/manual/en/migration70.removed-exts-sapis.php)已在PHP 7中刪除。瞭解[編寫]​​(http://en.wikipedia.org/ wiki/Prepared_statement)語句[PDO](http://php.net/manual/en/pdo.prepared-statements.php)和[MySQLi](http://php.net/manual/en/mysqli.quickstart .prepared-statements.php)並考慮使用PDO,[這真的很簡單](http://jayblanchard.net/demystifying_php_pdo.html)。 –

回答

2

您需要fetch,http://php.net/manual/en/function.mysql-fetch-row.php,查詢結果。那麼你會有價值。

請注意,您也正在使用已棄用/已刪除的驅動程序。

警告 此擴展在PHP 5.5.0中被棄用,它在PHP 7.0.0中被刪除。相反,應該使用MySQLi或PDO_MySQL擴展。

因此,代碼如下:

<?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'; 
} 
?> 

您還應該使用id S或class s到您指定的CSS屬性。

...或與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'; 
} 
?> 

然後CSS分配成爲:

#aval { 

#full { 

.更換#如果您選擇使用class。如果可以有多個事件使用類。

+0

工作。謝謝。 – RickCJ7