0

I want to recreate something like this...

var CA, AZ, TN = 'user1';

With a returned...

<?php 
$phpstring = $wpdb->get_row("SELECT value FROM user_data WHERE user_id = 5");
?>    
var state_phpstring = <?php echo $phpstring ?>; 

And have the 'user1' string added to each (CA, AZ and TN) just as it would if I wrote it in.

Is there a way to do this without creating a loop, by just writing the code similar to this...

var <?php echo $phpstring ?> = 'user1';
3
  • im not sure it is a good thing to add this type of dynamic variables. Commented Nov 20, 2012 at 1:02
  • I don't see why this would not work. But how would you know what variables to use in the rest of your javascript? Commented Nov 20, 2012 at 1:02
  • I think he plan to change some of them later to keep all steps of main variable this may be useful Commented Nov 20, 2012 at 1:09

2 Answers 2

1

Your variable with some checks ...

<?php 
$phpstring = $wpdb->get_row("SELECT value FROM user_data WHERE user_id = 5");
?>    
var state_phpstring = <?php echo (is_array($phpstring)) ? implode(',',$phpstring):'null'; ?>; 

Edit : You can use this variable in your sample code at line

var <?php echo $phpstring ?> = 'user1';

But each record you get from db will be assigned to the same properties which doesn't make a sense you 'd better store all property datas in mysql and fetch them too and assign them later... and btw, now, after you tell me the main goal you'd like to achieve i would like to tell you these ;

To store different properties of a main variable (an individual here in your sample) you'd better use arrays. Like this;

user1 = new Array();
user1["state"] = "CA";
user1["country"] = "USA";
//and so on...

To use sample php above for this usage you can try something like this;

<?php 
$phpstring = $wpdb->get_row("SELECT username,user_state,property2,property3 FROM user_data WHERE user_id = 5");
// make sure $phpstring is an array and has item in it here
// you can also assign more than one person data with foreach usage
foreach($phpstring as $individuals){
    echo $individuals["username"]." = new Array();";
    echo $individuals["username"].'["state"] = "'.$individuals["user_state"].'";';
    echo $individuals["username"].'["property2"] = "'.$individuals["property2"].'";';
    echo $individuals["property2"].'["property3"] = "'.$individuals["property2"].'";';
}
?>

Note that this is just a sample and i don't know how your phpstring returns (associative or not etc)

As a last note ; Indeed using object oriented programming which treats variables as a realworld object (for instance a person) is the best approach but this requires understanding OOP logic and usage so in short term arrays will do what you want simpler.

Sign up to request clarification or add additional context in comments.

2 Comments

this gives me a string, but does not make it individual variables. My goal was to declare each variable (CA, AZ, TN) with the same string (user1) in one line. That way, if there are 1 or 100 different variables, it can work. Otherwise I will just do a for each.
For example I want them to be var CA = 'user1'; AZ = 'user1'; and so on. These will be states and zipcodes associated with a users URL they are assigned to. The end result will be somewebsite.com/user1 ...
0
var <?php echo implode( ', ', array( 'CA', 'AZ', 'TN')); ?> = 'user1';

Although I'm not sure if that will yield the desired results in JS. It will produce the output you're looking for though:

var CA, AZ, TN = 'user1';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.