Php split value into different li classes - php

I use this code to display user entered data.
But all data is displayed on 1 single line separate by comma.
I want to display all separate data in an individual < li > class
How can I achieve that?
<?php if ( $this->showPros() ): ?>
<div class="pros">
<h4><?php echo $this->__('Pros:') ?></h4>
<ul class="pros-ul">
<li class="pros-li">
<?php $isFirst = true ?>
<?php foreach( $this->getProsCollection() as $pros )
{
$name = $this->__( $pros->getName() );
echo ( $isFirst ? $name : ( ', '.$name ) );
$isFirst = false;
} ?><br />
</li>
</ul>
</div>
<?php endif ?>

Just wrap the $name in li tags:
<?php if ( $this->showPros() ): ?>
<div class="pros">
<h4><?php echo $this->__('Pros:') ?></h4>
<ul class="pros-ul">
<?php foreach( $this->getProsCollection() as $pros )
{
$name = $this->__( $pros->getName() );
echo ('<li class="pros-li">'.$name.'</li>');
}
?>
</ul>
</div>
<?php endif ?>

Related

How to add a span tag if the UL list is first child and last child? PHP Condition if else

I'm working on WordPress custom fields and I need to write PHP if-else conditions in a loop whether it is the first child or last child of the ul list. but I don't know how to apply, kindly please help me to solve it
Here is my code below
<?php if( have_rows('chart') ): ?>
<ul class="list-unstyled">
<?php while( have_rows('chart') ): the_row();
$sr_no = get_sub_field('sr_no');
$list_content = get_sub_field('list_content');
?>
<li><div><span class="w-num"><?php echo $sr_no; ?></span><?php echo $list_content; ?></div><span class="chart-divider"><span class="chart-divider-r"></span></span></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Count the entries for rows.
Set up a counter, and every time you iterate increase.
<?php if( have_rows('chart') ): ?>
<ul class="list-unstyled">
<?php
$counter = 1;
$total-row = // Figure out how to count(rows), or something like that
while( have_rows('chart') ): the_row();
$sr_no = get_sub_field('sr_no');
$list_content = get_sub_field('list_content');
if($counter == 1){
// First Row
} elseif ($counter == $total-row){
// Last Row
}
?>
<li><div><span class="w-num"><?php echo $sr_no; ?></span><?php echo $list_content; ?></div><span class="chart-divider"><span class="chart-divider-r"></span></span></li>
<?php
$counter++;
endwhile; ?>
</ul>
<?php endif; ?>
Please try with below:
The result you will see in the li as a class
If fist li, you will see the first_class
If last li, you will see the last_class
Else only some_class
<?php
$Total_charts_count = wp_count_posts( 'chart' )->publish; //Total count of charts
$chart_number = 1; //Loop starting point.
if( have_rows('chart') ): ?>
<ul class="list-unstyled">
<?php
while( have_rows('chart') ): the_row();
$sr_no = get_sub_field('sr_no');
$list_content = get_sub_field('list_content');
// First item
if($chart_number == 1) {
$class = 'some_class first_class';
// Last item
} else if ($chart_number == $Total_charts_count) {
$class = 'some_class last_class';
// Not first / Not last item
} else {
$class = 'some_class';
}
?>
<li class="<?php echo $class; ?>">
<div>
<span class="w-num"><?php echo $sr_no; ?></span>
<?php echo $list_content; ?>
</div>
<span class="chart-divider">
<span class="chart-divider-r"></span>
</span>
</li>
<?php
$chart_number++;
endwhile; ?>
</ul>
<?php endif;
?>
Hope it's work for you.

Change HTML for odd & even items in a foreach loop

