Combine loops?

I have this bit of PHP

if($ps_result = mysqli_query($conn, $sql)){
    if(mysqli_num_rows($ps_result) > 0){
        echo '<select class="form-control" id="Source" name="Source" required>';
            echo '<option disabled selected value="">Select</option>';
        while($row = mysqli_fetch_array($ps_result)){
           echo '<option disabled>'.$row['pdu_name']."</option>";
		   echo "\r\n";
           echo '    <option value="'.$row['power_strip_id'].'">&nbsp;&nbsp;'.$row['name']."</option>";
		   echo "\r\n";
        }
        echo "</select>";
    } else{
        echo "No power strips were found.";
    }						      
}

which results in

<select class="form-control" id="Source" name="Source" required><option disabled selected value="">Select</option><option disabled>Fred's PDU.</option>
    <option value="13">&nbsp;&nbsp;Luke's Power Strip</option>
<option disabled>Fred's PDU.</option>
    <option value="14">&nbsp;&nbsp;Ian's Power Strip</option>
<option disabled>Fred's PDU.</option>
    <option value="15">&nbsp;&nbsp;Wally's Power Strip</option>
<option disabled>Ian's PDU.</option>
    <option value="8">&nbsp;&nbsp;Ian's Power Strip</option>
<option disabled>Ian's PDU.</option>
    <option value="16">&nbsp;&nbsp;Slick Rick's Power Strip</option>
<option disabled>Luke's PDU.</option>
    <option value="1">&nbsp;&nbsp;Luke's Power Strip</option>
<option disabled>Luke's PDU.</option>
    <option value="2">&nbsp;&nbsp;Ian's Power Strip</option>
<option disabled>Luke's PDU.</option>
    <option value="3">&nbsp;&nbsp;Wally's Power Strip</option>
<option disabled>Luke's PDU.</option>
    <option value="4">&nbsp;&nbsp;Slick Rick's Power Strip</option>
<option disabled>Luke's PDU.</option>
    <option value="5">&nbsp;&nbsp;Hot Carl's Power Strip</option>
<option disabled>Luke's PDU.</option>
    <option value="7">&nbsp;&nbsp;Luke's Power Strip</option>
<option disabled>Pete's PDU.</option>
    <option value="18">&nbsp;&nbsp;George's Power Strip</option>
<option disabled>Pete's PDU.</option>
    <option value="19">&nbsp;&nbsp;Luke's Power Strip</option>
<option disabled>Pete's PDU.</option>
    <option value="20">&nbsp;&nbsp;Ian's Power Strip</option>
<option disabled>Pete's PDU.</option>
    <option value="21">&nbsp;&nbsp;Ian's Power Strip</option>
<option disabled>Thor's PDU.</option>
    <option value="17">&nbsp;&nbsp;Hot Carl's Power Strip</option>
<option disabled>Wally's PDU.</option>
    <option value="6">&nbsp;&nbsp;George's Power Strip</option>
<option disabled>Wally's PDU.</option>
    <option value="9">&nbsp;&nbsp;Wally's Power Strip</option>
<option disabled>Wally's PDU.</option>
    <option value="10">&nbsp;&nbsp;Slick Rick's Power Strip</option>
<option disabled>Wally's PDU.</option>
    <option value="11">&nbsp;&nbsp;Hot Carl's Power Strip</option>
<option disabled>Wally's PDU.</option>
    <option value="12">&nbsp;&nbsp;George's Power Strip</option>
</select>	

Id like the output to be

<select class="form-control" id="Source" name="Source" required>
<option disabled selected value="">Select</option>
<option disabled>Fred's PDU.</option>
    <option value="13">&nbsp;&nbsp;Luke's Power Strip</option>
    <option value="14">&nbsp;&nbsp;Ian's Power Strip</option>
    <option value="15">&nbsp;&nbsp;Wally's Power Strip</option>
<option disabled>Ian's PDU.</option>
    <option value="8">&nbsp;&nbsp;Ian's Power Strip</option>
    <option value="16">&nbsp;&nbsp;Slick Rick's Power Strip</option>
<option disabled>Luke's PDU.</option>
    <option value="1">&nbsp;&nbsp;Luke's Power Strip</option>
    <option value="2">&nbsp;&nbsp;Ian's Power Strip</option>
    <option value="3">&nbsp;&nbsp;Wally's Power Strip</option>
    <option value="4">&nbsp;&nbsp;Slick Rick's Power Strip</option>
    <option value="5">&nbsp;&nbsp;Hot Carl's Power Strip</option>
    <option value="7">&nbsp;&nbsp;Luke's Power Strip</option>
<option disabled>Pete's PDU.</option>
    <option value="18">&nbsp;&nbsp;George's Power Strip</option>
    <option value="19">&nbsp;&nbsp;Luke's Power Strip</option>
    <option value="20">&nbsp;&nbsp;Ian's Power Strip</option>
    <option value="21">&nbsp;&nbsp;Ian's Power Strip</option>
<option disabled>Thor's PDU.</option>
    <option value="17">&nbsp;&nbsp;Hot Carl's Power Strip</option>
<option disabled>Wally's PDU.</option>
    <option value="6">&nbsp;&nbsp;George's Power Strip</option>
    <option value="9">&nbsp;&nbsp;Wally's Power Strip</option>
    <option value="10">&nbsp;&nbsp;Slick Rick's Power Strip</option>
    <option value="11">&nbsp;&nbsp;Hot Carl's Power Strip</option>
    <option value="12">&nbsp;&nbsp;George's Power Strip</option>
</select>

I think I should alter the loop like

while($row = mysqli_fetch_array($ps_result)){
    if($row['pdu_name'] = ) {
           echo '<option disabled>'.$row['pdu_name']."</option>";
	  echo "\r\n";
    }
           echo '    <option value="'.$row['power_strip_id'].'">&nbsp;&nbsp;'.$row['name']."</option>";
	   echo "\r\n"; 
}

But what do I test for?

Sounds like you’re looking for optgroup rather than <option disabled ?

3 Likes

Yes, this is more about HTML/CSS rather than your PHP.
But…

…Should not be an option, it should be a label, outside of the select.

<label for="Source">Select Source</label>

Each of the disabled options should be optgroups.

And two consecutive &nbsp; should never be seen (same goes for <br>).
This should be CSS, Eg.

.form-control option {
    padding-left: 2em ;
}

Once we get those points out of the way, we can look at how to make these groups in PHP.
That really depends upon the data you fetch (and possibly how you fetch it).
We can see the data you fetched in your resulting HTML (if a little hard to read mixed in with that).
Given this task, I may be looking to some of the neat fetch modes that PDO has to offer, probably PDO::FETCH_GROUP, though this really depends upon the nature of the data in your database, which we are not party to.
But choosing the right fetch mode can take a lot of the donkey work out of organising your data.

1 Like

The essence of the question seems to be how to group by pdu_name. Sometimes it can help to strip away extraneous stuff and just focus on the basics:

$rows = [
    [ 'pdu_name' => "Fred's PDU.", 'name' => "Luke's Power Strip", 'power_strip_id' => 13 ],
    [ 'pdu_name' => "Fred's PDU.", 'name' => "Ian's Power Strip",  'power_strip_id' => 14 ],
    [ 'pdu_name' => "Fred's PDU.", 'name' => "Luke's Power Strip", 'power_strip_id' => 15 ],

    [ 'pdu_name' => "Ian's PDU.", 'name' => "Ian's Power Strip", 'power_strip_id' => 8 ],
    [ 'pdu_name' => "Ian's PDU.", 'name' => "Slick Rick's Power Strip", 'power_strip_id' => 16 ],

    [ 'pdu_name' => "Luke's PDU.", 'name' => "Luke's Power Strip", 'power_strip_id' => 1 ],
    [ 'pdu_name' => "Luke's PDU.", 'name' => "Ian's Power Strip",  'power_strip_id' => 2 ],
    [ 'pdu_name' => "Luke's PDU.", 'name' => "Wally's Power Strip", 'power_strip_id' => 3 ],
];
    $pduName = '';
    foreach($rows as $row) {
        if ($pduName !== $row['pdu_name']) {
            $pduName = $row['pdu_name'];
            echo $pduName . "\n";
        }
        echo "  " . $row['power_strip_id'] . " " . $row['name'] . "\n";
    }

Results in:

Fred's PDU.
  13 Luke's Power Strip
  14 Ian's Power Strip
  15 Luke's Power Strip
Ian's PDU.
  8 Ian's Power Strip
  16 Slick Rick's Power Strip
Luke's PDU.
  1 Luke's Power Strip
  2 Ian's Power Strip
  3 Wally's Power Strip

You can go on from there.

ok, made a go at the PDO thing, but ran into a few questions…

try{
	$sql = "...";
	
	$result = $pdo->query($sql);

echo '<pre>';print_r($result->fetch());echo '</pre>';
echo '<pre>';print_r($result->fetchAll());echo '</pre>';

    if($result->rowCount() > 0){
      echo '<select class="form-control" id="Source" name="Source" required>';
      echo '<option value="">Select</option>';
        while($row = $result->fetchAll(PDO::FETCH_GROUP)){
           echo '<optgroup label="'.$row['pdu_name'].'">';
		   echo "\r\n";
           echo '    <option value="'.$row['power_strip_id'].'">'.$row['name']."</option>";
		   echo "\r\n";
		   echo "</optgroup>";
		   echo "\r\n";
        }
      echo "</select>";
        // Free result set
        unset($result);
    } else{
        echo "No pdus were found.";
    }
} catch(PDOException $e){
    die("ERROR: Could not able to execute $sql. " . $e->getMessage());
}

