How to loop within a variable in Javascript?

Asked

Viewed 194 times

0

I have a page index.php. In it has a loop which collects row by row from a mysql database table and inserts into variables that are sent to the file dashboard.js where the code is below.

var visitorsData = {
[CODE_PAIS]: TOTAL_VISIT_PAIS, //Brazil
};

What I want is that variable visitorsData of the archive dashboard.js store the same amount of line and information as in my table. NOTE: The number of rows in the database is not fixed.

  • Is this Dashboard.js file being generated by PHP, or is it a static file? If it is being generated, how is the code generating it?

  • It is static! Caught only a few variables from php file.

  • 1

    So I don’t get it. You say "What I want is that variable visitorsData of the archive dashboard.js storehouse the same amount of row and information you have in my table" and "The number of lines in the database is not fixed", but if it is a static file, when the database changes and that file does not change the quantities will be different, no?

  • This implies rewriting the entire content ?

  • All right, here’s the deal. The only information the Dashboard.js file takes is the global variables CODE_PAIS and TOTAL_VISIT_PAIS that are generated in the index.php file. But with each loop that is made in the index.php file the information of these variables changes. So I wanted to know if there is any way to collect all the information generated in the index.php loop and not the last information stored in these variables. The purpose is to make the line '[CODE_PAIS]: TOTAL_VISIT_PAIS, //Brazil' of the visitorsData variable repeat the same amount of times and with the same loop information as the index.php file

  • 1

    So he’s dynamic, not static! (if it changes - even a little bit - it is not static) I had asked you to post the code that generates this variable, but forget it, what you are trying to do does not seem practical. PHP is (or at least should be) stateless. If you really need two different files (which implies two different requests) to relate in some way, the way is to save some data in the session when generating the index.php and then take that same data when generating the dashboard.js. To ensure that an old cache copy does not come, create...

  • 1

    ...an element <script type="text/javascript" src="dashboard.js?dados=ID"></script> in his index.php, where ID is the key responsible for taking the data previously stored in the session (may have better forms, I don’t have much experience with PHP so I’m only talking about what I know). But to reiterate: if you can avoid doing this, avoid it! You could not instead put Javascript inline on its own index.php, for example?

  • 1

    Put in your code... the more you show us, the more we can help you. So you’re wasting your time and creating questions and accepting answers that don’t solve the problem.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.