簡體   English   中英

創建一個從數據源讀取文本文件的函數,並將其轉換為php的目標文件

[英]Creating a function that reads a text file from a data-feed and turning it into an object file for php

我正在創建一個商店網站,但是我收到的數據饋送是一種用豎杠而不是逗號分隔的格式。 我希望能夠從文本文件中讀取內容,以將內容顯示到網頁中,並允許客戶搜索商品。

示例行:

710734240|8mm - Men's Freemason Ring / Masonic Ring - Gold and Black Inlay Tungsten Ring Comfort Fit|73628|

我嘗試循環處理分離的項目,然后將每個項目放入自己的數組中。

        const my_data = 'test_data.txt';

        async function getData() {

            const response = await fetch(my_data);
            const data = await response.text();
            //console.log(data);

            const rows = data.split('\n').splice(1);
            rows.forEach(elt => {
                const row = elt.split('|');

                row.forEach(item => {
                    const items = item.split(',');
                    //    const filtered = items.filter(function (el){
                    //    return el != ""; 
                    const lens = items.length;
                    //document.getElementById('data').innerHTML = rows + "<br>";

                    var goods = {
                        ProductId: row[0],
                        Name: row[1],
                        MechantId: row[2],
                        Mechant: row[3],
                        link: row[4],
                        thumbnail: row[5],
                        bigImage: row[6],
                        Price: row[7],
                        RetailPrice: row[8],
                        mainCat: row[9],
                        subCat: row[10],
                        Description: row[11],

                    };
                    document.getElementById('data').innerHTML = goods.Description + "<br>";
                });


                console.log(row);
                //console.log(lens);



            });
        }
        getData();

也許您想從這里開始:

 let list = {}; const rows = `710734240|8mm - Men's Freemason Ring / Masonic Ring - Gold and Black Inlay Tungsten Ring Comfort Fit|73628| 710734241|9mm - Men's Freemason Ring / Masonic Ring - Gold and Black Inlay Tungsten Ring Comfort Fit|73628| 710734242|10mm - Men's Freemason Ring / Masonic Ring - Gold and Black Inlay Tungsten Ring Comfort Fit|73628|` .split("\\n") .forEach(row => { // destructing the rows curtesy of Code Maniac ( ([ProductId, Description, subCat]) => list[ProductId] = { Description, subCat } )( row.split("|") ) }) // displaying example let dl = document.createElement("dl"); let content = [] Object.keys(list).forEach( k => content.push(`<dt>${k}</dt><dd>${list[k].Description}</dd>`) ) dl.innerHTML = content.join(""); document.getElementById("container").appendChild(dl) 
 dl { display: flex; flex-flow: row wrap; border: solid #333; border-width: 1px 1px 0 0; } dt { flex-basis: 10%; padding: 2px 4px; background: #333; text-align: right; color: #fff; } dd { flex-basis: 80%; flex-grow: 1; margin: 0; padding: 2px 4px; border-bottom: 1px solid #333; } 
 <div id="container"></div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM