使用多列的最早日期时间

时间:2014-06-14 23:00:10

标签: php mysql datetime

我的表包含10个日期时间列(item1 - item10),代表我的成员提供的10项服务。在任何给定的“城市/县”中,10项服务中每项服务只有12个职位。基本上,我需要将每列中最旧的12个日期时间分别作为“item1”,“item2”,“item3”等返回,否则返回为“”。

项目#被回显到成员的列表中,我使用jquery来过滤列表。

我当前的代码设置了一个限制,但不是根据日期时间。如果页面上的所有位置都已填满,则会出现此问题。如果一位资深成员决定提供额外服务,他们就可以“挫败”新成员。

如果我不能很好地解释这个,这是一个有效的例子: http://integritycontractingofva.com/pest_control/Richmond_VA.php

$result = @mysqli_query($db, "SELECT * FROM table WHERE category LIKE '%$category%' AND territories LIKE '%$area.%' AND exp >= NOW()");
if (!$result){echo ("<p>Error performing listing query:" . mysql_error() . "</p>");}

$item1cnt = $item2cnt = $item3cnt = $item4cnt = $item5cnt = $item6cnt = $item7cnt = $item8cnt = $item9cnt = $item10cnt = 0;   //start counter at "0"
$items = array();
$today = date('Y-m-d');
  while ($row = mysqli_fetch_array($result)){
    if($row["exp"] > $today){

      if($row["item1"]!="0000-00-00 00:00:00"){ // if datetime is set
        $item1cnt++;                            // add one to counter
        if($item1cnt > 12){                     // if counter is greater than 12
          $row["item1"]="";                     // itemx = ""
        }else{                                  // if counter is less than 12
          $row["item1"]="item1";                // item = itemx
        }
      }else{                                    // if datetime is not set
          $row["item1"]="";                     // itemx = ""
       }

// repeat above for all 10 items


// part of member's listing used by jquery to filter services
  $items[] = "<li class=\"services " . $row["item1"] . " " . $row["item2"] . " " . $row["item3"] . " " . $row["item4"] . " " . $row["item5"] . " " . $row["item6"] . " " . $row["item7"] . " " . $row["item8"] . " " . $row["item9"] . " " . $row["item10"] . "\">";

  }
}

如果成员的日期时间设置为item1item3item9,则打印结果为<li class="services item1 item3 item9">

1 个答案:

答案 0 :(得分:2)

不确定这会有效并且不确定性能,但您可以尝试这样的事情。

SELECT
    t.*,
    CONCAT_WS(' ',
        a1.class,
        a2.class,
        ...
        a10.class
    ) AS class
FROM table t
    LEFT JOIN (SELECT id,'item1' AS class FROM table ORDER BY item1 desc LIMIT 12) a1 ON t.id=a1.id
    LEFT JOIN (SELECT id,'item2' AS class FROM table ORDER BY item2 desc LIMIT 12) a2 ON t.id=a2.id
    ...
    LEFT JOIN (SELECT id,'item10' AS class FROM table ORDER BY item10 desc LIMIT 12) a10 ON t.id=a10.id

以及所有这些

if($row["item1"]!="0000-00-00 00:00:00"){ // if datetime is set
    $item1cnt++;                          // add one to counter
    if($item1cnt > 12){                   // if counter is greater than 12
        $row["item1"]="";                 // itemx = ""
    }else{                                // if counter is less than 12
        $row["item1"]="item1";            // item = itemx
    }
}else{                                    // if datetime is not set
    $row["item1"]="";                     // itemx = ""
}
// repeat above for all 10 items

可以像这样编写(对于所有10个元素):

// config
$item_count = 10;
$show_records = 12;
// process
$cnt = array_fill(1, $item_count, 0);
for ($i = 1; $i <= $item_count; $i++) {
    $n = "item" . $i;
    if (!$row[$n] || $row[$n] === "0000-00-00 00:00:00" || $cnt[$i] >= $show_records) {
        $row[$n] = "";
    } else {
        $cnt[$n]++;
    }
}

最终解决方案:

// === SELECTING ===
$result = mysqli_query($db, "
    SELECT
        t.*,
        CONCAT_WS(' ',
            a1.class,
            a2.class,
            ...
            a10.class
        ) AS classes
    FROM table t
        LEFT JOIN (SELECT id,'item1' AS class FROM table ORDER BY item1 desc LIMIT 12) a1 ON t.id=a1.id
        LEFT JOIN (SELECT id,'item2' AS class FROM table ORDER BY item2 desc LIMIT 12) a2 ON t.id=a2.id
        ...
        LEFT JOIN (SELECT id,'item10' AS class FROM table ORDER BY item10 desc LIMIT 12) a10 ON t.id=a10.id
    WHERE
        t.category LIKE '%$category%'
        AND t.territories LIKE '%$area.%'
        AND t.exp >= NOW()    
");
if (!$result){
    echo ("<p>Error performing listing query:" . mysql_error() . "</p>");
    exit;
}

// === PARSING ===

// config
$item_count = 10;
$show_records = 12;
// process
$cnt = array_fill(1, $item_count, 0);
for ($i = 1; $i <= $item_count; $i++) {
    $n = "item" . $i;
    if (!$row[$n] || $row[$n] === "0000-00-00 00:00:00" || $cnt[$i] >= $show_records) {
        $row[$n] = "";
    } else {
        $cnt[$n]++;
    }
}

// part of member's listing used by jquery to filter services
$items[] = '<li class="services ' . $row['classes'] . '">';

P.S。在SQL查询中注入之前,如果用户输入了$category$area,则应正确转义它们。这可以使用mysql_real_escape完成。