doudou7361 2016-01-09 22:54
浏览 43
已采纳

回声不同的背景色

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.

I have a formfield, where I can put in a number. 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; I have made the arrays for it, which I think is okay?

But how can I make my while loop for it?

<?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";

?>
  • 写回答

1条回答 默认 最新

  • duanbi3151 2016-01-09 23:07
    关注

    You can pick colors from color array by using an increamental variable like this:

    Your while loop:

    $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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)