PHP yaş ve sonuçları kodlama renge göre sıralama?

2 Cevap php

Ben veritabanından tek bir rastgele seçim çalışmasına her zaman sunan bir program var. Ne yapmak istiyorum seçimleri aktivite düzeyine göre sıralanır ve sonra rasgele seçim nasıl aktif temsil eden bir renkte gösterilir seçim başlık olması.

Örneğin: Item 1 45 gün eski, Item 2 61 gün eski ve Item 3 10 gün eskidir.

Zaman aralığı: 1-45days (siyah), 46-60 günlük (mor) ve 61days üzerinde (mavi)

I would like the PHP to sort the Items and when the program is run I would want the random item selected to be color coded so that: If Item 3 was chosen the text for the items title would be in color:#000000 If Item 1 was chosen the text for the items title would be in color:#770077 If Item 2 was chosen the text for the items title would be in color:#0000ff

Mevcut PHP programı öğenin yaş veya öğeleri başlığı boyama ile herhangi bir değişken uğraşan yoktur. PHP (sadece konuyla ilgili Larry Ullman kitabı aldım) ile hemen hemen tam bir acemi değilim yani bu yapılabilir eğer ben bile bilmiyorum ama ben sormak ve görmek düşündüm ...

2 Cevap

Sen aşağıdaki yöntemi deneyebilirsiniz. Ben sırayla sonraki dizi indeks her zaman bir önceki aralıktan daha büyük olduğunu varsayarak tarafından min / max azalttığını unutmayın.

// a mapping of ranges and their associated color
$range_map = array(
    array('color' => '#000000', 'min' => 1, 'max' => 46), 
    array('color' => '#770077', 'min' => 46, 'max' => 61),
    array('color' => '#0000ff', 'min' => 61, 'max' => 1000000)
);

$output = '';
foreach ($items as $item) {
    $color = '';
    foreach ($range_map as $range) {
        if ($item->days_old >= $range['min'] && $item->days_old < $range['max']) {
            $color = $range['color'];
            break;
        }
    }

    if (!empty($color)) {
        $output .= '<span style="color:' . $color . '">' . $YOUR_OUTPUT_HERE . '</span>';
    } else {
        $output .= $YOUR_OUTPUT_HERE;
    }
}

echo $output;

Bu hızlı çözüm değil ama çok az değişiklik ile vakaların herhangi bir sayıda işleyebilir.

Tamam. Aşağıdaki Bunu yapmanın en basit yolu:

$items[0]['title'] = 45;
$items[0]['daysold'] = 45;
$items[1]['title'] = 61;
$items[1]['daysold'] = 45;
$items[2]['title'] = 10;
$items[2]['daysold'] = 45;

$output = '';
foreach ($items as $item) {
    switch ($item['daysold']) {
        case ($item['daysold'] > 60):
            $color = "#00FFFF";
            break;
        case ($item['daysold'] > 45):
            $color = "#AA00FF";
            break;
        default:
            $color = "#000000";
    }
    $output .= '<span style="color:' . $color . '">' . $item['title'] . '</span>';
}
echo $output;

Madde 1 Madde 2 mavi olmalı, siyah olmalı ve Madde 3 ihtiyaçlarına göre de siyah olmalıdır.