0

我将使用 hansontable 的格式将 mysql 结果重新排列为 json。根据这个问题:查找字符串位置并加入另一个表的行。奥拉夫先生有他的天赋。我试过自己做。是的,再次 - 它不起作用。这是我的代码:

$sql_rCost="select date,
       substring_index(cost, '-', 1) type_a,
       case when cost regexp '.*-' then
                 substring_index(substring_index(cost, '-', 2), '-', -1)
            else ''
       end type_b,
       case when cost regexp '.*-.*-' th                substring_index(substring_index(cost, '-', 3), '-', -1)
            else ''
       end type_c,
       case when cost regexp '.*-.*-.*-' then
            substring_index(substring_index(cost, '-', 4), '-', -1)
            else ''
       end type_d,
       case when cost regexp '.*-.*-.*-.*-' then
            substring_index(substring_index(cost, '-', 5), '-', -1)
            else ''
       end type_e
from room_rate_cost where rate='2' and  hotel='1' and date between '2012-11-01' and '2012-11-31' order by date asc
";
$result_rCost=mysql_db_query($dbname,$sql_rCost);
while($rec_rCost=mysql_fetch_array($result_rCost)){
    $_rCostDBdate=$rec_rCost['date'];
    $_rCostDBtypea=$rec_rCost['type_a'];
    $_rCostDBtypeb=$rec_rCost['type_b'];
    $_rCostDBtypec=$rec_rCost['type_c'];
    $_rCostDBtyped=$rec_rCost['type_d'];
    $_rCostDBtypee=$rec_rCost['type_e'];

    $_rCostDBcost[]=$rec_rCost['cost'];

    $_array[]=array(    $_rCostDBdate,$_rCostDBcost);
}
    echo "{\"data\": [".json_encode($_array)."]}";

执行后返回:

{"data": [[["2012-11-01",[null]],["2012-11-02",[null,null]],["2012-11-03",[null,null,null]],["2012-11-04",[null,null,null,null]],["2012-11-05",[null,null,null,null,null]],["2012-11-06",[null,null,null,null,null,null]],["2012-11-07",[null,null,null,null,null,null,null]],["2012-11-08",[null,null,null,null,null,null,null,null]],["2012-11-09",[null,null,null,null,null,null,null,null,null]],["2012-11-10",[null,null,null,null,null,null,null,null,null,null]],["2012-11-11",[null,null,null,null,null,null,null,null,null,null,null]],["2012-11-12",[null,null,null,null,null,null,null,null,null,null,null,null]],["2012-11-13",[null,null,null,null,null,null,null,null,null,null,null,null,null]],["2012-11-14",[null,null,null,null,null,null,null,null,null,null,null,null,null,null]],["2012-11-15",[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]],["2012-11-16",[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]],["2012-11-17",[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]],["2012-11-18",[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]],["2012-11-19",[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]],["2012-11-20",[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]],["2012-11-21",[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]],["2012-11-22",[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]],["2012-11-23",[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]],["2012-11-24",[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]],["2012-11-25",[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]],["2012-11-26",[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]],["2012-11-27",[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]],["2012-11-28",[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]],["2012-11-29",[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]],["2012-11-30",[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]]]]}

预期的格式是:

    {
  "data": [
    ["", "type_a", "type_b", "type_c", "type_d", "type_e"],
    ["2011-11-01", 4700, 5500, 6100, 6600, 7300],
    ["2011-11-02", 4700, 5500, 6100, 6600, 7300],
    ["2011-11-03", 4700, 5500, 6100, 6600, 7300],
    ...
    ["2011-11-30", 4700, 5500, 6100, 6600, 7300],
  ]
}
4

1 回答 1

1

I would change the array creation from:

$_array[]=array($_rCostDBdate,$_rCostDBcost);

to:

$_array[]=array($_rCostDBdate, $_rCostDBtypea, $_rCostDBtypeb, $_rCostDBtypec,
                $_rCostDBtyped, $_rCostDBtypee);

and would leave out the line:

$_rCostDBcost[]=$rec_rCost['cost'];

since in the query result, there is no column named cost.

If you change the query to mysql_fetch_array($result_rCost, MYSQL_NUM), you could reduce the loop to just:

while ($rec_rCost = mysql_fetch_array($result_rCost,  MYSQL_NUM)) {
    $_array[] = $rec_rCost;
}

Update: To add the header line, just insert before the loop:

$_array[] = array('', 'type_a', 'type_b', 'type_c', 'type_d', 'type_e');
于 2012-11-04T19:17:22.997 回答