My PHP isn't great!
I have some metaboxes in a wordpress website that displays a column type layout. I can output the metabox content as follows:
<?php
// Group ID
$columns_values = rwmb_meta( 'columns_solutions' );
if ( ! empty( $columns_values ) ) : ?>
<section class="columns">
<?php foreach ( $columns_values as $columns_value ) {
// Grab the image
$columns_imgs = isset( $columns_value['_rtl_column_solutions_image'] ) ? $columns_value['_rtl_column_solutions_image'] : array();
foreach ( $columns_imgs as $columns_img ) {
// Set each image size for the responsive background
$column_image_lg = RWMB_Image_Field::file_info( $columns_img, array( 'size' => 'column-lg' ) );
$column_image_md = RWMB_Image_Field::file_info( $columns_img, array( 'size' => 'column-md' ) );
$column_image_sm = RWMB_Image_Field::file_info( $columns_img, array( 'size' => 'column-sm' ) );
$column_image_xs = RWMB_Image_Field::file_info( $columns_img, array( 'size' => 'column-xs' ) );
}
// Grab the title, oversized text and general text
$column_title = isset( $columns_value['_rtl_columns_title'] ) ? $columns_value['_rtl_columns_title'] : array();
$column_oversized = isset( $columns_value['_rtl_columns_oversized'] ) ? $columns_value['_rtl_columns_oversized'] : array();
$column_general_txt = isset( $columns_value['_rtl_columns_general_text'] ) ? $columns_value['_rtl_columns_general_text'] : array();
?>
<div class="columns-wrapper">
<div class="column">
<?php if(!empty($column_title)) { ?>
<div class="section-title">
<h2>
<?php echo $column_title; ?>
</h2>
</div>
<!-- /.section-title -->
<?php } ?>
<?php if(!empty($column_oversized)) { ?>
<div class="oversized">
<?php echo $column_oversized; ?>
</div>
<!-- /.oversized -->
<?php } ?>
<?php if(!empty($column_general_txt)) { ?>
<?php echo wpautop($column_general_txt); ?>
<?php } ?>
</div>
<!-- /.column -->
<div class="column">
<div class="image min-height cover bg-responsive" style="background-image: url(<?php echo LAZY_IMG; ?>);"
data-lg="<?php echo $column_image_lg['url']; ?>"
data-md="<?php echo $column_image_md['url']; ?>"
data-sm="<?php echo $column_image_sm['url']; ?>"
data-xs="<?php echo $column_image_xs['url']; ?>">
</div>
<!-- /.image -->
</div>
<!-- /.column -->
</div>
<!-- /.column-wrapper -->
<?php } ?>
</section>
<!-- /.columns -->
<?php endif; ?>
Output as below:
This is a clonable meta group, I would like to be able to swap the content and the image (left & right) with each odd and even foreach loop/array.
I feel so stupid, all I needed was to count the posts and output alternate html.
I wrapped my code above in this:
<?php $counter = "";
foreach ( $columns_values as $columns_value ) {
$counter +=1;
// all my var stuff here
?>
<?php if($counter == 1) { ?>
<div class="columns-wrapper odd">
<!-- HTML here for the first loop -->
</div>
<?php }
// If the second item in the loop
elseif($counter == 2) {
// Reset the counter
$counter = 0;
?>
<div class="columns-wrapper even">
<!-- Other HTML here for the second loop -->
</div>
<?php } //end the elseif $counter ?>
<?php } //end the foreach stuff ?>

How to arrange subtitles under a title in Yii2

