简体   繁体   中英

PHP color table from mysql database

i am having some problems with my echo '<tr style="background-color: . $color . '">'; it keeps giving me Parse error: syntax error, unexpected 'background' (T_STRING) in C:\\xampp\\htdocs\\test.php line 146 and i am pretty new to php so i have no clue what i can do.

My purpes with this code is to get some information from a colum in mysql database and then show it as a table in a website(that i have allready fixed, but i need the colors to work) and then it is going to color some of the table rows after some of their value in the database.

PS: This is not my whole code.

hope someone can help, Thanks.

 while($row = mysql_fetch_array($rs)) {


if ( ($row['Flyttet'] == "NEI") && ($row['Slettet'] != "JA") && ($row['Delt'] != "NEI") ) {
    $red ='#ff0000';    
}
else {
    $red = false;
}

if ( (($row['Flyttet'] == "NEI") && ($row['Slettet'] != "NEI") && ($row['Delt'] != "JA")) || (($row['Flyttet'] == "JA") && ($row['Slettet'] != "NEI") && ($row['Delt'] != "JA"))  ) {
    $gul ='#FFFF00';    
}
else {
    $gul = false;
}

if ( ($row['Flyttet'] == "JA") && ($row['Slettet'] != "NEI") && ($row['Delt'] != "NEI") ) {
    $blue ='#1E90FF';    
}
else {
    $blue = false;
}

if ( ($row['Flyttet'] == "NEI") && ($row['Slettet'] != "NEI") && ($row['Delt'] != "JA") ) {
    $green ='#3CB371';    
}
else {
    $green = false;
}


$color = ";
if(!$red && !$gul && !$blue) $color = $green;
if(!$red && !$gul && !$green) $color = $blue;
if(!$red && !$green && !$blue) $color = $gul;
if(!$green && !$gul && !$blue) $color = $red;

    //Første kolone som skal vises
    echo '<tr style="background-color: . $color . '">';
    echo "<td>" . utf8_encode($row['ID']) . "</td>";
    echo "<td>" . utf8_encode($row['Vsite']) . "</td>";
    echo "<td>" . utf8_encode($row['Registrar']) . "</td>";
    echo "<td>" . utf8_encode($row['Eier']) . "</td>";
    echo "<td>" . utf8_encode($row['NS']) . "</td>";
    echo "<td>" . utf8_encode($row['A']) . "</td>";
    echo "<td>" . utf8_encode($row['MX']) . "</td>";
    echo "<td>" . utf8_encode($row['Flyttet']) . "</td>";
    echo "<td>" . utf8_encode($row['Slettet']) . "</td>";
    echo "<td>" . utf8_encode($row['Delt']) . "</td>";
    echo "<td>" . utf8_encode($row['Behkd']) . "</td>";
    echo "<td>" . utf8_encode($row['Varenr']) . "</td>";
    echo "<td>" . utf8_encode($row['Server']) . "</td>";
    echo "<td>" . utf8_encode($row['Sistoppdatert']) . "</td>";
    echo "</tr>";

    }

You initialize your variable color wrong. It must be $color = ""; .

$color = "";
    if(!$red && !$gul && !$blue) $color = $green;
    if(!$red && !$gul && !$green) $color = $blue;
    if(!$red && !$green && !$blue) $color = $gul;
    if(!$green && !$gul && !$blue) $color = $red;

    //Første kolone som skal vises
    echo '<tr style="background-color:' . $color . '">';
    echo "<td>" . utf8_encode($row['ID']) . "</td>";
    echo "<td>" . utf8_encode($row['Vsite']) . "</td>";
    echo "<td>" . utf8_encode($row['Registrar']) . "</td>";
    echo "<td>" . utf8_encode($row['Eier']) . "</td>";
    echo "<td>" . utf8_encode($row['NS']) . "</td>";
    echo "<td>" . utf8_encode($row['A']) . "</td>";
    echo "<td>" . utf8_encode($row['MX']) . "</td>";
    echo "<td>" . utf8_encode($row['Flyttet']) . "</td>";
    echo "<td>" . utf8_encode($row['Slettet']) . "</td>";
    echo "<td>" . utf8_encode($row['Delt']) . "</td>";
    echo "<td>" . utf8_encode($row['Behkd']) . "</td>";
    echo "<td>" . utf8_encode($row['Varenr']) . "</td>";
    echo "<td>" . utf8_encode($row['Server']) . "</td>";
    echo "<td>" . utf8_encode($row['Sistoppdatert']) . "</td>";
    echo "</tr>";

    }

One single quote was missing. It should be like this:

echo '<tr style="background-color:' . $color . '">';

I ended up using something like this

$color = '';

if ( ($row['Flyttet'] == "NEI") && ($row['Slettet'] == "JA") && ($row['Delt'] == "NEI") ) $color ='#ff0000';
if ( (($row['Flyttet'] == "NEI") && ($row['Slettet'] == "NEI") && ($row['Delt'] == "JA"))|| (($row['Flyttet'] == "JA") && ($row['Slettet'] == "NEI") && ($row['Delt'] == "JA")) ) $color ='#FFFF00';
if ( ($row['Flyttet'] == "JA") && ($row['Slettet'] == "NEI") && ($row['Delt'] == "NEI") ) $color ='#1E90FF';
if ( (($row['Flyttet'] == "NEI") && ($row['Slettet'] == "NEI") && ($row['Delt'] == "JA")) || (($row['Flyttet'] == "NEI") && ($row['Slettet'] == "NEI") && ($row['Delt'] == "NEI")) ) $color ='#3CB371';

您的背景色需要双引号:

echo '<tr style="background-color:"' . $color . '"">';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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