menu

Questions & Answers

Echo different html filtered by nested variables

In a PHP page (content.php) I need to select/filter some html code which is included in an another file (include.php)

To select/filter the code in include.php I use variables to "label" the part of code I want to use in content.php More, I need to use another variable in content.php which contains the variable that filters the code. I will then use the variable to echo the contents I need.

Here's an example:

include.php

$var1 = <div>text1</div>
$var1 = <div>text2</div>

$var2 = <div>text3</div>
$var2 = <div>text4</div>

$var3 = <div>text5</div>
$var3 = <div>text6</div>

content.php

$varContent1 ($var1, $var2, $var3)
$varContent2 ($var1, $var3)

some code
echo $varContent1 (<div>text1</div>, <div>text3</div>, <div>text5</div>)
some code
echo $varContent2 (<div>text2</div>, <div>text6</div>)

and so on.

Let's say I'd like to use the same variable name to select two different pieces of codes to be echoed in another php page using other variables.

At the moment I'm solving this problem like this:

include.php

$var1 = <div>text1</div>
$$var1 = <div>text2</div>

$var2 = <div>text3</div>
$$var2 = <div>text4</div>

$var3 = <div>text5</div>
$$var3 = <div>text6</div>

content.php

<?php include 'include.php';?>
<?php $varContent1 = ($var1.$var2.$var3) ;?>
<?php $varContent2 = ($$var1.$$var2.$$var3) ;?>

some code
<?php echo $varContent1; ?> (I need to echo this code twice)
some code
<?php echo $varContent1; ?> (I need to echo this code twice)
some code
<?php echo $varContent2; ?> 

I'd like to be able to write the variables "filter" in content.php once and not twice (using $$). Any idea?

Comments:
2023-01-17 23:01:10
Quite hard to understand what the actual problem is supposed to be. Not sure what this is supposed to have to do with actually filtering data in the first place.
Answers(0) :