Jump to content

Can you "eval" a word?


pault

Recommended Posts

This is difficult to explain but here goes.

 

Imagine I have several variables called $field1, $field2, $field3 (not the best way to store things I know but that's the way it is - it's actually to do with columns in a MySQL database) and I want to loop through them and put them into an array. Ideally I'd want something like this:

 

for ($j = 1; $j < 4; ++$j) {

  $arrayvar[$j] = eval('$field' . $j);

}

 

so that the loop counter was added to the word $field to create $field1, $field2 and each line became the equivalent of $arrayvar[1]=$field1, thus automating the procedure.

 

Eval works but you have to do it on the whole line:

eval('$arrayvar'.[$j]. '=$field'.[$j].';');

 

So is there any way of using a sort of eval for just part of a line?

 

Hope you understand and thanks for any advice.

 

Link to comment
Share on other sites

How are your original $field1, $field2, $field3,.. variables getting set? Having code that sets them, then having more code to put them into an array is doubling the amount of code, processing time, and memory needed. It's often easily possible to directly get them into an array when they are created.

Link to comment
Share on other sites

They are actually columns in a MySQl database. It is a 'globals' table and originally there were only two so I called them code1 and code2. Now they have grown in number and it would be better to separate them into a different table but I can't face all the re-writing at the moment.

Link to comment
Share on other sites

Eg. $var[$j] = $row[$j+x];

 

Nop, that's still too much unnecessary code. What's wrong with the array: $row (or just use your final name when you fetch it, something like - $settings = mysql_fetch_assoc($result);)

 

At this point it, without seeing your query statement and what you are trying to get as an end result, cannot specifically help you more than to mention the concept and that every time we see someone with a series of numbered variables that their code is overly complex for what is being accomplished.

Link to comment
Share on other sites

I couldn't just use the whole returned row because there are other columns as well as the numbered ones (forgot to mention that) so I would have to filter them out. But your answers have given me enough to sort the problem  out and I've learnt a few things today.

 

Thanks to you both.

Link to comment
Share on other sites

If the point of this specific code is to get something like global settings (maybe someones multipliers in a RPG...), your query should just retrieve the columns you want and you would be done. In programming, the best method of accomplishing something generally takes knowing what you are actually doing, which is why just showing a few lines of your code out of context rarely results in the best solution.

 

I'm not trying to drag this out to give you a hard time. If what you are trying to create is a web application that will some day need to perform its best to handle a large number of visitors at the same time, having line after line of code that does something to data, only to be followed by more code that undoes what was just done, is something you want to nip in the bud as soon as possible in your coding.

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.