Using modulo in a if statement

I have

<?php
$pduName = '';
foreach($pduArray as $pdu) {
	if ($pduName !== $pdu['pdu_name']) {
		$pduName = $pdu['pdu_name'];
		echo "\r\n";
		echo '<h4 class="text-secondary">'.$pduName.'</h4>';
		echo "\r\n";
	}
		echo '  <a href="show_recepticle.php?id='.$pdu['recepticle_id'].'" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="'.$pdu['name'].'">';
		echo '<img src="../images/'.$pdu['type'].'.png" class="figure-img img-fluid rounded" alt="Recepticle.">';
		echo "</a>";
		echo "\r\n";
}
?>

the result



<h4 class="text-secondary">Luke's PDU.</h4>
  <a href="show_recepticle.php?id=1" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Foxtrot"><img src="../images/1.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=2" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Chris Farley"><img src="../images/1.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=3" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="The Promised Neverland"><img src="../images/2.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=4" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Donald Trump"><img src="../images/6.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=5" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Luke Urtnowski"><img src="../images/4.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=7" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Loner"><img src="../images/4.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>

<h4 class="text-secondary">Wally's PDU.</h4>
  <a href="show_recepticle.php?id=6" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Peter Griffin"><img src="../images/3.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=9" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Rick & Morty"><img src="../images/1.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=10" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Desert Eagle"><img src="../images/5.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=11" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Tango"><img src="../images/2.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=12" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="The Patriot"><img src="../images/1.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>

<h4 class="text-secondary">Fred's PDU.</h4>
  <a href="show_recepticle.php?id=13" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="The Renegade"><img src="../images/3.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=14" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Eric Andre"><img src="../images/7.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=15" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Decker"><img src="../images/4.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>

<h4 class="text-secondary">Ian's PDU.</h4>
  <a href="show_recepticle.php?id=8" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Coronado"><img src="../images/2.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=16" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Nathan Explosion"><img src="../images/1.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>

<h4 class="text-secondary">Thor's PDU.</h4>
  <a href="show_recepticle.php?id=17" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Phil Hartman"><img src="../images/1.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>

<h4 class="text-secondary">Pete's PDU.</h4>
  <a href="show_recepticle.php?id=18" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Joe Rogan"><img src="../images/2.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=19" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Dan Crenshaw"><img src="../images/5.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=20" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Will Ferrell"><img src="../images/3.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=21" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Hannibal Burress"><img src="../images/9.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>

I’ve been trying to figure out how to wrap a
<div>
around each group like

<div>

<h4 class="text-secondary">Luke's PDU.</h4>
  <a href="show_recepticle.php?id=1" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Foxtrot"><img src="../images/1.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=2" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Chris Farley"><img src="../images/1.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=3" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="The Promised Neverland"><img src="../images/2.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=4" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Donald Trump"><img src="../images/6.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=5" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Luke Urtnowski"><img src="../images/4.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=7" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Loner"><img src="../images/4.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
</div>
...
...

If you index/pivot the data using the pdu_name, when you fetch it, you can just loop over the resulting arrays of rows of data to produce the output that you want.

Edit: which I see was covered in a previous thread about using PDO::FETCH_GROUP. The point of grouping the fetched data into arrays of rows of data, is that there are definitive locations in the code where you start and end each section. This eliminates the need for conditional logic and of repeating the code needed to test/end the last section, regardless of how complicated the start/end markup is. For the ‘balanced’ markup you want to add <div>...</div> using the existing method, you would need to add more conditional logic to test upon every new section if there is a previous ‘open’ section, close that section before starting the new section, and then you have to test/close any last section.

1 Like

ok, am using

$pduArray = $result->fetchAll(PDO::FETCH_GROUP);
echo '<pre>';print_r($pduArray);echo '</pre>';

And get