the first 2 echo statements return

 Source SELECT pdus.name AS pdu_name, power_strip_id,power_strips.name, recepticles.name AS recepticles_name FROM power_strips INNER JOIN recepticles ON power_strips.recepticle_id = recepticles.recepticle_id INNER JOIN circuit_breakers ON recepticles.circuit_breaker_id = circuit_breakers.circuit_breaker_id INNER JOIN pdus ON circuit_breakers.pdu_id = pdus.pdu_id ORDER BY pdus.name

Array
(
    [pdu_name] => Fred's PDU.
    [0] => Fred's PDU.
    [power_strip_id] => 13
    [1] => 13
    [name] => Luke's Power Strip
    [2] => Luke's Power Strip
    [recepticles_name] => The Renegade
    [3] => The Renegade
)

Array
(
    [0] => Array
        (
            [pdu_name] => Fred's PDU.
            [0] => Fred's PDU.
            [power_strip_id] => 14
            [1] => 14
            [name] => Ian's Power Strip
            [2] => Ian's Power Strip
            [recepticles_name] => Eric Andre
            [3] => Eric Andre
        )

    [1] => Array
        (
            [pdu_name] => Fred's PDU.
            [0] => Fred's PDU.
            [power_strip_id] => 15
            [1] => 15
            [name] => Wally's Power Strip
            [2] => Wally's Power Strip
            [recepticles_name] => Decker
            [3] => Decker
        )

    [2] => Array
        (
            [pdu_name] => Ian's PDU.
            [0] => Ian's PDU.
            [power_strip_id] => 8
            [1] => 8
            [name] => Ian's Power Strip
            [2] => Ian's Power Strip
            [recepticles_name] => Coronado
            [3] => Coronado
        )

    [3] => Array
        (
            [pdu_name] => Ian's PDU.
            [0] => Ian's PDU.
            [power_strip_id] => 16
            [1] => 16
            [name] => Slick Rick's Power Strip
            [2] => Slick Rick's Power Strip
            [recepticles_name] => Nathan Explosion
            [3] => Nathan Explosion
        )

    [4] => Array
        (
            [pdu_name] => Luke's PDU.
            [0] => Luke's PDU.
            [power_strip_id] => 1
            [1] => 1
            [name] => Luke's Power Strip
            [2] => Luke's Power Strip
            [recepticles_name] => Foxtrot
            [3] => Foxtrot
        )

    [5] => Array
        (
            [pdu_name] => Luke's PDU.
            [0] => Luke's PDU.
            [power_strip_id] => 2
            [1] => 2
            [name] => Ian's Power Strip
            [2] => Ian's Power Strip
            [recepticles_name] => Chris Farley
            [3] => Chris Farley
        )

    [6] => Array
        (
            [pdu_name] => Luke's PDU.
            [0] => Luke's PDU.
            [power_strip_id] => 3
            [1] => 3
            [name] => Wally's Power Strip
            [2] => Wally's Power Strip
            [recepticles_name] => The Promised Neverland
            [3] => The Promised Neverland
        )

    [7] => Array
        (
            [pdu_name] => Luke's PDU.
            [0] => Luke's PDU.
            [power_strip_id] => 4
            [1] => 4
            [name] => Slick Rick's Power Strip
            [2] => Slick Rick's Power Strip
            [recepticles_name] => Donald Trump
            [3] => Donald Trump
        )

    [8] => Array
        (
            [pdu_name] => Luke's PDU.
            [0] => Luke's PDU.
            [power_strip_id] => 5
            [1] => 5
            [name] => Hot Carl's Power Strip
            [2] => Hot Carl's Power Strip
            [recepticles_name] => Luke Urtnowski
            [3] => Luke Urtnowski
        )

    [9] => Array
        (
            [pdu_name] => Luke's PDU.
            [0] => Luke's PDU.
            [power_strip_id] => 7
            [1] => 7
            [name] => Luke's Power Strip
            [2] => Luke's Power Strip
            [recepticles_name] => Loner
            [3] => Loner
        )

    [10] => Array
        (
            [pdu_name] => Pete's PDU.
            [0] => Pete's PDU.
            [power_strip_id] => 18
            [1] => 18
            [name] => George's Power Strip
            [2] => George's Power Strip
            [recepticles_name] => Joe Rogan
            [3] => Joe Rogan
        )

    [11] => Array
        (
            [pdu_name] => Pete's PDU.
            [0] => Pete's PDU.
            [power_strip_id] => 19
            [1] => 19
            [name] => Luke's Power Strip
            [2] => Luke's Power Strip
            [recepticles_name] => Dan Crenshaw
            [3] => Dan Crenshaw
        )

    [12] => Array
        (
            [pdu_name] => Pete's PDU.
            [0] => Pete's PDU.
            [power_strip_id] => 20
            [1] => 20
            [name] => Ian's Power Strip
            [2] => Ian's Power Strip
            [recepticles_name] => Will Ferrell
            [3] => Will Ferrell
        )

    [13] => Array
        (
            [pdu_name] => Pete's PDU.
            [0] => Pete's PDU.
            [power_strip_id] => 21
            [1] => 21
            [name] => Ian's Power Strip
            [2] => Ian's Power Strip
            [recepticles_name] => Hannibal Burress
            [3] => Hannibal Burress
        )

    [14] => Array
        (
            [pdu_name] => Thor's PDU.
            [0] => Thor's PDU.
            [power_strip_id] => 17
            [1] => 17
            [name] => Hot Carl's Power Strip
            [2] => Hot Carl's Power Strip
            [recepticles_name] => Phil Hartman
            [3] => Phil Hartman
        )

    [15] => Array
        (
            [pdu_name] => Wally's PDU.
            [0] => Wally's PDU.
            [power_strip_id] => 6
            [1] => 6
            [name] => George's Power Strip
            [2] => George's Power Strip
            [recepticles_name] => Peter Griffin
            [3] => Peter Griffin
        )

    [16] => Array
        (
            [pdu_name] => Wally's PDU.
            [0] => Wally's PDU.
            [power_strip_id] => 9
            [1] => 9
            [name] => Wally's Power Strip
            [2] => Wally's Power Strip
            [recepticles_name] => Rick & Morty
            [3] => Rick & Morty
        )

    [17] => Array
        (
            [pdu_name] => Wally's PDU.
            [0] => Wally's PDU.
            [power_strip_id] => 10
            [1] => 10
            [name] => Slick Rick's Power Strip
            [2] => Slick Rick's Power Strip
            [recepticles_name] => Desert Eagle
            [3] => Desert Eagle
        )

    [18] => Array
        (
            [pdu_name] => Wally's PDU.
            [0] => Wally's PDU.
            [power_strip_id] => 11
            [1] => 11
            [name] => Hot Carl's Power Strip
            [2] => Hot Carl's Power Strip
            [recepticles_name] => Tango
            [3] => Tango
        )

    [19] => Array
        (
            [pdu_name] => Wally's PDU.
            [0] => Wally's PDU.
            [power_strip_id] => 12
            [1] => 12
            [name] => George's Power Strip
            [2] => George's Power Strip
            [recepticles_name] => The Patriot
            [3] => The Patriot
        )

)

