1

The while loop gets DB rows as such:

    while($row = $result->fetch_assoc()){
        $metadata_field74[] = $row['field74'];
        $metadata_field75[] = $row['field75'];
        $metadata_field76[] = $row['field76'];
        $metadata_field77[] = $row['field77'];
    }

Those rows are based on another DB table. Basically this loop should get the rows as based on another array of values received from a DB query.

Those values (field74, field75, etc) are stored in a array $metadata_id_basic[]. I get the values from that array like this and insert into the while loop:

    while($row = $result->fetch_assoc()){

        foreach ($metadata_id_basic as $value){
            $ref = '$metadata_'.$value.'[] = $row[\''.$value.'\'];';
            echo $ref;
        }
    }

However the array of rows is not seen. If I print $ref out it looks like:

        $metadata_field74[] = $row['field74'];
        $metadata_field75[] = $row['field75'];
        $metadata_field76[] = $row['field76'];
        $metadata_field77[] = $row['field77'];

So why does the while loop not read it correctly?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • 1
    this line `$ref = '$metadata_'.$value.'[] = $row[\''.$value.'\'];';` - it's a quotes issue. Variables only get parsed in double quotes, or concatenated. So this `$metadata_` never gets parsed. So try removing the quotes `$metadata_.$value. .....` or wrap the declaration in double quotes. You may need to do a few other modifications, but that should get you started. – Funk Forty Niner Jul 29 '15 at 14:48
  • Where is this being set `$metadata_id_basic` where are you referencing `$row` how does that `$row` relate to `$metadata_id_basic` ? `'$metadata_'.$value.'[] = $row[\''.$value.'\'];';` what is that all about. – ArtisticPhoenix Jul 29 '15 at 14:48

3 Answers3

1

I don't know what that is trying to do .

  '$metadata_'.$value.'[] = $row[\''.$value.'\'];';

But, maybe it should be something like this?

$metadata[$value][] = $row[$value];

You should initialize $metadata = array() somewhere though. If you really want this

 '$metadata_'.$value.'[]

I would suggest doing it this way.

  ${'metadata_'.$value}[] = .....

Then it will be an actual variable you can use normally. What you have right now is just a string or put another way, just words..

That might be what you want in that case you'll still want this

   $ref =  '$metadata_'.$value.'[] = '.$row[$value];

I don't know what this means for example So why does the while loop not read it correctly? what is correctly?

Also, it's hard to say without knowing what $metadata_id_basic is or what you are trying to get as the end result, but if you just want the keys of the $row you can do this without the external dependency.

  foreach ($row as $key => $value){

For example

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
0

The line

$ref = '$metadata_'.$value.'[] = $row[\''.$value.'\'];';

will put together a string and not initialize variables.

Take a look at : Multidimensional Arrays

Tobias
  • 653
  • 4
  • 12
0

The foreach loop is simply echoing out a string, not populating a variable with the values from the database.

Also, I'm not sure from your code if you want the data in an array or multiple variables, so here are two versions

To put the data into multiple separate variables:

while($row = $result->fetch_assoc()){

    foreach ($metadata_id_basic as $value){
        $var_name = metadata_' . $value;
        $$var_name = $row[$value];
        echo '$' . $var_name . ' : ' . $$var_name;
    }
}

This should create the $metadata_XXXXXvariables.

To put your data into an array, indexed by $metadata_id_basic:

$metadata = array();
while($row = $result->fetch_assoc()){

    foreach ($metadata_id_basic as $value){
        $metadata[$value] = $row[$value];
    }
}

print_r($metadata);

The echo and print_r statments are in there to show you what has been created.

theHands
  • 373
  • 1
  • 3
  • 8