How to arrange subtitles under a title in Yii2 - php

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.

Related

Variable ++ in query loop php

There are 2 dynamic changing requiers thing in my html.they are "active" and "numbers (for example 1,2,3...)".I want write "active" into class if it is first element in query, else write only one space.Also I want write "1" into class of first element and for other elements,write like this "2,3,...".
So, for this scenerio, I wrote below code.But my plugin doesnt work like what I want.You can see how the numbers are created for this script on that link:
http://www.hekim.deniz-tasarim.site/elementor-685/
may it loads slowly, so I write echos for numbers here:
0 1 0 2 0 3 0 4 for first query
1 0 1 0 1 0 1 0 for second query
So, how can I solve this?
<?php
$activeornot = 0;
$teamnumber = 1;
$query = new \WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
if ($activeornot = 0):
$echo_active_or_not = "active";
else:
$echo_active_or_not = " ";
endif;
?>
<li role="presentation" class="<?php echo $echo_active_or_not; ?>"><img src="<?php the_post_thumbnail_url(); ?>" alt="s1.jpg"></li>
<?php echo $activeornot++; ?>
<?php echo $teamnumber++; ?>
<?php } }
else {
echo "<div style=background:red> There is no available team member with current settings. </div>";
}
?>
</ul>
</div>
</div>
<div class="row">
<!-- Tab panes -->
<div class="tab-content">
<?php
$query = new \WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$activeornot = 0;
if ($activeornot = 0):
$echo_active_or_not = "active";
else:
$echo_active_or_not = " ";
endif;
$teamnumber = 1;
?>
<div role="tabpanel" class="tab-pane <?php echo $echo_active_or_not; ?> team<?php echo $teamnumber; ?>" id="team<?php echo $teamnumber; ?>">
<div class="col-xxs-12 col-xs-6 col-sm-6 col-md-4">
//something
</div>
</div>
</div>
</div>
<?php echo $teamnumber++; ?>
<?php echo $activeornot++; ?>
<?php } } ?>

Foreachloop accordion where the title closes every 2 and the body closes every 2 but are contained in a single row

