Jump to content

Complete Noob PHP Question


Tom.
 Share

Recommended Posts

I have no idea why, but I can't solve this haha! I have a block system that groups two half blocks together. So it checks what comes before or after it to create the markup

  <div class="masonry flex flex-wrap -ml-1 -mt-1">
    <div class="masonry-sizer w-1/2 lg:w-1/3"></div>
    <?php foreach($value as $item): $item->block = $blocks[$item->size->value]; ?>
      <?php if($item->size->value == 'half' && $item->prev()->size->value == 'half'): ?>
      <?php else: ?>
        <div class="pl-1 mt-1 <?=$item->block['class']?>">
      <?php endif; ?>

        <?=$item->render()?>


      <?php if($item->size->value == 'half' && $item->next()->size->value == 'half'): ?>
      <?php else: ?>
        </div>
      <?php endif; ?>
    <?php endforeach; ?>
  </div>

So I've got the logic down, however I don't want the else. Easy solution, I just turn it into a false. 
 

  <div class="masonry flex flex-wrap -ml-1 -mt-1">
    <div class="masonry-sizer w-1/2 lg:w-1/3"></div>
    <?php foreach($value as $item): $item->block = $blocks[$item->size->value]; ?>
      <?php if($item->size->value != 'half' && $item->prev()->size->value != 'half'): ?>
        <div class="pl-1 mt-1 <?=$item->block['class']?>">
      <?php endif; ?>

        <?=$item->render()?>


      <?php if($item->size->value != 'half' && $item->next()->size->value != 'half'): ?>
        </div>
      <?php endif; ?>
    <?php endforeach; ?>
  </div>

I assumed that would work. I thought wrong! hahaha. 

Maybe my brain just isn't on it today. Anyone know what's happening here? 

Link to comment
Share on other sites

Boolean logic says that, if you want to negate a compound term, you need to negate each individual term AND change the operator. This is commonly referred to as De Morgan's law.

!(a and b) = !a or !b

Thus, you only need to change the and to or in your if clause:

<?php if($item->size->value != 'half' || $item->prev()->size->value != 'half'): ?>

 

  • Like 7
  • Thanks 1
Link to comment
Share on other sites

10 minutes ago, BitPoet said:

Boolean logic says that, if you want to negate a compound term, you need to negate each individual term AND change the operator. This is commonly referred to as De Morgan's law.


!(a and b) = !a or !b

Thus, you only need to change the and to or in your if clause:


<?php if($item->size->value != 'half' || $item->prev()->size->value != 'half'): ?>

 

Perfect response, thank you very much ? I just couldn't get my head around the logic for some reason. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...