0

I'm using a function that returns a Fetched Array from a Database ( PDO::fetch(PDO::FETCH_ASSOC) ).

this function returns A LOT OF COLUMNS and when I am editing all those columns in a Form, obviously I need the fields to be populate with the current data.

Code (modified for the question):

//returns something like $owner["name"], $owner["lastname"], 
//$owner["phone1"] ... and 53 fields more.

$owner = controllerGetOnwer($ownerID);

//So I used a foreach to create VARIABLE VARIABLES
foreach($owner as $key=>$value) {
     ${$key} = $value; //you get something like $name = <what is in that column>
}

I am gonna use this form in a lot of pages, not only for owners, but also for customers, administrators, and so on... That's why I decided to put the form in a function inside a static class I already used to 'render' all the HTML I will use a lot of times (such as Headers, logos, menus, etc)

//This is inside the HTMLRenderClass

renderTheEditForm() {

?>
<form>
    Name: *<br/>
    <input type="text" name="personalname" value="<?php if(isset($name)) echo $name; ?>"/><br/><br/>
    Last Name: *<br/>
    <input type="text" name="personallastname" value="<?php if(isset($lastname)) echo $lastname; ?>"/><br/><br/>
    Phone Number 1:<br/>
    <input type="text" name="personalphone1" value="<?php if(isset($phone1)) echo $phone1; ?>"/><br/><br/>


<!-- AND 53 FIELDS MORE -->


</form>    
<?PHP
}

All the variables that you see inside the VALUE attribute are the same as the ones that are being created dinamically in the FOREACH. When I paste the form HTML code below the Foreach, I can see the data being loaded, but when I use the Function from the HTMLRenderClass I get nothing... I haven't been able to find the reason.

I hope I explained well, thanks beforehand!

5
  • 1
    Would [extract()](php.net/manual/en/function.extract.php) not work? Commented Jul 16, 2013 at 3:48
  • Why? Why create a level of indirection? Why not just use the original array values? Commented Jul 16, 2013 at 3:49
  • With 53 fields I'd just think of a different approach... How about storing all data in an array by name, value and label and generating the HTML with PHP in a loop? Commented Jul 16, 2013 at 3:50
  • your idea is wrong ,how are you assigning variables inside a function which are not defined ? Commented Jul 16, 2013 at 3:50
  • I agree with Arun. You must define those variables or pass them into the function Commented Jul 16, 2013 at 3:54

1 Answer 1

1

It appears that you need to define your variables inside the function or pass them into the function.

Sign up to request clarification or add additional context in comments.

4 Comments

as clrockwell stated in the comments using extract is a one line replacement for the foreach loop (god forbid if one of the keys in the array is "owner")
It worked... I declared the function that renders the FORM this way: ..static function renderTheForm($currentValues)
Try replacing foreach($currentvalues as $key=>$value) { ${$key} = $value; } with extract($currentvalues);
Using Extract() instead of a foreach loop works perfect... I just didn't know that Function.. Thank you!

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.