0%

PHP array cheetsheet

array_keys: get array keys

array_flip: flip array key and value

array_chunk: split array into chunks

  • split array into chunks (把 array 切成數個小 array)
  • $new_array = array_chunk($input_array, 2)
  • $new_array = array_chunk($input_array, 2, true)
    • true: keep original array key (如果設為 true 表示要保留 array key)
  • useful when data updating in batch (在批次匯入大量資料到 db 的時候好用)
  • Ref: https://www.php.net/manual/en/function.array-chunk.php

array_column: get specific key value from multidimensional array

  • get specific key value from multidimensional array (多層 array,取得每個子層的某個鍵的數值拼成新 array)
  • Get the column of first names from a recordset (取每個子層 key 為 first_name 的值,拼成新 array)
    • $first_names = array_column($records, 'first_name');
  • Get the column of last names from a recordset, indexed by the “id” column (取每個子層 key 為 first_name 的值,並且新 array key 為 id,拼成新 array)
    • $last_names = array_column($records, 'last_name', 'id');
  • Ref: https://www.php.net/manual/en/function.array-column.php

array_combine: creates an array by using one array for keys and another for its values

array_search: find if the value in the array (尋找 array 是否有這個數值,有的話回傳 key)

如何操作 PHP dom?

  • the CRUD of php node

A. Create

A-1. create node

  • create a node
    • $element = $dom->createElement('div', 'text');
  • attach $element as a child of $parent (把新增的 $element 加到 $parent 底下)
    • $parent->appendChild($element);

https://www.php.net/manual/en/domdocument.createelement.php

A-2. copy a node

  • $dom->cloneNode($deep = false)
    • $deep defaulted false
  • copy first child of nodes (僅複製第一層的子節點)
    • $element = $dom->cloneNode();
    • $element = $dom->cloneNode(false);
  • copy all descendant nodes (複製所有的子節點)
    • $element = $dom->cloneNode(true);
  • The reference of a node will be moved after cloning it.
    • 注意,在 cloneNode 之後,就沒辦法使用 $element-> parentNode() 找到它的父層

https://www.php.net/manual/en/domnode.clonenode.php

B. Read

B-1. get nodes with the specified tag name (找到有某個 tag name 的 node)

$div = $dom->getElementsByTagName('div');

B-2. about attribute

  • DOMElement::hasAttribute()
    • Checks to see if attribute exists
  • DOMElement::setAttribute()
    • Adds new attribute
  • DOMElement::removeAttribute()
    • Removes attribute
  • example
    • $searchNode->getAttribute('ID');

https://www.php.net/manual/en/domelement.getattribute.php

C. Update

C-1. replace current node

D. Delete

D-1. remove a node

tmux 的基本操作筆記

進入

  • 進入第 0 個 session
    • tmux attach -t 0
    • 如果只有一個,直接 tmux attach 就可以了
  • 切換到不一樣的 window
    • control b + number

建立

  • 建立新的 session
    • tmux
  • 建立新的 window
    • control b + c

關閉

  • 結束可以直接關掉,注意不要使用 exit

chrome extension manifest V3 無法載入 background.js

  • 問題:chrome extension 官方的 getstarted,在 Manifest V3 跑不起來
  • 文件:Getting started - Chrome Developers
    • Step-by-step instructions on how to create a Chrome Extension.
    • Updated on Wednesday, November 18, 2020
  • 遇到的錯誤:
    1
    2
    background.js not working
    chrome.storage.sync.set undefined

處理方式

  1. 確認 Chrome 更新到 88

    Manifest V3 is available beginning with Chrome 88

  2. 處理無法載入 background.js 的情況

  • 原本的範例:

    1
    2
    3
    4
    "background": {
    "scripts": ["background.js"],
    "persistent": false
    },
  • 改寫成:

    1
    2
    3
    "background": {
    "service_worker": "background.js"
    },

    Replace background.page or background.scripts with background.service_worker in manifest.json. Note that the service_worker field takes a string, not an array of strings.

    Remove background.persistent from manifest.json.

驗證

  • 出現下面框起來的這行代表有成功

參考資料

在 Laravel / Mysql,如何讓 DB 的搜尋結果以指定的順序排序?

  • 問題:要取得 table 當中 id 為 20, 21, 1, 7 的資料,並要求取回的資料排序依照此順序

MySQL

1
SELECT * FROM table WHERE id IN (20, 21, 1, 7) ORDER BY FIELD (id, 20, 21, 1, 7)

Laravel Eloquent ORM

1
2
3
4
5
6
$id_list = [20, 21, 1, 7];
$id_list_string = implode(", ", $id_list);

Table::whereIn('id', $id_list)
->orderByRaw("FIELD (id, {$id_list_string})")
->get();

參考資料

在 react js,如何加上 .env 檔案以傳入自訂變數?

步驟

  1. 在 root 加上 .env 檔案,不要放在 /src 底下
  2. .env 檔案內,加上以 REACT_APP_XXXX 開頭的變數
    • 例如:REACT_APP_API_HOST=http://localhost:80
  3. 在 .js 檔案中,如何取用?
    • 不需要 import,直接使用 process.env.REACT_APP_XXXX 即可取得

參考資料

laravel ORM 讀寫分離的 db ,如何強迫取得來自 write db 的資料?

讓 ORM 讀寫入的那一台 DB

Model::onWriteConnection()...

讓 join 的 table,也讀寫入的那台 DB

1
2
3
->with(['joined_table' => function ($query) {
$query->useWritePdo();
}])

組合

1
2
3
Model::onWriteConnection()->with(['joined_table'=>function($query){
$query->useWritePdo();
}])->find($id);

參考資料

如何在 windows 安裝 codimd

前期準備 - 安裝 docker

安裝 codimd

如何取得直接存取 postgresql 的資料?

  • 改寫 docker-compose.yml,置換 volume path
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    version: "3"
    services:
    database:
    image: postgres:11.6-alpine
    environment:
    - POSTGRES_USER=username
    - POSTGRES_PASSWORD=password
    - POSTGRES_DB=codimd
    ports:
    - "5432:5432"
    volumes:
    - /c/terri/mount/var/lib/postgresql/data:/var/lib/postgresql/data
    restart: always
    codimd:
    image: nabo.codimd.dev/hackmdio/hackmd:2.2.0
    environment:
    - CMD_DB_URL=postgres://username:password@database/codimd
    - CMD_USECDN=false
    depends_on:
    - database
    ports:
    - "3000:3000"
    volumes:
    - /c/terri/mount/var/app/public/codimd/uploads:/home/hackmd/app/public/uploads
    restart: always