1

Using PHP I have a Form that POSTS a Text Line from user input.

Each text line also has other data posted with it. So each text line a user submits has this data:

  • Text line string
  • Text line color
  • Text line font name

A user can submit anywhere from 1 to 6 Text Lines in the Form Post.

So in the backend PHP script I am trying to do something like this...

//Sign Text Lines
$numberOfTextLines = $this->getRequest()->getPost('count'); // 4

for ($line=1; $line <= $numberOfTextLines; $line++) {

    $line1Text = $this->getRequest()->getPost('line-1-text');
    $line1TextColor = $this->getRequest()->getPost('line-1-colorpicker');
    $line1TextFont = $this->getRequest()->getPost('line-1-font');

}

$numberOfTextLinesholds the number of Text Lines the user submitted in the POST.

So with that number of lines, I want to loop over that number of times and setup a variable to hold the Text Line string, color, and font values for each line of text submitted.

In the example above it would be 4 lines of text. So it needs to loop 4 times and set the 3 variables for each line. 4 lines of text x 3 variables = 12 variables set altogether.

In other parts of my script, I should then easily be able to do something like this below to check if a text line exist or not and if so should be able to access it's values...

if(isset($line4Text) && $line4Text != ''){
    echo 'Line 4 Text: ' . $line4Text;
    echo 'Line 4 Text Color: ' . $line4TextColor;
    echo 'Line 4 Text Font: ' . $line4TextFont;
}else{
    $line4Text = '';
    $line4TextColor = '';
    $line4TextFont = '';
}

So you can see, I need to take a number that is stored in a variable $numberOfTextLines that can be any value between 1 and 6.

Based on the number, I need to loop that many times and setup 3 variables for each number. Replacing the number in the Variable names with the number of the line. So in the 3 variables below, the number 4 in the name would be replaced with the corresponding number based on the number in the loop iteration:

$line4Text
$line4TextColor
$line4TextFont

I am not sure how to do this but I think possibly something to do with Variable Variables?

1 Answer 1

3

Yes this is exactly for what variable variables are used, you can just use this with curly syntax:

${"line". $line . "Text"} = $this->getRequest()->getPost('line-' . $line . '-text');
${"line" . $line . "TextColor"} = $this->getRequest()->getPost('line-' . $line . '-colorpicker');
${"line" . $line . "TextFont"} = $this->getRequest()->getPost('line-' . $line . '-font');
Sign up to request clarification or add additional context in comments.

1 Comment

great I thought it was something simple like this but I couldn't seem to get the correct syntax. I'm not used to using {and } like that so that seems to be the trick, thanks!

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.