如何基于登录用户

时间:2018-03-14 22:54:40

标签: javascript php jquery html json

我有一个用户登录的表单,数据以动态方式保存在JSON中。所有这些工作,但我只想显示包含在会话中设置的登录用户的电子邮件ID的JSON数据。 这是创建JSON的脚本: `

var g_objJSON = [];

        /** setJSON - Create JSON object
        * Returns - Nothing
        **/
    function setJSON() {
        //alert($(this).data('session-name'));
            var v_aJSON = [];
            var v_hObject = {};
            var v_hTempHash = {};

            var v_sKey = '<?php echo (isset($_SESSION['userEmail'])) ? $_SESSION['userEmail'] : ''; ?>';
            console.log(v_sKey);
            var x = document.getElementsByClassName('product-description')
            for (var i = 0; i < x.length; i++) {
                x[i].addEventListener("click", function(){

                    var selectedEl = document.querySelector(".selected");
                    if(selectedEl){
                        //selectedEl.classList.remove("selected");
                    }
                    //this.classList.add("selected");
                    alert('in addEventListener: ' + $(this).data('name'));
                    var v_sValue = $(this).data('name');
                    alert('event name : ' + v_sValue);
                    try {
                        v_hObject[v_sKey] = v_sValue;
                        if (g_objJSON == undefined) {
                            v_aJSON.push(v_hObject);
                        } else {
                            v_hTempHash = mergeHashOb(g_objJSON[0], v_hObject);
                            v_aJSON.push(v_hTempHash);
                        }
                        g_objJSON = v_aJSON;
                        alert("Events JSON created!");
                        for (var item in g_objJSON[0]) {
                            console.log("Email: " + item + "\nLocation: " +   g_objJSON[0][item]);
                            $.ajax({
                            url: 'json-2.php',
                            type: 'POST',
                            data: {json: JSON.stringify(g_objJSON)},
                            dataType: 'json'
                        });
                        }

                    } catch (x) {
                            alert(x.message);
                    }
                }, false);;
            }
    }
    /** mergeHashOb - Merge a new JSON object with the global JSON object
               * @prm_hObj - Existing Hash object
               * @prm_hObj2 - New Hash object
               * Returns - A new Hash object contains the merged Hash objects
               **/
               function mergeHashOb(prm_hObj, prm_hObj2) {
                       var v_hObj = {};
                       for (var item in prm_hObj) { 
                               v_hObj[item] = prm_hObj[item]; 
                       }
                       for (var item in prm_hObj2) { 
                               v_hObj[item] = prm_hObj2[item]; 
                       }
                       return v_hObj;
               }

`

JSON-2.PHP: `

<?php
    // let's parse this right away
    $json = json_decode($_POST['json']);

    /* sanity check */
    if ($json != null)
    {
      // parse the file contents to json
      $fileContents = file_get_contents('event_data.json');
      $parsedJson = json_decode($fileContents);

      if ($parsedJson === null) {
        // it's either the file contains bad json or it is empty (tested with 7.1)
        $parsedJson = array();
      }

      // append your new data to the parsed json
      // I'm assuming the $_POST['json'] returns a stringified array, I'll take the first element and append it.
      $parsedJson[] = $json[0];
      // now write to the file
      $file = fopen('event_data.json','w'); // note that we're writing, not appending.
      // write to file the json_encoded $parsedJson
      fwrite($file, json_encode($parsedJson));
      fclose($file);
    }
    else
    {
      // handle the error 
    }
?>

`

event_data.json: [{"charlie@gmail.com":"Finding Accomodation"},{"charlie@gmail.com":"Money Matters"},{"charlie@gmail.com":"Pathways to Deakin"},{"_empty_":"Finding Accomodation"},{"vrishty.garg@deakin.edu.au":"Finding Accomodation"},{"vrishty.garg@deakin.edu.au":"Finding Accomodation"},{"charlie@gmail.com":"Finding Accomodation"}]

所以,我想只显示那些包含登录用户密钥的数组,一次说charlie@gmail.com,而不是以上所有。

创建表格的功能: `

$(document).ready(function(){
  //alert('inside document.ready');
  $.getJSON("event_data.json", function (data) {
    //alert('in each function of getJSON()');

                    var arrItems = [];      // THE ARRAY TO STORE JSON ITEMS.
                    $.each(data, function (index, value) {
                      arrItems.push(value);       // PUSH THE VALUES INSIDE THE ARRAY.
                    });

                    // EXTRACT VALUE FOR TABLE HEADER.
                   var col = [];
                    for (var i = 0; i < arrItems.length; i++) {
                        for (var key in arrItems[i]) {
                            if (col.indexOf(key) === -1) {
                                col.push(key);
                            }
                        }
                    }

                    // CREATE DYNAMIC TABLE.
                    var table = document.createElement("table");
                    //var button = document.createElement("button");

                    // CREATE HTML TABLE HEADER ROW USING THE EXTRACTED HEADERS ABOVE.

                    var tr = table.insertRow(-1);                   // TABLE ROW.

                    for (var i = 0; i < col.length; i++) {
                        var th = document.createElement("th");      // TABLE HEADER.
                        th.innerHTML = col[i];
                        tr.appendChild(th);
                    }

                    // ADD JSON DATA TO THE TABLE AS ROWS.
                    for (var i = 0; i < arrItems.length; i++) {

                        tr = table.insertRow(-1);

                        for (var j = 0; j < col.length; j++) {
                            var tabCell = tr.insertCell(-1);
                            tabCell.innerHTML = arrItems[i][col[j]];
                        }
                    }

                    // FINALLY ADD THE NEWLY CREATED TABLE WITH JSON DATA TO A CONTAINER.
                    var divContainer = document.getElementById("showData");
                    //divContainer.innerHTML = "";
                    divContainer.appendChild(table);
                    //divContainer.appendChild(button);
                });
});

`

0 个答案:

没有答案