简体   繁体   English

回声不同的背景色

[英]echo different background-color

I have started to make some code, but I really do not know how to make my while loop, or if I am thinking right at all. 我已经开始编写一些代码,但是我真的不知道如何制作while循环,或者我是否在考虑正确。

I have a formfield, where I can put in a number. 我有一个Formfield,可以在其中输入数字。 That number is printed out in: 该号码打印在:

<div id="showsection">          
            <ul class="sectionnumbers" style="list-style:none; padding: 4px 0px 0px 0px ;">
                <?php include('sections.php');?>
            </ul>
        </div>

Everytime I fx put in the number "0", it should get the background: "#81c77d", with a height on 20px; 每当我fx输入数字“ 0”时,它应该得到背景:“#81c77d”,高度为20px; I have made the arrays for it, which I think is okay? 我已经为此制作了数组,我认为可以吗?

But how can I make my while loop for it? 但是我该如何使其while循环呢?

<?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';
$voisins = 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");
$jeu = array(0 => "#81e87c", 3 => "#81e87c", 12 => "#81e87c", 15 => "#81e87c", 26 => "#81e87c", 32 => "#81e87c", 35 => "#81e87c");
$orp_1 = array(6 => "#ffffff", 17 => "#ffffff", 34 => "#ffffff");
$tiers = 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");
$orp_2 = array(1 => "#ffffff", 9 => "#ffffff", 14 => "#ffffff", 20 => "#ffffff", 31 => "#ffffff");

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

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

    // gør variabler klar:    
    $number = null;    
    $n_id = null;

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

   /* fetch values for hver row, her kun 1 row dog: */
     while ($stmt->fetch()) {

            echo "<li><div style='background-color: ".$voisins."</li>";
            // loop through number and echo the background color of array


        }

     }  


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

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

echo "test";

?>

You can pick colors from color array by using an increamental variable like this: 您可以通过使用像这样的increamental变量从颜色数组中选择颜色:

Your while loop: 您的while循环:

$i = 0; // initilize as 0
while ($stmt->fetch()) {


if ( !isset ($voisins[$i] )){
$i = 0; reinitilize if index null or offset color will repeat after last iteration.
}

 echo "<li><div style='background-color: ".$voisins[$i]."'></li>";


// loop through number and echo the background color of array
    $i++; 

}

Note that: You have an error in your html you forgot to close the style quotes. 请注意:您的html中有一个错误,您忘记了关闭样式引号。

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

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