第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用 PHP 和 MySQL 查詢?yōu)橐幌盗袛?shù)據(jù)創(chuàng)建 JSON

使用 PHP 和 MySQL 查詢?yōu)橐幌盗袛?shù)據(jù)創(chuàng)建 JSON

PHP
米琪卡哇伊 2022-06-17 15:48:29
我正在嘗試使用 JSON 數(shù)據(jù)創(chuàng)建條形圖,并且需要將格式更改為月份系列,但我對(duì)如何做到這一點(diǎn)感到困惑。請(qǐng)幫助我如何解決這個(gè)問題。當(dāng)前 JSON 數(shù)據(jù):{ “數(shù)據(jù)”: [{“名稱”:“一月”,“設(shè)施”:“討論室”,“價(jià)值”:22},{“名稱”:“一月”,“設(shè)施”:“卡雷爾室”,“價(jià)值”:102},{“名稱”:“一月”,“設(shè)施”:“儲(chǔ)物柜”,“價(jià)值”:5},{“名稱”:“二月”,“設(shè)施”:“討論室”,“價(jià)值”:86},{“名稱”:“二月”,“設(shè)施”:“卡雷爾室”,“價(jià)值”:155},{“名稱”:“Mac”,“設(shè)施”:“卡雷爾室”,“價(jià)值”:224},{“名稱”:“Mac”,“設(shè)施”:“儲(chǔ)物柜”,“價(jià)值”:3},]}所需的 JSON 數(shù)據(jù):{ “數(shù)據(jù)”: [{“名稱”:“一月”,“系列”:[{ "name":"討論室","value": 22},{ "name": "Carrel Room", "value": 102},{“名稱”:“儲(chǔ)物柜”,“價(jià)值”:5},]“名稱”:“二月”,“系列”:[{ "name":"討論室","value": 86},{ "name": "Carrel Room", "value": 155},{“名稱”:“儲(chǔ)物柜”,“價(jià)值”:0},]“名稱”:“三月”,“系列”:[{ "name":"討論室","value": 0},{ "name": "Carrel Room", "value": 224},{“名稱”:“儲(chǔ)物柜”,“價(jià)值”:3},]]}我的代碼:$db = mysqli_connect($host, $user, $pass, $database) or die("you did not connect");    header('Access-Control-Allow-Origin: *');    header("Access-Control-Allow-Credentials: true");    header('Access-Control-Allow-Methods: GET');    header('Access-Control-Max-Age: 1000');    header('Access-Control-Allow-Headers: Origin, Content-Type');$facility =($_GET['year']);$query = "select monthname(datetime) as 'name',        case             when all_items.itype = '127' then 'Discussion Room'            when all_items.itype = '126' then 'Carrel Room'            when all_items.itype = '121' then 'Locker'            else '0'         end as 'facility',        count(*) AS 'value'        from statistics         left join (            select itemnumber, itype from deleteditems            union            select itemnumber, itype from items             ) as all_items USING (itemnumber)        where all_items.itype in (127, 126, 121) and        statistics.type = 'issue' and        year(statistics.datetime) = '$facility'         group by month(datetime), all_items.itype desc";$result = mysqli_query($db, $query)or die(mysqli_error());$response = array();$posts = array();
查看完整描述

2 回答

?
弒天下

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超8個(gè)贊

您應(yīng)該更改查詢數(shù)據(jù)庫(kù)的方式。您應(yīng)該使用準(zhǔn)備好的語句,而不是在查詢中注入字符串。這使您容易受到 sql 注入的影響。

閱讀有關(guān)準(zhǔn)備好的語句的更多信息。

回到問題:

對(duì)于發(fā)現(xiàn)的每個(gè)月,您需要?jiǎng)?chuàng)建一個(gè)數(shù)組,其中包含一個(gè)系列數(shù)組。檢查月份數(shù)組是否存在,如果它不創(chuàng)建它并創(chuàng)建系列數(shù)組。然后創(chuàng)建一個(gè)包含系列數(shù)據(jù)的數(shù)組,并將其中的系列數(shù)據(jù)推送到該月的系列數(shù)組中。

查看代碼要容易得多。我也添加了評(píng)論。


$result = mysqli_query($db, $query)or die(mysqli_error());


$response = array();


$posts = array();


while($row=$result->fetch_assoc()) 

{

    $month=$row['name'];


    if(!isset($posts[$month])){// Check to see if the month array exists

    //if it doesn't create it

        $posts[$month] = array();


        //create the series array so data can be pushed to it

        $posts[$month]["series"] = array();

    }


    //check to see if it is null and if it is make it 0

    if(is_null($row['value'])){

        $row['value'] =0;

    }


    //put the series data in the correct format with correct names

    $series_array = array(

        "name" => $row['facility'],

        "value" => $row['value']

    );


    //push the series data into the months series array

    array_push($posts[$month]["series"],$series_array);

}

// put months and series together 

$new_posts=array();

foreach($posts as $month_name => $data){

    array_push($new_posts, array("name"=> $month_name, "series"=> $data['series'] ));

}


$posts = $new_posts;

$response['data'] = array($posts);


header('Content-Type: application/json');

echo json_encode($response, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);


查看完整回答
反對(duì) 回復(fù) 2022-06-17
?
素胚勾勒不出你

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超9個(gè)贊

這是您需要的代碼


$result = mysqli_query($db, $query)or die(mysqli_error($db));

$response = array();

$posts = array();

while($row=$result->fetch_assoc()){

    $month = $row['name'];

    $value = $row['value'];

    if(empty($value)){

        $value = 0;

    }

    $series = array(

        "name"  => $row['facility'],

        "value" => $value 

    );

    $found_key = array_search($month, array_column($posts, 'name'));// check if month value exist in name key 

    if(!$found_key){//if not found

        $found_key = sizeof($posts);// get the size of array which will be 1 more then last index of array 

    }

    $posts[$found_key]['name']  = $month;// this add name of month 

    if(!isset($posts[$found_key]['series'])){ // check if series is set

       $posts[$found_key]['series'] = array();// or set it

    }

    $posts[$found_key]['series'][] = $series; //assign the series array

}

$response['data'] = $posts;

header('Content-Type: application/json');

echo json_encode($response, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);


查看完整回答
反對(duì) 回復(fù) 2022-06-17
  • 2 回答
  • 0 關(guān)注
  • 173 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)