So I am trying to build a special accordion or collapse in this sense as it's Bootstrap, and I am having a hard time getting the code to cooperate.
I am using a foreach loop to display a repeater. Within it, I have the header, and the body. I am doing rows of 2 each. However, this is how I need it laid out. I need 1 row to hold the header and body, then inside that, 2 more rows. One row is for the header, and one row is for the body. This way the content is full span under the header.
Here is a visual of what I am talking about
I am not sure with my code what I am doing wrong. I've tried running a foreach loop twice, I've tried a modulus inside the loop to close the header row and reopen it, and the same for the body. However that just doesn't sit well at all. Every odd item opens and closes the head and that's it. The body is contained within the header.
I don't have anything live to showcase, but here is my code:
<div class="panel-group" id="accordion_<?php echo $accordion_widget_title; ?>" role="tablist" aria-multiselectable="true">
<div class="panel panel-default">
<?php $x = 1; ?>
<?php foreach( $instance['row_repeater'] as $i => $repeater ) :
// Concatenate the Accordion Title, then convert it to a lower case string
$accordion_title = preg_replace('/\s+/', '-', $repeater[ 'row_accordion_title' ] );
$accordion_title = strtolower( $accordion_title );
$accordion_title = $accordion_title . '_' . $x;
// Start Accordion Title Loop ?>
<div class="panel-heading" role="tab" id="heading-<?php echo $accordion_title; ?>">
<a role="button" data-toggle="collapse" data-parent="#accordion-<?php echo $accordion_widget_title; ?>" href="#item-<?php echo $accordion_title; ?>" aria-expanded="false" aria-controls="item-<?php echo $accordion_title; ?>">
<h4 class="panel-title">
<?php _e( $repeater[ 'row_accordion_title' ], 'boss' ); ?>
</h4>
</a>
</div>
<?php if ( ( $x % 2 == 0 ) ) : ?>
</div><div class="panel panel-default">
<?php endif; ?>
<?php $x++; ?>
<?php endforeach; ?>
<div class="panel panel-default">
<?php foreach( $instance['row_repeater'] as $i => $repeater ) :
// Concatenate the Accordion Title, then convert it to a lower case string
$accordion_title = preg_replace('/\s+/', '-', $repeater[ 'row_accordion_title' ] );
$accordion_title = strtolower( $accordion_title );
$accordion_title = $accordion_title . '_' . $x;
// Start Accordion Title Content ?>
<div id="item-<?php echo $accordion_title; ?>" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading-<?php echo $accordion_title; ?>">
<div class="panel-body">
<?php _e( $repeater[ 'row_accordion_content' ], 'boss' ); ?>
</div>
</div>
<?php if ( ( $x % 2 == 0 ) ) : ?>
</div><div class="panel panel-default">
<?php endif; ?>
<?php $x++; ?>
<?php endforeach; ?>
</div>
Here is another variation of what I've tried
<div class="panel-group<?php if ( !empty( $additional_class ) ) : echo $additional_class; endif; ?>" id="accordion_<?php echo $accordion_widget_title; ?>" role="tablist" aria-multiselectable="true">
<?php $x = 1; ?>
<?php foreach( $instance['row_repeater'] as $i => $repeater ) : ?>
<?php
// Concatenate the Accordion Title, then convert it to a lower case string
$accordion_title = preg_replace('/\s+/', '-', $repeater[ 'row_accordion_title' ] );
$accordion_title = strtolower( $accordion_title );
$accordion_title = $accordion_title . '_' . $x;
?>
<?php // Start Accordion Title Loop ?>
<?php if ( ( $x % 2 == 1 ) || ( $x == 1 ) ) : ?>
<div class="panel panel-default">
<?php endif; ?>
<div class="panel-heading" role="tab" id="heading-<?php echo $accordion_title; ?>">
<a role="button" data-toggle="collapse" data-parent="#accordion-<?php echo $accordion_widget_title; ?>" href="#item-<?php echo $accordion_title; ?>" aria-expanded="false" aria-controls="item-<?php echo $accordion_title; ?>">
<h4 class="panel-title">
<?php _e( $repeater[ 'row_accordion_title' ], 'boss' ); ?>
</h4>
</a>
</div>
<?php $x++; ?>
<?php if ( ( $x % 2 == 1 ) || ( $x == 1 ) ) : ?>
</div>
<?php endif; ?>
<?php endforeach; ?>
</div>
<div class="panel-group" data-parent="#accordion-<?php echo $accordion_widget_title; ?>" id="accordion_<?php echo $accordion_widget_title; ?>" role="tablist" aria-multiselectable="true">
<?php $x = 1; ?>
<?php foreach( $instance['row_repeater'] as $i => $repeater ) : ?>
<?php
// Concatenate the Accordion Title, then convert it to a lower case string
$accordion_title = preg_replace('/\s+/', '-', $repeater[ 'row_accordion_title' ] );
$accordion_title = strtolower( $accordion_title );
$accordion_title = $accordion_title . '_' . $x;
?>
<?php // Start Accordion Title Content ?>
<?php if ( ( $x % 2 == 1 ) || ( $x == 1 ) ) : ?>
<div class="panel-body-row">
<?php endif; ?>
<div id="item-<?php echo $accordion_title; ?>" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading-<?php echo $accordion_title; ?>">
<div class="panel-body">
<?php _e( $repeater[ 'row_accordion_content' ], 'boss' ); ?>
</div>
</div>
<?php $x++; ?>
<?php if ( ( $x % 2 == 1 ) || ( $x == 1 ) ) : ?>
</div>
<?php endif; ?>
<?php endforeach; ?>
</div>
I'm just not 100% sure what it is I'm not grasping.
I actually think that the PHP function array_chunk will make your life much easier. http://php.net/manual/en/function.array-chunk.php
I won't try to recreate all your HTML here, but here's the basic idea you could use:
//Split our repeaters into an array of arrays, each with 2 elements
$rows = array_chunk($instance['row_repeater'], 2);
foreach($rows as $row){
echo "<div class='row'>";
foreach($row as $items){
echo "<div class='col-sm-6'>";
echo $items('row_accordion_title');
// echo the rest of your repeater stuff...
echo "</div>";
}
echo "</div>";
}
I think your approach to generating the HTML with PHP is making it difficult to troubleshoot. You should have the foreach loop contain the entire html output you are trying to achieve for each iteration to make it easier to keep track of. Additionally I would make sure that you are creating an incremental set of IDs for the collapsible elements to avoid confusing Boostrap's JS components. Something like:
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<?php
for($i = 0; $i < count($instance['row_repeater']); $i++){
$accordion_title = preg_replace('/\s+/', '-', $instance['row_repeater'][$i][ 'row_accordion_title' ] );
$accordion_title = strtolower( $accordion_title );
$accordion_title = $accordion_title . '_' . $x;
$out = "<div class=\"panel panel-default\">
<div class=\"panel-heading\" role=\"tab\" id=\"heading".$i+1."\">
<h4 class=\"panel-title\">
<a role=\"button\" data-toggle=\"collapse\" data-parent=\"#accordion\" href=\"#collapse".$i+1."\" aria-expanded=\"true\" aria-controls=\"collapse".$i+1."\">
".$accordion_title."
</a>
</h4>
</div>
<div id=\"collapse".$i+1."\" class=\"panel-collapse collapse in\" role=\"tabpanel\" aria-labelledby=\"heading".$i+1."\">
<div class=\"panel-body\">
".$instance['row_repeater'][$i][ 'row_accordion_content' ]."
</div>
</div>
</div>";
echo $out;
}
?>
</div>
Doing this will help to simplify your looping and make it easier to track down issues in your code. Additionally using the iteration of $i in the for look you can make sure you create unique IDs to prevent the bootstrap triggers from stepping on each other. Hope this helps

