From 3f375f41a0d8176396da0a5988a9db06383e1636 Mon Sep 17 00:00:00 2001 From: Maksymilian Jopek Date: Thu, 24 Mar 2022 14:31:15 +0100 Subject: Option to use or two xpaths ( speed ) --- index.php | 19 +++++++++---------- src/main.ts | 7 +++---- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/index.php b/index.php index a67bd26..ee7431c 100644 --- a/index.php +++ b/index.php @@ -3,19 +3,18 @@ header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Headers: Content-Type"); $method = $_SERVER['REQUEST_METHOD']; $xml = simplexml_load_file("./terc.xml"); -// var_dump($xml); if ($method === "GET") { - // $result = $xml->xpath('//row/NAZWA_DOD[text()="województwo"]/ancestor::*/NAZWA/text()'); - $result = $xml->xpath('//NAZWA_DOD[text()="województwo"]/ancestor::*/NAZWA/text() | //NAZWA_DOD[text()="województwo"]/ancestor::*/WOJ/text()'); + $result = $xml->xpath('//NAZWA_DOD[text()="województwo"]/ancestor::*/NAZWA/text()'); } else if ($method === "POST") { $body = file_get_contents('php://input'); - // echo $body; - // echo '//row/NAZWA[text()="' . $body . '"]/ancestor::*/NAZWA_DOD[contains(text(),"miejs")]/ancestor::*/NAZWA/text()'; - $result = $xml->xpath('//row/WOJ[text()="' . $body . '"]/ancestor::*/NAZWA_DOD[contains(text(),"miejs")]/ancestor::*/NAZWA/text()'); -} else { - $result = $xml->xpath('//row/NAZWA[text()="ŁÓDZKIE"]/ancestor::*/NAZWA_DOD[contains(text(),"miejs")]/ancestor::*/NAZWA/text()'); - $result = $xml->xpath('//WOJ[text()="10"]/ancestor::*/NAZWA_DOD[contains(text(),"miejs")]/ancestor::*/NAZWA/text()'); - $result = $xml->xpath('//NAZWA_DOD[text()="województwo"]/ancestor::*/NAZWA/text() | //NAZWA_DOD[text()="województwo"]/ancestor::*/WOJ/text()'); + $one_xpath = true; + if (!$one_xpath) { + $id = $xml->xpath('//row/NAZWA[text()="' . $body . '"]/ancestor::*/WOJ'); + $id = "{$id[0]}"; + $result = $xml->xpath('//WOJ[text()=' . $id . ']/ancestor::*/NAZWA_DOD[contains(text(),"miejs")]/ancestor::*/NAZWA/text()'); + } else { + $result = $xml->xpath('//WOJ[text()=//row/NAZWA[text()="' . $body . '"]/ancestor::*/WOJ]/ancestor::*/NAZWA_DOD[contains(text(),"miejs")]/ancestor::*/NAZWA/text()'); + } } $result = array_map(function ($x) { return "$x"; diff --git a/src/main.ts b/src/main.ts index 7ecd320..2406f04 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,11 +5,11 @@ const URL = "http://localhost:8000/index.php"; (async () => { const voivs = await (await fetch(URL)).json() as string[] const tbody = document.getElementById('tds-here') as HTMLTableSectionElement; - for (let i = 0; i < voivs.length; i += 2) { + for (const v of voivs) { const tr = document.createElement("tr") const td = document.createElement("td") - td.innerText = voivs[i + 1] - td.onclick = () => showCities(voivs[i + 1], voivs[i]); + td.innerText = v + td.onclick = () => showCities(v, v); tr.appendChild(td) tbody.appendChild(tr) } @@ -20,7 +20,6 @@ async function showCities(n: string, id: string) { method: "POST", body: id, })).json() as string[] - // console.log(res) const nh = document.getElementById("name-here") as HTMLTableRowElement const vt = document.getElementById("vtds-here") as HTMLTableSectionElement vt.innerHTML = '' -- cgit v1.3.1