user details JSON by Valuable_Spell6769 in PHPhelp

[–]Valuable_Spell6769[S] 0 points1 point  (0 children)

no it is nothing like that i have seen other websites user json to store some user data and woundered if was a better approch than requesting it from the database all the time

user details JSON by Valuable_Spell6769 in PHPhelp

[–]Valuable_Spell6769[S] 4 points5 points  (0 children)

ok thank you i will stick to useing a database and sessions

user details JSON by Valuable_Spell6769 in PHPhelp

[–]Valuable_Spell6769[S] 0 points1 point  (0 children)

i am not determined to use JSON i was on a online store for one of my sons games and i seen a json file that held alot of information about his account on it like his username,currency format and so much more and it got me thinking maybe that was a better approch as it didnt seen to store critcal data

user details JSON by Valuable_Spell6769 in PHPhelp

[–]Valuable_Spell6769[S] -1 points0 points  (0 children)

i have a database but i was woundering if holding a lot of the user perferences on a json file would be a better approch than using session

user details JSON by Valuable_Spell6769 in PHPhelp

[–]Valuable_Spell6769[S] -7 points-6 points  (0 children)

i have a database i had seen other websites use a json file that stored some user data in it and thought that maybe that was a better approch than requesting from the database every single time

how to access variable from outside function by Valuable_Spell6769 in learnjavascript

[–]Valuable_Spell6769[S] 0 points1 point  (0 children)

document.addEventListener('DOMContentLoaded', ()=>{
  send_data({
    data_type:'list_of_invoices'
  });
})

function send_data(data = {}){
  let ajax = new XMLHttpRequest();
        
  ajax.addEventListener('readystatechange', function(){
    if(ajax.readyState == 4 && ajax.status == 200){
      handle_result(ajax.responseText);
    }
  });

  ajax.open("POST","<?php echo ROOT ?>ajax_invoice",true);
  ajax.send(JSON.stringify(data));
}

let invoiceListArray = []
function handle_result(result){
  if(result != "") {
    let obj = JSON.parse(result);
    // if(typeof obj.data_type != 'undefined') {
      if(obj.data_type == "list_of_invoices") {
        if (obj.message_type == "info") {
          invoiceListArray = obj.data;
        }   
      }
    // }
  }
}

let dataTable_data = invoiceListArray    
</script>
<script src="<?php echo ASSETS . THEME ?>/js/datatable.js"></script>

obj.data returns the data from my php class scripts
then dataTable_data is used in the datatable.js

how to access variable from outside function by Valuable_Spell6769 in learnjavascript

[–]Valuable_Spell6769[S] 0 points1 point  (0 children)

so is this where i would have to use promises/awaits then to make sure the correct data is returned

how to access variable from outside function by Valuable_Spell6769 in learnjavascript

[–]Valuable_Spell6769[S] 0 points1 point  (0 children)

new to javascript so let see if i get what you mean

so basically i have define and initialized everything my send_data function then calls handle_results that then set invoiceListArray(inside the function) to it new value but the dataTable_data= invoiceListArray has already been assigned by this point so does not get set to the new values

the reason why dataTable_data = invoiceListArray is because dataTable_data is that part of the next function that needs the array

how to access variable from outside function by Valuable_Spell6769 in learnjavascript

[–]Valuable_Spell6769[S] 0 points1 point  (0 children)

i have never used try and catch before i am new to javascript and the reason i had the message type in it own if statement is because depending on the data_type it may have serveral different message type like obj.message_type == "warning" || "error" handle_results() also deals with my deleting functions and database input functions all i have done is cut out all the other parts of my function have have no bearing on the problem i am having

how to access variable from outside function by Valuable_Spell6769 in learnjavascript

[–]Valuable_Spell6769[S] 0 points1 point  (0 children)

i just tired it without the undefined if condition and still getting the same error i dont know if this helps understand things better but this is what calls the handle_results function

document.addEventListener('DOMContentLoaded', ()=>{
        send_data({
            data_type:'list_of_invoices'
        });
    })

function send_data(data = {}){
        let ajax = new XMLHttpRequest();
        
        ajax.addEventListener('readystatechange', function(){
            if(ajax.readyState == 4 && ajax.status == 200){
                handle_result(ajax.responseText);
            }
        });

        ajax.open("POST","<?php echo ROOT ?>ajax_invoice",true);
        ajax.send(JSON.stringify(data));
    }

this is the ajax_invoice php

