Jump to content

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
https://forums.phpfreaks.com/topic/252304-can-you-eval-a-word/
Share on other sites

Hi

 

Think what you want is something like this:-

 

http://php.net/manual/en/language.variables.variable.php

 

Not tested but something like this:-

 

<?php

for ($j = 1; $j < 4; ++$j) 
{
$fieldname = $field.$j;
$arrayvar[$j] = $$fieldname;
}

?>

 

All the best

 

Keith

Link to comment
https://forums.phpfreaks.com/topic/252304-can-you-eval-a-word/#findComment-1293438
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
https://forums.phpfreaks.com/topic/252304-can-you-eval-a-word/#findComment-1293448
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
https://forums.phpfreaks.com/topic/252304-can-you-eval-a-word/#findComment-1293476
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
https://forums.phpfreaks.com/topic/252304-can-you-eval-a-word/#findComment-1293483
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
https://forums.phpfreaks.com/topic/252304-can-you-eval-a-word/#findComment-1293488
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
https://forums.phpfreaks.com/topic/252304-can-you-eval-a-word/#findComment-1293492
Share on other sites

Hi

 

Just realised the code I pasted earlier was garbage and might have confused you.

 

Something more like this:-

 

<?php
$j=1;
if ($row = mysql_fetch_array($rs))
{
for($j = 1;$j<=10;$j++)
{
	$arrayvar[$j] = $row['field'.$j];
}
}
?>

 

All the best

 

Keith

Link to comment
https://forums.phpfreaks.com/topic/252304-can-you-eval-a-word/#findComment-1293498
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.