3

I've got a list of countries, sometimes the list contains just one country and sometimes more. This is my code:

<?php if($this->value): ?>
<a class="tag" href="{{env::url}}/business?land=<?php echo $this->value; ?>" title="<?php echo $this->value; ?>"><?php echo $this->value; ?></a>
<?php endif; ?>

The output right now is: "Germany,Austria,Switzerland".

I want to create a link for every country, how can I do that?

I hope you guys can help me.

6
  • Are you looking for a loop? Commented May 13, 2016 at 11:41
  • I dont know^^ my php is not so good. Commented May 13, 2016 at 11:42
  • you need to use foreach loop to loop through countries Commented May 13, 2016 at 11:43
  • I think your code is not complete. So post the whole code... code and output are not sync. Commented May 13, 2016 at 11:44
  • Thats the code for the country attribute, that is given from my CMS. Commented May 13, 2016 at 11:46

3 Answers 3

2

I assume that you are getting comma separate list of countries inside $this->value.

We can use function like explode to split that string into array and then use foreach to loop through array and generate individual link

<?php if($this->value): ?>
    <?php
        $array = explode( ',', $this->value );
    ?>
    <?php foreach($array as $value): ?>
        <a class="tag" href="{{env::url}}/business?land=<?php echo $value; ?>" title="<?php echo $value; ?>"><?php echo $value; ?></a> - 
    <?php endforeach; ?>
<?php endif; ?>

I hope this answers your question.

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

Comments

2

I assume that $this->value is an array so you can get the countries one by one using foreach loop like below:

<?php if($this->value): ?>
    <? $countries = $this->value; ?>
    <? foreach ($countries as $country): ?>
        <a class="tag" href="{{env::url}}/business?land=<?php echo $country; ?>" title="<?php echo $country; ?>"><?php echo $country; ?></a>
    <? endforeach; ?>
<? endif; ?>

if it is string than you can use explode() function and get the countries as an array like:

<?php $countries = explode("," , $this->value); ?>

and pass this $countries array to foreach loop.

Comments

2

For achieving this you need to check and explode the country string by comma and than loop it in foreach.

Sample code:

<?php
if ($this->value) {
    $countries = explode(",", $this->value);
    if (count(countries) > 1) {
        foreach ($countries as $country) {
            ?>
            <a class="tag" href="{{env::url}}/business?land=<?php echo $country; ?>" title="<?php echo $country; ?>"><?php echo $country; ?></a>
            <?php
        }
    } else {
        ?>
        <a class="tag" href="{{env::url}}/business?land=<?php echo $this->value; ?>" title="<?php echo $this->value; ?>"><?php echo $this->value; ?></a>
        <?php
    }
}
?>

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.