Working with php explode? - php

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.

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
}
}
?>

Related

Echo ACF color picker value inside foreach loop for WP tags

I have a foreach loop to display all the tags of my project. Each Tag must have a colour associated with it, and I am using ACF to create this field on the tags dashboard.
However, when I try to display the value from color picker inside my foreach loop it just doesn't work.
I just can't see what I am doing wrong.
Here is my php code:
<?php
$tagslist = get_tags();
foreach($tagslist as $tag) {
?>
<li>
<p><?php echo get_field('tag_color'); ?></p>
<a class="tag-list_item theme-<?php echo $tag->slug; ?>" data-tag="<?php echo $tag->term_id; ?>" href="<?php echo get_tag_link($tag->term_id); ?>" stye>
<?php echo $tag->name; ?>
</a>
</li>
<?php }
?>
The name of the field that I made is "tag_color".
You can use below code to get field from tags:
<?php
$tagslist = get_tags();
foreach($tagslist as $tag) {
?>
<li>
<p><?php echo get_field('tag_color', 'post_tag_'.$tag->term_id); ?></p>
<a class="tag-list_item theme-<?php echo $tag->slug; ?>" data-tag="<?php echo $tag->term_id; ?>" href="<?php echo get_tag_link($tag->term_id); ?>" stye>
<?php echo $tag->name; ?>
</a>
</li>
<?php } ?>
Below is the reference link:
https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

codeigniter inside multiple checkbox

I have small problem with my PHP code. I must insert to database multiple values but I dont have any idea how I can do this.
My View:
<?php foreach($myKidsGroupID as $row): ?>
<label><input type="checkbox" name="user_my_group_msg" value="<?php echo $row->id; ?>" class="my_group_msg pull-right"><?php echo $row->firstname; ?> <?php echo $row->lastname; ?></label><br>
<?php endforeach; ?>
And my Model looks like this:
...
elseif($checked_my_group == 1) {
foreach ( ?? ) {
$new_mail = array(
'to_user' => $this->input->post('user_my_group_msg'),
);
$this->db->insert('mailbox', $new_mail);
}
}
...rest code....
Inside my View I display all users as checkbox but If I select two persons I must INSERT two query to database. Anyone can help me?
simple thing is
use serialize function don't use foreach
serialize $this->input->post('user_my_group_msg')
try it once it will help you your View code will look as like
<?php foreach($myKidsGroupID as $row): ?>
<label><input type="checkbox" name="user_my_group_msg" value="<?php echo $row->id; ?>" class="my_group_msg pull-right"><?php echo $row->firstname; ?> <?php echo $row->lastname; ?></label><br>
<?php endforeach; ?>
Model will be corrected like this
<?php
elseif(sizeof($this->input->post('user_my_group_msg'))>=1) {
foreach ( $this->input->post('user_my_group_msg') as $value ) {
$new_mail = array(
'to_user' => $value,
);
$this->db->insert('mailbox', $new_mail);
}
}
.....rest code
?>

Endforeach loop different class for first item

