简体   繁体   中英

while loop and echo background color

I need to loop through my while loop, and echo the background color out that belongs to the specifik number. the variable $green is working, but I really dont know how to make the others work.

I have a form field where I can put in my number, and to the number belongs a specifik background color, which is defined in the array.

Can anybody see how I can finish my while loop?

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
//include('session.php');

// Selecting Database
include 'dbconfic.inc.php';
$green = array(0 => "#81c77d", 2 => "#81c77d", 3 => "#81c77d", 4 => "#81c77d", 7 => "#81c77d", 12 => "#81c77d", 15 => "#81c77d", 18 => "#81c77d", 19 => "#81c77d", 21 => "#81c77d", 22 => "#81c77d", 25 => "#81c77d", 26 => "#81c77d", 28 => "#81c77d", 29 => "#81c77d", 32 => "#81c77d", 35 => "#81c77d");
$darkgreen = array(0 => "#81e87c", 3 => "#81e87c", 12 => "#81e87c", 15 => "#81e87c", 26 => "#81e87c", 32 => "#81e87c", 35 => "#81e87c");
$white = array(6 => "#ffffff", 17 => "#ffffff", 34 => "#ffffff");
$red = array(5 => "#dfb07b", 8 => "#dfb07b", 10 => "#dfb07b", 11 => "#dfb07b", 13 => "#dfb07b", 16 => "#dfb07b", 23 => "#dfb07b", 24 => "#dfb07b", 27 => "#dfb07b", 30 => "#dfb07b", 33 => "#dfb07b", 36 => "#dfb07b");
$grey = array(1 =>"#ffffff", 9 => "#ffffff", 14 => "#ffffff", 20 => "#ffffff", 31 => "#ffffff");

    // '?' is placeholders for variabler
    $stmt = $mysqli->prepare("SELECT * FROM numbertable ORDER BY num_id DESC LIMIT 27;");

    // execute prepared statement 
    $stmt->execute();

    // Make variables ready    
    $number = null;    
    $n_id = null;

    /* bind result variabler */
    $stmt->bind_result($n_id, $number);

    /* fetch values for each row*/

     while ($stmt->fetch()) {
        $voi = ($number = $green? $green[$number] : $green[0]);


            echo "<li><div style='background-color: ".$voi."'>$number</div></li>";
     }

    // close statement                        
    $stmt->close();

    // close connection
    $mysqli->close();

?>

** Updated (is one big array better?)**

$property=array(
    "green"     => array(0 => "#81c77d", 2 => "#81c77d", 3 => "#81c77d", 4 => "#81c77d", 7 => "#81c77d", 12 => "#81c77d", 15 => "#81c77d", 18 => "#81c77d", 19 => "#81c77d", 21 => "#81c77d", 22 => "#81c77d", 25 => "#81c77d", 26 => "#81c77d", 28 => "#81c77d", 29 => "#81c77d", 32 => "#81c77d", 35 => "#81c77d"),
    "darkgreen"         => array(0 => "#81e87c", 3 => "#81e87c", 12 => "#81e87c", 15 => "#81e87c", 26 => "#81e87c", 32 => "#81e87c", 35 => "#81e87c"),
    "white"     => array(6 => "#ffffff", 17 => "#ffffff", 34 => "#ffffff"),
    "red"     => array(5 => "#dfb07b", 8 => "#dfb07b", 10 => "#dfb07b", 11 => "#dfb07b", 13 => "#dfb07b", 16 => "#dfb07b", 23 => "#dfb07b", 24 => "#dfb07b", 27 => "#dfb07b", 30 => "#dfb07b", 33 => "#dfb07b", 36 => "#dfb07b"),
    "grey"     => array(1 => "#ffffff", 9 => "#ffffff", 14 => "#ffffff", 20 => "#ffffff", 31 => "#ffffff")
); 

You should change your code like below, a single = is not used in condition. for check two variable you should check it with == .

also $number is index of array, NOT array.

$voi = (isset($green[$number]) ? $green[$number] : $green[0]);

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