0

This is going to be pretty hard to explain, so I'll try to make it as much of a chronological story as possible and end with the question, so that anyone who needs a relatively in-depth idea of what I'm talking about has one :).

I have a theme template with four text containers named primary_headline, primary_subline, secondary_headline and secondary_subtext. The content for each container comes from echo($container_name).

Each container has it's own variable, $container_name, which posts user created content to from a form, contents which sometimes contains the variables $city_name and $ref_name as raw data (i.e. "Hello, I'm from $city_name"), as $city_name and $ref_name has assigned values, for arguments sake let's just say $city_name= Dallas and $ref_name = Facebook.

Originally, I believed that I only wanted the user to be able to use $city_name and $ref_name within the secondary_subtext container, and I was also going to make the option to use $city_name OR $ref_name the choice of the user in the first place (they'd have to select one, or the other, or none - but couldn't select both.

However, I now want to allow $city_name AND $ref_name across all four container variables ($secondary_subtext for example).

So, my question is, how do I go about doing that in the easiest possible fashion?

Here's my old code for good measure:

if($geo_text == "enable") {
    $geo_init = include("inc/geo_text.php");
    $secondary_headline = str_replace('$city_name', $city_name, $_POST['secondary_headline']); // $_POST data from user
} else($ref_text == "enable") {
    $ref_init = include("inc/ref_text.php");
    $secondary_headline = str_replace('$ref_name', $ref_name, $_POST['secondary_headline']); // $_POST data from user
} else { $secondary_headline = $_POST['secondary_headline']; }

Any comments/answers etc will be very greatly appreciated :)!!

1 Answer 1

0
$placeholders = array('city_name','ref_name');
$include_mapper('geo_text' => 'inc/geo_text.php','ref_text'=>'inc/ref_text.php');
$containers = array('first_headline','scondary_headline');

$found = 0;
foreach ($include_mapper as $map => $include){
    if ($$map == "enable"){
        include ($include_mapper[$map]);
        foreach ($container as $container){
            foreach ($placeholders as $placeholder){
                $$container = str_replace('$'.$placeholder, $$placeholder, $_POST[$container];
            }
        }
      $found = 1;
      break;
    }
}
if (!$found){
    foreach ($container as $container){
        $$container = $_POST[$container];
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the input. According to others this fits what I need, but it's a little complex for me (I've coded something already using less lines that I posted on my "duplicate" question), however I'll accept this for others to see :).

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.