jerryrowe Posted April 16, 2011 Share Posted April 16, 2011 My users have search pages that have dozens of variables. I thought it would be easiest to somehow just jam the whole lot into the database as a huge text string, and then just retrieve that one database entry as a giant string and echo it all to set all the variables in one big chunk, instead of working with an array. The problem is that I don't think it is possible to declare a variable when you are echoing, and this vexes me. For instance, I would LIKE to be able to store this, exactly as you see it, as a database entry: echo $username='MrCool'; echo $superpower='Awesomeness'; But see, those variables have already been declared, and so you wind up actually getting this, instead: MrCoolMrCool AwesomenessAwesomeness So, I am scratching my head, and am not even sure that what I am trying to do is possible. I want to declare variables IN an echo, and I don't think it is possible. I tried doing the $$var and ${$var} thing, but that didn't work, either. So... [*]Am I trying to do the impossible? [*]Is there something *like* what I am trying to do, where I can just store a huge chunk at once and not worry about a complex function? Thanks for reading, and I hope someone can help! Quote Link to comment Share on other sites More sharing options...
ignace Posted April 16, 2011 Share Posted April 16, 2011 Have you tried eval? Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted April 18, 2011 Share Posted April 18, 2011 It would make more sense to have a seperate settings table with individual fields for all the variables that joins with the user table, rather than having a single text field for storing every variable for that user. Setting default values within the database for the variables would have a standard "template" produced for every new user with the ability to update and change after user creation. I just think it makes things much easier to manage in the long term. Quote Link to comment Share on other sites More sharing options...
kickstart Posted April 18, 2011 Share Posted April 18, 2011 Hi You could declare variables in the table field and use eval to execute it, but this is a bit risky as someone could insert some fairly damaging code in there. Think echo $username='MrCool'; would produce 'McCool' irrespective of whether it is assigned a value first. I would be very tempted to split the variables off into a separate table:- Id MasterId FieldName FieldValue 1 1 username MrCool 2 1 superpower Awesomeness Then when your record from the main table is read you then read all the matching records (ie, MasterId) from this table. <?php $MasterId = 1; $somevars = mysql_query("SELECT FieldName, FieldValue FROM VarsTable WHERE MasterId = $MasterId"); while($row = mysql_fetch_array($somevars)) { $$row['FieldName'] = $row['FieldValue']; } ?> That said I wouldn't want to use variable variable names unless 100% necessary. Will likely give code that is very difficult to maintain and shouldn't be necessary. All te best Keith Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.