I'm not that good at php. What i have is a list of items looped with endforeach. What I want is that the first loop will have a class of col-lg-12 and from the second one that class will become col-lg-6. How can I achive that?
Here's the code:
<?php $firstLoop = true;
foreach( $network_value as $key => $value ){
if( $firstLoop ){
echo '<div class="col-lg-12">';
}
echo '<div class="col-lg-6">';
$firstLoop = false;
} ?>
^This is the code that I've tried, but it's not working how i wanted.
<?php if ($img): ?>
<img src="<?php echo $thumb->src ?>" width="<?php echo $thumb->width ?>" height="<?php echo $thumb->height ?>" alt="" />
<?php endif; ?>
<h4>
<div class="circle"><?php echo $datePublic = date('d M', strtotime($page->getCollectionDatePublic())); ?></div>
<br>
<a class="blogTitle" href="<?php echo $url ?>" target="<?php echo $target ?>"><?php echo $title ?></a></h4>
<h6>Posted by <?php echo $author; ?></h6>
<br>
<p><?php echo $description ?></p>
</div>
<?php endforeach; ?>
The most simple way to do it is to add a counter and check if it is the first value in the counter.
<?php
$counter = 0;
foreach( $network_value as $key => $value )
{
if($counter == 0){
echo '<div class="col-lg-12">';
} else {
echo '<div class="col-lg-6">';
}
$counter++;
}
?>
Also I want to add that there are two ways of using foreach and if-statements, but you are trying to mix them, which is wrong.
The first method is using brackets "{" and "}":
foreach($users as $user) {
// do things for each user
echo $user->name; // Example of writing out users name
}
And if-statement:
if(true) {
// do something
}
The second method is using "foreach(statement):" and "endforeach;"
foreach($users as $user):
// do things for each user
echo $user->name; // Example of writing out users name
endforeach;
And if-statement:
if(true):
// do something
endif;
Edited:
In your case you can use:
<?php
foreach ($pages as $key=>$page):
if($key==0){
echo '<div class="col-lg-12">';
} if($key==1){
echo '<div class="col-lg-6">';
}
endforeach; ?>
Or you can use:
<?php
foreach ($pages as $key=>$page):
if($key==0){
echo '<div class="col-lg-12">';
} else {
echo '<div class="col-lg-6">';
}
endforeach; ?>
OLD:
foreach($array as $key=>$row){
if($key==0){
echo '<div class="col-lg-12"></div>';
}
if($key==5){
echo '<div class="col-lg-6"></div>';
}
// OR try use %
if($key%2 == 0){
echo '<div class="col-lg-12"></div>';
} else {
echo '<div class="col-lg-12"></div>';
}

Display second value from mysql by foreach loop

Sorry, If my question is not good.
I am using this code to display values from mysql database.
<?php foreach ($values as $value) { ?>
<span><?php echo cimy_uef_sanitize_content($value['VALUE']); ?></span>
<?php } ?>
It is displaying result like this
-> http://pkbazaar.com/realoffers/wp-content/Cimy_User_Extra_Fields/riaz/avatar/aget-3.png
-> Johar Town
-> 1234567
-> 54000
-> Australia
-> WA
-> Lahore
But I don't want to display first value like "http://localhost/realoffers/wp-content/Cimy_User_Extra_Fields/riaz/avatar/aget-3.png"
What I should do to start displaying value from second value like "Johar Town".
If you don't need the first value you can shift the element off the beggining of array.
<?php $old_value = array_shift($values); ?>
<?php foreach ($values as $value) : ?>
<span>
<?php echo cimy_uef_sanitize_content($value['VALUE']); ?>
</span>
<?php endforeach; ?>
<?php $count = 0;
foreach ($values as $value) {
if($count == 0 ) {
$count++;
} else { ?>
<span><?php echo cimy_uef_sanitize_content($value['VALUE']); ?></span>
<?php }
} ?>
Try this:
<?php
foreach ($values as $k=>$value) {
if ($k==1){
?><span><?php echo cimy_uef_sanitize_content($value['VALUE']); ?></span><?php
}
}
?>
If the number of records is the same in each array and a fixed value, it would be fairly simple to use the following:
<?php
foreach ($values as $value)
{
?>
<span><?php echo cimy_uef_sanitize_content($value['FIELD2']); ?></span>
<span><?php echo cimy_uef_sanitize_content($value['FIELD3']); ?></span>
<span><?php echo cimy_uef_sanitize_content($value['FIELD4']); ?></span>
<span><?php echo cimy_uef_sanitize_content($value['FIELD5']); ?></span>
<span><?php echo cimy_uef_sanitize_content($value['FIELD6']); ?></span>
<?php
}
?>
It's probably better to change your query to not return a row you don't need but you can just remove the first item from the array:
<?php
array_shift($values);
foreach ($values as $value) { ?>
<span><?php echo cimy_uef_sanitize_content($value['VALUE']); ?></span>
<?php } ?>
See http://uk1.php.net/array_shift
<?php
unset($values[0]);
foreach ($values as $value) { ?>
<span><?php echo cimy_uef_sanitize_content($value['VALUE']); ?></span>
<?php } ?>
If you want to exclude url pattern than you should use regex match like this:-
<?php
foreach ($values as $value)
{
if(!preg_match('#^(http|https)?://#', $value))
{
?>
<span><?php echo cimy_uef_sanitize_content($value['VALUE']); ?></span>
<?php
}
}
?>
Hope this will help you to exclude urls.

How to Separate PHP Generated List Item - Magento

I have some php generated magento top-links with a screenshot below:
My goal is to separate the "Log In" link and have it floated to the left of the same row.
I am hoping for an efficient way to select the last generated list item element and apply some CSS to it.
The code is below:
<ul class="links pull-right"<?php if($this->getName()): ?> id="<?php echo $this->getName() ?>"<?php endif;?>>
<?php foreach($_links as $_link): ?>
<?php if ($_link instanceof Mage_Core_Block_Abstract):?>
<?php echo $_link->toHtml() ?>
<?php else: ?>
<li<?php if($_link->getIsFirst()||$_link->getIsLast()): ?> class="<?php if($_link->getIsFirst()): ?>first<?php endif; ?><?php if($_link->getIsLast()): ?> last<?php endif; ?>"<?php endif; ?> <?php echo $_link->getLiParams() ?>><?php echo $_link->getBeforeText() ?><a href="<?php echo $_link->getUrl() ?>" title="<?php echo $_link->getTitle() ?>" <?php echo $_link->getAParams() ?>><?php echo $_link->getLabel() ?></a><?php echo $_link->getAfterText() ?></li>
<?php endif;?>
<?php if (! $_link->getIsLast()):?>|<?php endif;?>
<?php endforeach; ?>
</ul>
Any ideas would be greatly appreciated!
CSS offers a way to style the last-child of a collection, no tinkering with PHP required.
http://tinker.io/926d2
ul.links.pull-right :last-child {
margin-left: 2em;
}
I answered a similar question not long ago. This adds the class "last-item" to the last item processed.
<?php list($parent) = split('/', $this->url); ?>
<?php $last_articles = $this->find('/news')->children(array('limit'=>5, 'order'=>'page.created_on DESC')); ?>
<ul id="latest-news">
<?php $count = count($last_articles); $num = 0; ?>
<?php foreach ($last_articles as $article): ?>
<li <?php if($num == $count-1){ ?> class="last-item" <?php } ?>>
<?php echo '<h3>'.$article->link($article->title()).'</h3>'; ?>
<?php echo strip_tags(substr($article->content(),0,100)).'...'; ?>
</li>
<?php $num++ ?>
<?php endforeach; ?>
</ul>
Evening All,
Id try and limit the amount of business logic you are adding to your templates. As what you are looking to achieve is custom to this instance of magento I would create a very basic module. I would then look to either implement a new block or just a helper function that will return the data you want.
If your working on the block function ensure your class extends the Magento navigation class. ( Sorry I have not checked what this is ) Then create the action: E.g.
public function getNavigation()
{
$links = $this->getLinks();
$linkArray = array();
$linkCount = count($links);
$i;
foreach($links as $link) {
if($i == $linkCount) {
$last = true;
} else {
$last = false;
}
$linkArray[] = 'link' => $link->getLink()->toHtml(),
'isLast' => $last
$i++;
}
return $linkArray();
}
Your block would then have minimal logic applied. Mainly just iterating through the result set.
Hope that makes sense, If not let me know and I will get you what you require.

Categories