whats the difference between fetch() and fetchAll()?
Also I dont think i m using PDO::FETCH_GROUP right as I got nothing, I thought it would group by the first column

But when I try to run the query, it works…

And how would I use a foreach loop instead like ahundiak?

try{
	$sql = "SELECT pdus.name AS pdu_name, power_strip_id,power_strips.name,
		recepticles.name AS recepticles_name
		FROM power_strips
		INNER JOIN recepticles ON power_strips.recepticle_id = recepticles.recepticle_id 
		INNER JOIN circuit_breakers ON recepticles.circuit_breaker_id = circuit_breakers.circuit_breaker_id
		INNER JOIN pdus ON circuit_breakers.pdu_id = pdus.pdu_id
		ORDER BY pdus.name";
	
	$result = $pdo->query($sql);

	$pduArray = $result->fetchAll();
      echo '<select class="form-control" id="Source" name="Source" required>';
      echo '<option value="">Select</option>';
	
    $pduName = '';
    foreach($pduArray as $pdu) {
        if ($pduName !== $pdu['pdu_name']) {
            $pduName = $pdu['pdu_name'];
           echo '<optgroup label="'.$pduName.'">';
		   echo "\r\n";
        }
	   echo '    <option value="'.$pdu['power_strip_id'].'">'.$pdu['name']."</option>";
	   echo "\r\n";
    }
      echo "</select>";
} catch(PDOException $e){
    die("ERROR: Could not able to execute $sql. " . $e->getMessage());
}

seems to work

fetch() fetches the next row in the results set into an array, fetchAll() fetches all rows in a single hit, into a two-dimensional array.

Never even knew there was a [PDO:FETCH_GROUP option] (https://www.php.net/manual/en/pdostatement.fetchall.php). Look at Example 3 in the link. You need a FETCH_COLUMN to tell it which column to group by. In which case you would end up with an array of arrays of arrays. Seems more trouble than it is worth. I showed you how to group by a value in my previous post.

I would strongly suggest that you take some time and get comfortable with running php from your console command line. Right now you are trying to debug your queries and html at the same time by refreshing your browser. I think you find it easier in the long run to move your queries into their own functions and debug them separately from the rendering.

1 Like

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