Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3787116
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:47:25+00:00 2026-05-19T11:47:25+00:00

This question is a bit messy, so I’ll try to stick to the basics

  • 0

This question is a bit messy, so I’ll try to stick to the basics of my problem. When users logs into my site, their site preferences are fetched from the database. The preferences could, for instance, represent a certain visual layout, customized by the user himself/herself. Let’s say the user can choose to show or hide 3 “info”-boxes that are, by default, shown on the webpage. The preferences are stored like this in the db:

----------------------------------
| user_id | box_1 | box_2 | box3 |
----------------------------------
| 23      | true  | false | true |
----------------------------------

This data has to be fed into the javascript on the page, in order for the user to get their personal visual layout. So I would do something like this:

<head, title, jsscripts meta tags and what have you>
<script type="text/javascript">
<?php

//fetch data from db here
$array = fetchfunction();

echo 'preference_box_1 = '.$array['box_1'].';' // this would be set to true
echo 'preference_box_2 = '.$array['box_2'].';' // this would be set to false
echo 'preference_box_3 = '.$array['box_3'].';' // this would be set to true

?>
</script>

Is this the best solution to implement? Or is there a more elegant way? My concerns are also that this would be messy when a user has 100+ preferences.

EDIT: I’m sorry, I don’t think I wrote my question clearly enough. I’m aware of PHP’s JSON encoder, but the PHP code is not my concern here. What I’m wondering is if this is the best way to inject external data into javascript? Should it be done otherwise?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T11:47:26+00:00Added an answer on May 19, 2026 at 11:47 am

    Personally I would consider transferring your data via JSON. It’s lightweight and far more readable.

    http://ditio.net/2008/07/17/php-json-and-javascript-usage/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is motivated by something I've lately started to see a bit too
I know this question has been asked a bit before. But looking around I
let me tell you a bit about where this question came from. I have
First, yes I know about this question , but I'm looking for a bit
This is a bit of a lazyweb question but you get the rep so
This is a bit of an open question but I would really like to
This may be a bit of a nooby question, I have been trying to
This might be a bit of a silly question but; If I have two
This might sound like a little bit of a crazy question, but how can
This is my very first question so I am a bit nervous about it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.