Home > Net >  How to add wrapAll jquery after tags with the same id
How to add wrapAll jquery after tags with the same id

Time:11-12

php

<?php foreach ($forlop as $value) : ?>
            <?php 
                $stringTitle = substr($value->getTitle(), 0, 1);
            ?>
            <?php if(is_numeric($stringTitle)){
                echo "<h3 id='other'>0-9</h3>";
            } else{
                echo "<h3 id=".strtolower($stringTitle).">".strtoupper($stringTitle)."</h3>";
            }?>
            <div class="item"><?php echo $value->getId(); ?></div>            
<?php endforeach; ?>
<h3 id="c">C</h3> 
<div class="item">1</div>
<div class="item">2</div>

<h3 id="d">D</h3> 
<div class="item">3</div>
<div class="item">4</div>

<h3 id="e">E</h3>
<div class="item">5</div>


js

$('[id]').each(function (i) {
    $('[id="'   this.id   '"]').slice(1).remove();
});

desired result

<h3 id="c">C</h3> 
<div class="items-add">
    <div class="item">1</div>
    <div class="item">2</div>
</div>
<h3 id="d">D</h3>
<div class="items-add"> 
    <div class="item">3</div>
    <div class="item">4</div>
</div>
<h3 id="e">E</h3>
<div class="items-add"> 
    <div class="item">5</div>
</div>

I want products with the same initials to be grouped together, I don't have an effective solution. I want to add a wrapper tag to the tags that have a class item in the loop using jquery is there any way I can do this . or have any other treatment let me know about your ideas. Thanks.

CodePudding user response:

You can solve it in php like this:

<?php $oldTitle = ""; ?>
<?php foreach ($forlop as $value) : ?>
            <?php 
                $stringTitle = substr($value->getTitle(), 0, 1);
            ?>
            <?php if($oldTitle !== $stringTitle && $oldTitle !== ""){
                echo "</div>"; // close items add tag
            ?>
            <?php if(is_numeric($stringTitle)){
                echo "<h3 id='other'>0-9</h3>";
            } else{
                echo "<h3 id=".strtolower($stringTitle).">".strtoupper($stringTitle)."</h3>";
            }?>
            <?php if($oldTitle !== $stringTitle){
                echo "<div class='items-add'>";
                $oldTitle = $stringTitle;
            ?>
            <div class="item"><?php echo $value->getId(); ?></div>            
<?php endforeach; ?>
  • Related