Php split value into different li classes

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

PHP while loop, display in pairs of two

In Wordpress, I am creating a slider to slide through some content that the user specify. I am using ACF (advanced custom fields) if anyone is familiar with that, however what I am trying to accomplish is to show two content items at a time, while sliding through the jQuery slider.
Here is my loop:
<?php while(has_sub_field('popular_topic')): ?>
<li>
<div class="slide">
<img src="<?php the_sub_field('popular_topic_image'); ?>" alt="" />
<div class="img-wrapper"></div>
<div class="slider-content">
<?php
$len = 60;
$popularTopicTitle = get_sub_field('popular_topic_title');
$newContent = substr($popularTopicTitle, 0, $len);
if(strlen($newContent) < strlen($popularTopicTitle)) {
$newContent = $newContent.'...';
}
echo '<p>'.$newContent.'</p>';
?>
<a class="more-arrow" href="<?php the_sub_field('popular_topic_link'); ?>">Read More</a>
</div>
</div>
</li>
<?php endwhile; ?>
This is currently working, however it only shows one slide. I want it to show two slides and a time. Is there something I can do with a counter? Does this make sense?
my syntax might be a little off but maybe give this a shot?
<?php $i = 0; ?>
<?php while(has_sub_field('popular_topic')): ?>
<?php
$len = 60;
$popularTopicTitle = get_sub_field('popular_topic_title');
$newContent = substr($popularTopicTitle, 0, $len);
if(strlen($newContent) < strlen($popularTopicTitle)) {
$newContent = $newContent.'...';
}
$contentVar[$i] = array (
'img' => the_sub_field('popular_topic_image'),
'title' => $newContent,
'link' => the_sub_field('popular_topic_link')
);
$i++;
?>
<?php endwhile; ?>
<?php $j = 0; ?>
<?php while ($j < $i) : ?>
<li>
<div class="slide">
<img src="<?php echo $contentVar[$j]['img']; ?>" alt="" />
<div class="img-wrapper"></div>
<div class="slider-content">
<p><?php echo $contentVar[$j]['title']; ?></p>
<a class="more-arrow" href="<?php echo $contentVar[$j]['link']; ?>">Read More</a>
</div>
<?php $j++; ?>
<?php if ($j <= $i) : ?>
<img src="<?php echo $contentVar[$j]['img']; ?>" alt="" />
<div class="img-wrapper"></div>
<div class="slider-content">
<p><?php echo $contentVar[$j]['title']; ?></p>
<a class="more-arrow" href="<?php echo $contentVar[$j]['link']; ?>">Read More</a>
</div>
<?php endif; ?>
</div>
</li>
<?php $j++; ?>
<?php endwhile; ?>

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();

Categories