I am trying to arrange some subtitles under a main tile but it is repeating the title.
<div id="downloads" class="tab-pane" role="tabpanel">
<?php
$product_id = $model->id;
$downloads_model = Printingmachinedownloads::find()->where(['productid'=>$product_id])->all();
foreach ($downloads_model as $doc) {
$doc_type = $doc['type'];
$doc_label = $doc['documentlabel'];
$doc_title = $doc['documentname'];
?>
<div class="amazingcarousel-container-1 tab_style">
<h3><?php echo $doc_type;?></h3>
<a target = '_blank' href="<?php echo Yii::$app->homeUrl?>images/printingmachine/downloads/<?php echo $doc_title; ?>">
<?php echo $doc_label ?>
</a>
</div>
<?php
}
// $doc_title = $downloads_model[0]->documentlabel;
?>
</div>
</div>
The output is:
What I need is
Brochures
abc
def
ghi
Specificationsheet
xyz
lmn
opq
Can Anyone tell me what I have to do?
Thanks in Advance
If you don't have issue with new MySQL version group by concept, you can use something like:
...
$downloads_model = Printingmachinedownloads::find()->where(['productid'=>$product_id])->groupBy(['type'])->all();
foreach($downloads_model as $doc):
<div class="amazingcarousel-container-1 tab_style">
<h3><?= type['type'] ?></h3>
$subtitles = Printingmachinedownloads::find()->where(['productid'=>$product_id, 'type' => $doc['type'] )->all();
foreach($subtitles as $subtitle):
<a target = '_blank' href="<?php echo Yii::$app->homeUrl?>images/printingmachine/downloads/<?php echo $subtitle['documentname']; ?>">
<?php echo $subtitle['documentlabel'] ?>
</a>
enforeach;
</div>
endforeach;
...
If have issue with group by you can use little non standard way, it's not best practice and effective way:
$downloads_model = Printingmachinedownloads::find()->where(['productid'=>$product_id])->orderBy(['type'=>SORT_DESC])->all();
$previous_title = ''; $iteration = 1;
foreach($downloads_model as $doc):
$subtitles = Printingmachinedownloads::find()->where(['productid'=>$product_id, 'type' => $doc['type'] )->all();
$count = count($subtitles);
$previous_title = $previous_title ?: $doc['type'];
if( $previous_title == $doc['type'] && $count == $iteration ):
<div class="amazingcarousel-container-1 tab_style">
<h3><?= $previous_title ?></h3>
foreach($subtitles as $subtitle):
<a target = '_blank' href="<?php echo Yii::$app->homeUrl?>images/printingmachine/downloads/<?php echo $subtitle['documentname']; ?>">
<?php echo $subtitle['documentlabel'] ?>
</a>
enforeach;
</div>
$previous_title = ''; $iteration = 1;
else:
$iteration++;
endif;
endforeach;
I didn't test it, but something similar should work.
But my suggestion and effective way will be you use relational tables 1 title container another related table containing sub titles.

Get Magento Categories with Especific Sort of Admin Panel

I wrote the code below to put together a customized menu of categories. Everything works fine, but would like the order of the categories were the same order as defined in the administrator panel where there drap and drop functionality.
<?php
$subCats = Mage::getModel('catalog/category')->load(76)->getChildren();
$dispositosCatIds = explode(',',$subCats);
?>
<ul class="menu">
<?php $controleNum = 0; ?>
<?php foreach($dispositosCatIds as $dispositoCatId): ?>
<?php $aparelhoCat = Mage::getModel('catalog/category')->load($dispositoCatId); ?>
<?php if($aparelhoCat->getIsActive()): ?>
<li class="<?php print $controleNum ? '' : 'submenu first'; ?>"><a class="drop" href="<?php echo $aparelhoCat->getUrl(); ?>"> <span><?php echo $aparelhoCat->getName(); ?></span></a> <!--Begin 6 column Item -->
<div class="dropdown_6columns">
<div class="inner"><span class="title"><?php echo $aparelhoCat->getName(); ?></span>
<div class="col_2">
<div class="col_2 firstcolumn"><img src="<?php echo $aparelhoCat->getImageUrl(); ?>" alt="<?php echo $aparelhoCat->getName(); ?>" /></div>
</div>
<div class="col_4" style="margin-bottom: 20px;">
<?php echo $aparelhoCat->getDescription(); ?>
</div>
<div class="col_2 categorias-super"><span class="title_col">Produtos para <?php echo $aparelhoCat->getName(); ?></span>
<?php $subSubCats = $aparelhoCat->getChildrenCategories();?>
<?php if (count($subSubCats) > 0): ?>
<?php //$controleNumLI = 0; ?>
<ul style="list-style: none; float: none !important;">
<?php foreach($subSubCats as $_subcategory): //Rodando entre as categorias de Um dispositivo ?>
<?php if($_subcategory->getIsActive()): ?>
<li class="level1 <?php //print $controleNumLI ? '' : 'first'; ?>"> <?php echo $_subcategory->getName(); ?></li>
<?php //$controleNumLI += 1; ?>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</div>
</div>
</li>
<?php $controleNum += 1; ?>
<?php endif; ?>
<?php endforeach; ?>
</ul>
I tried to use other modes (based here) can do this, but I could not. The problem that the function returns getChildren() is a string with IDs in ascending order.
Some Ideas?
This is the code I use to display category in a dropdown box in the order of the admin... the key is setOrder('path','ASC')
$categories = array();
$_categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToFilter('is_active',array('eq'=>true))
->addAttributeToSelect('level')
->setOrder('path','ASC')
->addAttributeToSelect('name')->load();
foreach($_categories as $cat){
$level = $cat->getLevel() - 1;
$pad = str_repeat("----", ($level > 0) ? $level : 0);
$categories[] = array('value' => $cat->getEntityId(), 'label' => $pad . ' ' . $cat->getName());
}
print_r($categories);
You could do something like this: create array tree from array list
I got it:
$dispositovosCategoryId = 76;
$dispositovosCategoryIds = Mage::getModel('catalog/category')->getCollection()
->addFieldToFilter('parent_id', array('eq'=>$dispositovosCategoryId))
->addAttributeToFilter('is_active',array('eq'=>true))
->addAttributeToSelect('level')
->setOrder('position','ASC')
->addAttributeToSelect('name','url','image')
->load();

alternate class to each LI foreach

I am trying to give an alternate class to each LI foreach.
i.e.
<li class="odd">
text
</li>
<li class="even">
text
</li>
<li class="odd">
text
</li>
<li class="even">
text
</li>
This is my code:
<ul>
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php if($extraField->value): ?>
<li class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
Text here
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
Please can anyone help...
Thanks
try :
<?php $count = 0; // need to set first value
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php if($extraField->value): ?>
<li class="<?php echo (++$count % 2) ? "odd" : "even"; ?> type <?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
Text here
</li>
<?php endif; ?>
<?php endforeach; ?>
Link to pre/post increment docs if needed
I would do it that way .. it's key independent and you better see what the li-class will look like.
<ul>
<?php
foreach ($this->item->extra_fields as $key=>$extraField) {
if($extraField->value) {
$toggle = ($toggle=="odd"?"even":"odd");
echo '<li class="',$toggle,' type',ucfirst($extraField->type),' group', $extraField->group,'">';
echo 'Text here';
echo '</li>';
}
}
?>
</ul>
jQuery can easily do this if that's an option for you. It's :odd and :even selectors should do just what you're looking for without too much hassle.
I know this is old, but I humbly present a cleaner solution:
<ul>
<?php
$odd = true;
foreach ( $this->item->extra_fields as $key => $extraField ) {
// Output template.
$template = '<li class="%1$s">%2$s</li>';
// Create our classes.
$classes = [
( $odd ) ? 'odd' : 'even',
'type' . ucfirst($extraField->type),
'group' . $extraField->group
];
// Create our class string.
$class_string = implode( ' ', $classes );
// Print our content out.
printf( $template, $class_string, 'Text here' );
// Flip the classname for next time.
$odd = ! $odd;
}
?>
</ul>
It could of course be cleaner still, by using fewer variables. This is just my style.

Categories