Jump to content

Possibly insane request


jerryrowe

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.