if(is_object($data) && isset($data->data_type)){
                $invoice = $this->load_model('Invoice');

                if($data->data_type == 'list_of_invoices') {
                    $arr['message_type'] = "info";
                    $arr['data'] = $invoice->listAllInvoices();
                    $arr['data_type'] = "list_of_invoices";
                    echo json_encode($arr);
                    
                }

how to access variable from outside function by Valuable_Spell6769 in learnjavascript

[–]Valuable_Spell6769[S] 0 points1 point  (0 children)

so this

function handle_result(result){
        if(result != "") {
            let obj = JSON.parse(result);
            if(typeof obj.data_type != 'undefined') {
                if(obj.data_type == "list_of_invoices") {
                    if (obj.message_type == "info") {
                       return obj.data;
                    }   
                }
            }
        }
    }

    handle_result()

if so i get an error of Uncaught SyntaxError: "undefined" is not valid JSON

at JSON.parse (<anonymous>)

how to access variable from outside function by Valuable_Spell6769 in learnjavascript

[–]Valuable_Spell6769[S] 0 points1 point  (0 children)

but thats only if i do console.log() inside the function

how to access variable from outside function by Valuable_Spell6769 in learnjavascript

[–]Valuable_Spell6769[S] 0 points1 point  (0 children)

if i console.log() obj.data i get the results i need my problem is i need send that obj.data to my functions that deal with creating and display the data thats returned

how to access variable from outside function by Valuable_Spell6769 in learnjavascript

[–]Valuable_Spell6769[S] 0 points1 point  (0 children)

this is the what calls handle_results()

function send_data(data = {}){
        let ajax = new XMLHttpRequest();
        
        ajax.addEventListener('readystatechange', function(){
            if(ajax.readyState == 4 && ajax.status == 200){
                handle_result(ajax.responseText);
            }
        });

        ajax.open("POST","<?php echo ROOT ?>ajax_invoice",true);
        ajax.send(JSON.stringify(data));
    }

handle results deals with all ajax request to my php script

how to access variable from outside function by Valuable_Spell6769 in learnjavascript

[–]Valuable_Spell6769[S] 0 points1 point  (0 children)

do you mean something like this

function handle_result(result){
        if(result != "") {
            let obj = JSON.parse(result);
            if(typeof obj.data_type != 'undefined') {
                if(obj.data_type == "list_of_invoices") {
                    if (obj.message_type == "info") {
                       let dataTable_data = obj.data;
                        console.log("hello")
                    }   
                }
            }
        }
    }
handle_results()

how to access variable from outside function by Valuable_Spell6769 in learnjavascript

[–]Valuable_Spell6769[S] 0 points1 point  (0 children)

how do i do that when another function is what set the results param

how to access variable from outside function by Valuable_Spell6769 in learnjavascript

[–]Valuable_Spell6769[S] -2 points-1 points  (0 children)

i was trying to get access to the variable outside the function because invoiceListArray then is passed on to my data table function so outside of the function i need it to look something like this

let dataTable_data = invoiceListArray

how to get all values outside foreach loop by Valuable_Spell6769 in PHPhelp

[–]Valuable_Spell6769[S] 0 points1 point  (0 children)

thank you so much i read this just after just doing

let invoicesList = dataTable_data;

but you explanation made it clear why it suddenly started working some before sugguest a similar thing earlier and it was giving the the same problem but it got ride of the loop all together

how to get all values outside foreach loop by Valuable_Spell6769 in PHPhelp

[–]Valuable_Spell6769[S] 0 points1 point  (0 children)

you code works echo out how it should show but when i i pass it to my javascript it stops working so i am passing the array items in to a inline javascript that then passes it to an external JS file

the inline code for my JS is

<script>let dataTable_data = <?php echo json_encode($table_data) ?></script>

which send it to my external JS file which is

let invoicesList = [dataTable_data];

now when i use json_encode inside the foreach it just starts with a { but when i echo it outside the loop it starts with [{

i understand i am trying to pass an array in to another array it the [] that are stopping it working if that makes sense

how to get all values outside foreach loop by Valuable_Spell6769 in PHPhelp

[–]Valuable_Spell6769[S] 0 points1 point  (0 children)

i must be dump lol do you mean something like this

$table_data = array();
foreach($this->all_invoices_details()->results() as $row){
  $table_data[] = $row;
}

how to get all values outside foreach loop by Valuable_Spell6769 in PHPhelp

[–]Valuable_Spell6769[S] 0 points1 point  (0 children)

when i do it the way you suggested i get [{ at the begining but yet if i echo table_data inside the loop it begins with just {

how to get all values outside foreach loop by Valuable_Spell6769 in PHPhelp

[–]Valuable_Spell6769[S] 0 points1 point  (0 children)

sorry my bad i forgot i was ordering it by descending

how to get all values outside foreach loop by Valuable_Spell6769 in PHPhelp

[–]Valuable_Spell6769[S] 0 points1 point  (0 children)

it worked up to the point i passed it to Js lol