Jump to content

[SOLVED] Eval problem


ejaboneta

Recommended Posts

$e = '3';
$h = '4';

$str1 = "\$_POST[value$e][$h]";
eval("\$values = \"'$str1'\";");
echo $values;

 

I'm trying to get is the value of $_POST[3][4]... but the eval is processing the $_POST[3] and leaving the [4]. So what I end up with is Array[4]....

 

Any help?

 

Link to comment
Share on other sites

I'm trying to get is the value of $_POST[3][4]...

Your code is trying a little too hard to be clever. Simplify.

 

I may be missing some details that you post doesn't make clear but the following might work for you.

$e = 3;
$h = 4;
$values = $_POST[$e][$h];

Link to comment
Share on other sites

There is no reason to use eval() unless you are dynamically writing php code in a string someplace. All you are doing is evaluating array index names -

 

$e = '3';
$h = '4';
$values = $_POST['value' . $e][$h];

 

Edit: And if you lose the word 'value' from the form field, you can do what salathe posted above.

 

Link to comment
Share on other sites

Well the post comes from a dynamic amount of values from a dynamic amount of fields. I was trying to keep it simple and just ask for what i needed but maybe I should be more detailed.

 

 

I'm trying to allow my users to upload CSVs into our database. The problem is that the CSVs come from different places and so the field order and amount may be different. So I made a script that asks the user which columns in the CSV go into which column in the database. Then the next page, containing this code, formats the values to be inserted into the database. 

 

 

Hopefully this helps to be more clear....

 


### FOR EACH ROW ###
  for($h=1; $h<$rowcount; $h++) {    
    $values = $values . "$comma(";
    ##### GET VALUES #####
    for($e=1; $e<$fieldcount; $e++) {
      $str1 = "\$_POST[$e][$h]";

      eval("\$values = \$values . \"'$comma1$str1'\";");
      $comma1 = ',';
    }
    $values = $values . ")";
  $comma = ',';
  }

 

the result should look like:

('Joe','Smith','1234 Main St'),

('Sally','Smith','431 North St')

 

but I keep getting:

('Array[1]','Array[1]','Array[1]')

('Array[2]','Array[2]','Array[2]')

 

 

 

Link to comment
Share on other sites

try

<?php
$values = array();
for($h=1; $h<$rowcount; $h++) {    
    $val = array();
    ##### GET VALUES #####
    for($e=1; $e<$fieldcount; $e++) {
      $val[] = $_POST[$e][$h];
    }
    $values[] = "('". implode("', '", $val) . "')";
}
$values = implode(",\n",$values);
?>

Link to comment
Share on other sites

try

<?php
$values = array();
for($h=1; $h<$rowcount; $h++) {    
    $val = array();
    ##### GET VALUES #####
    for($e=1; $e<$fieldcount; $e++) {
      $val[] = $_POST[$e][$h];
    }
    $values[] = "('". implode("', '", $val) . "')";
}
$values = implode(",\n",$values);
?>

 

Perfect! For what its worth, I found a workarond by using eval() to change $_POST[3] to $post3, and then another eval on $post3[4]. But that works better! Thanks!

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.