Array
(
    [Luke's PDU.] => Array
        (
            [0] => Array
                (
                    [type] => 1
                    [0] => 1
                    [recepticle_id] => 1
                    [1] => 1
                    [name] => Foxtrot
                    [2] => Foxtrot
                )

            [1] => Array
                (
                    [type] => 1
                    [0] => 1
                    [recepticle_id] => 2
                    [1] => 2
                    [name] => Chris Farley
                    [2] => Chris Farley
                )

            [2] => Array
                (
                    [type] => 2
                    [0] => 2
                    [recepticle_id] => 3
                    [1] => 3
                    [name] => The Promised Neverland
                    [2] => The Promised Neverland
                )

            [3] => Array
                (
                    [type] => 6
                    [0] => 6
                    [recepticle_id] => 4
                    [1] => 4
                    [name] => Donald Trump
                    [2] => Donald Trump
                )

            [4] => Array
                (
                    [type] => 4
                    [0] => 4
                    [recepticle_id] => 5
                    [1] => 5
                    [name] => Luke Urtnowski
                    [2] => Luke Urtnowski
                )

            [5] => Array
                (
                    [type] => 4
                    [0] => 4
                    [recepticle_id] => 7
                    [1] => 7
                    [name] => Loner
                    [2] => Loner
                )

        )

    [Wally's PDU.] => Array
        (
            [0] => Array
                (
                    [type] => 3
                    [0] => 3
                    [recepticle_id] => 6
                    [1] => 6
                    [name] => Peter Griffin
                    [2] => Peter Griffin
                )

            [1] => Array
                (
                    [type] => 1
                    [0] => 1
                    [recepticle_id] => 9
                    [1] => 9
                    [name] => Rick & Morty
                    [2] => Rick & Morty
                )

            [2] => Array
                (
                    [type] => 5
                    [0] => 5
                    [recepticle_id] => 10
                    [1] => 10
                    [name] => Desert Eagle
                    [2] => Desert Eagle
                )

            [3] => Array
                (
                    [type] => 2
                    [0] => 2
                    [recepticle_id] => 11
                    [1] => 11
                    [name] => Tango
                    [2] => Tango
                )

            [4] => Array
                (
                    [type] => 1
                    [0] => 1
                    [recepticle_id] => 12
                    [1] => 12
                    [name] => The Patriot
                    [2] => The Patriot
                )

        )

    [Fred's PDU.] => Array
        (
            [0] => Array
                (
                    [type] => 3
                    [0] => 3
                    [recepticle_id] => 13
                    [1] => 13
                    [name] => The Renegade
                    [2] => The Renegade
                )

            [1] => Array
                (
                    [type] => 7
                    [0] => 7
                    [recepticle_id] => 14
                    [1] => 14
                    [name] => Eric Andre
                    [2] => Eric Andre
                )

            [2] => Array
                (
                    [type] => 4
                    [0] => 4
                    [recepticle_id] => 15
                    [1] => 15
                    [name] => Decker
                    [2] => Decker
                )

        )

    [Ian's PDU.] => Array
        (
            [0] => Array
                (
                    [type] => 2
                    [0] => 2
                    [recepticle_id] => 8
                    [1] => 8
                    [name] => Coronado
                    [2] => Coronado
                )

            [1] => Array
                (
                    [type] => 1
                    [0] => 1
                    [recepticle_id] => 16
                    [1] => 16
                    [name] => Nathan Explosion
                    [2] => Nathan Explosion
                )

        )

    [Thor's PDU.] => Array
        (
            [0] => Array
                (
                    [type] => 1
                    [0] => 1
                    [recepticle_id] => 17
                    [1] => 17
                    [name] => Phil Hartman
                    [2] => Phil Hartman
                )

        )

    [Pete's PDU.] => Array
        (
            [0] => Array
                (
                    [type] => 2
                    [0] => 2
                    [recepticle_id] => 18
                    [1] => 18
                    [name] => Joe Rogan
                    [2] => Joe Rogan
                )

            [1] => Array
                (
                    [type] => 5
                    [0] => 5
                    [recepticle_id] => 19
                    [1] => 19
                    [name] => Dan Crenshaw
                    [2] => Dan Crenshaw
                )

            [2] => Array
                (
                    [type] => 3
                    [0] => 3
                    [recepticle_id] => 20
                    [1] => 20
                    [name] => Will Ferrell
                    [2] => Will Ferrell
                )

            [3] => Array
                (
                    [type] => 9
                    [0] => 9
                    [recepticle_id] => 21
                    [1] => 21
                    [name] => Hannibal Burress
                    [2] => Hannibal Burress
                )

        )

Which looks like what im looking for, but how do I output them so the arrays are grouped in a div?

foreach($pduArray[0] as $pdu) {
		echo "<div class='col-6'>";
		echo "\r\n";
		echo '<h4 class="text-secondary">'.$pdu.'</h4>';
		echo "\r\n";
	foreach($pdu as $recepticle) {

		echo '  <a href="show_recepticle.php?id='.$recepticle['recepticle_id'].'" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="'.$recepticle['name'].'">';
		echo '<img src="../images/'.$recepticle['type'].'.png" class="figure-img img-fluid rounded" alt="Recepticle.">';
		echo "</a>";
		echo "\r\n";
		echo '</div>';
		echo "\r\n";
	}	
} 

something like this?

foreach($pduArray as $pduName=>$group)
{
	// start the section here...
	// output the <div> and the <h4>...</h4> markup
	
	// loop over the rows of data within the group
	foreach($group as $pdu)
	{
		// output the <a ...>...</a> markup
		
	}
	// end the section here...
	// output the </div>
}
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.