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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With