1

What is the right way for doing this?
It gives me an error:

$lingue       = array('IT','EN','FR');
$item         = new stdClass();
$item->IDCat  =  1;

foreach($lingue as $l){
    $item->Desc_{$l} = trim(addslashes($_POST['Desc_'.$l]);     
}
3
  • "6 new answers have been posted".. lol sure catch the easy parse errors guys :p Commented Apr 19, 2011 at 10:31
  • 2
    missing ) in trim(addslashes($_POST['Desc_'.$l]); Commented Apr 19, 2011 at 10:32
  • as what was always stressed indentation and formatting matters Commented Apr 19, 2011 at 10:33

5 Answers 5

2

You would need to wrap the whole expression into the curly quotes: {"Desc_".$l}

However, seeing as the set of languages is going to be dynamic, consider using an Array for this instead:

$item = new STDClass();
$item->Desc = new Array();

foreach($lingue as $l){
  $item->Desc[$l] = trim(addslashes($_POST["Desc_$l"]));
 }  

echo $item->Desc["IT"]; // outputs the italian description

Additional observations:

Note that if you're going to use these values in a database query, addslashes() is not sufficient protection against SQL injection. Use the string escaping function of the SQL library you are using.

Using $_POST["Desc_xyz"] without checking whether it's set will throw a PHP notice, which you want to avoid. Consider adding a check:

if (!empty($_POST["Desc_$l"]))
 $item->Desc[$l] = trim(addslashes($_POST["Desc_$l"]));
Sign up to request clarification or add additional context in comments.

2 Comments

Or just $item->{"Desc_$l"} and $_POST["Desc_$l"] for short.
Still missing a ) after the trim() call... ;-)
0
$lingue      = array('IT','EN','FR');
$item        = new stdClass();
$item->IDCat =   1;
foreach($lingue as $l){
    $item->{'Desc_'.$l}    = trim(addslashes($_POST['Desc_'.$l]));
}

The dynamic accessor must be enclosed with curly braces and you'r lacking a ) at the end of the trim() call.

Just to highlight the difference between $item->{'Desc_'.$l} = ... and $item->Desc_{$l} = ...:

//...
foreach($lingue as $l){
    $item->{'Desc_'.$l}    = trim(addslashes($_POST['Desc_'.$l]));
}
print_r($item);
// outputs:
/*
stdClass Object
(
    [IDCat] => 1
    [Desc_IT] => a
    [Desc_EN] => a
    [Desc_FR] => a
)
*/

while

//...
foreach($lingue as $l){
    $item->Desc_{$l}    = trim(addslashes($_POST['Desc_'.$l]));
}
print_r($item);
// outputs:
/*
stdClass Object
(
    [IDCat] => 1
    [Desc_] => Array
        (
            [IT] => a
            [EN] => a
            [FR] => a
        )

)
*/

This is actually the same as

//...
foreach($lingue as $l){
    $item->Desc_[$l]    = trim(addslashes($_POST['Desc_'.$l]));
}

Seems to be an imprudence in the parser.

2 Comments

Not an imprudence, just the alternative array access syntax (which was mostly used for string offset access, but now is on the way to deprecation.)
@nikic: I knew it was used as a string accessor... Didn't know it was used as an alternative array accessor. Thanks for that info.
0

If your problem is with $item->Desc_{$l} = trim(addslashes($_POST['Desc_'.$l]); try $item->{'Desc_'.$l} = trim(addslashes($_POST['Desc_'.$l]); instead

Comments

0
$varName = 'Desc_'.$l;
$item->$varName = trim(addslashes($_POST['Desc_'.$l]);

Comments

0

Try with:

$item->{'Desc_' . $l} = ....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.