Sorting an array

I have
image
which is produced with

	if(array_key_exists('blade_server',$assetArray)) { 
		foreach($assetArray['blade_server'] as $value) {\
                 //output link
		}	
	}	
	if(array_key_exists('server',$assetArray)) { 
		foreach($assetArray['blade_server'] as $value) {\
                 //output link
		}	
	}	

	if(array_key_exists('shelf',$assetArray)) { 
		foreach($assetArray['blade_server'] as $value) {\
                 //output link
		}	
	}	

Which is a little odd.
I think it would be better to group them by type.
Would this work?

foreach($assetArray as $key=>$val){ 

    foreach($val as $k=>$v){ 
        // $output link
    }
}

Is the data coming from a database query?

1 Like

Yes

$sql = 'SELECT type,assets.asset_id,
		blade_servers.name AS blade_server_name,blade_server_id,blade_server_type_id,blade_servers.beginning_ru AS blade_server_beginning_ru,blade_servers.ending_ru AS blade_server_ending_ru,
		kvms.name AS kvm_name,kvm_id,kvm_type_id,kvms.beginning_ru AS kvm_beginning_ru,kvms.ending_ru AS kvm_ending_ru,
		monitors.name AS monitor_name,monitor_id,monitors.beginning_ru AS monitor_beginning_ru,monitors.ending_ru AS monitor_ending_ru,
		servers.name AS server_name,server_id,servers.beginning_ru AS server_beginning_ru,servers.ending_ru AS server_ending_ru,
		shelf_id,shelfs.beginning_ru shelf_beginning_ru
		FROM assets 
		LEFT JOIN blade_servers ON assets.asset_id = blade_servers.asset_id
		LEFT JOIN kvms ON assets.asset_id = kvms.asset_id
		LEFT JOIN monitors ON assets.asset_id = monitors.asset_id
		LEFT JOIN servers ON assets.asset_id = servers.asset_id
		LEFT JOIN shelfs ON assets.asset_id = shelfs.asset_id
		WHERE rack_id = '.$rack_id.' AND type !="rack"';

//echo $sql;
$result = $pdo->query($sql);
$row_cnt = $result->rowCount();				

$assetArray = $result->fetchAll(PDO::FETCH_GROUP);
``

[off-topic]
May I suggest writing setting the $sql statement like the following because it is so much easier to see at a glance what is happening and it is also far easier to modify:

<?php declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', 'true');

$rack_id = 'DEBUG_$rack_id';

$sql = '
  SELECT 
    type, 
    assets.asset_id,
    blade_servers.name AS blade_server_name,
    blade_server_id,
    blade_server_type_id,
    blade_servers.beginning_ru AS blade_server_beginning_ru,
    blade_servers.ending_ru AS blade_server_ending_ru,
    kvms.name AS kvm_name,
    kvm_id,kvm_type_id,kvms.beginning_ru AS kvm_beginning_ru,
    kvms.ending_ru AS kvm_ending_ru,
    monitors.name AS monitor_name,
    monitor_id,
    monitors.beginning_ru AS monitor_beginning_ru,
    monitors.ending_ru AS monitor_ending_ru,
    servers.name AS server_name,
    server_id,
    servers.beginning_ru AS server_beginning_ru,
    servers.ending_ru AS server_ending_ru,
    shelf_id,
    shelfs.beginning_ru shelf_beginning_ru
  FROM 
    assets 
  LEFT JOIN 
    blade_servers ON assets.asset_id = blade_servers.asset_id
  LEFT JOIN 
    kvms ON assets.asset_id = kvms.asset_id
  LEFT JOIN 
    monitors ON assets.asset_id = monitors.asset_id
  LEFT JOIN 
    servers ON assets.asset_id = servers.asset_id
  LEFT JOIN 
    shelfs ON assets.asset_id = shelfs.asset_id
  WHERE 
    rack_id = ' .$rack_id .' AND type !="rack"
  ';

// DEBUG     
   echo '<pre> $sql ==> '; // add line-feeds
     echo $sql;
   echo '</pre>';  

Output

$sql ==> 
  SELECT 
    type, 
    assets.asset_id,
    blade_servers.name AS blade_server_name,
    blade_server_id,
    blade_server_type_id,
    blade_servers.beginning_ru AS blade_server_beginning_ru,
    blade_servers.ending_ru AS blade_server_ending_ru,
    kvms.name AS kvm_name,
    kvm_id,kvm_type_id,kvms.beginning_ru AS kvm_beginning_ru,
    kvms.ending_ru AS kvm_ending_ru,
    monitors.name AS monitor_name,
    monitor_id,
    monitors.beginning_ru AS monitor_beginning_ru,
    monitors.ending_ru AS monitor_ending_ru,
    servers.name AS server_name,
    server_id,
    servers.beginning_ru AS server_beginning_ru,
    servers.ending_ru AS server_ending_ru,
    shelf_id,
    shelfs.beginning_ru shelf_beginning_ru
  FROM 
    assets 
  LEFT JOIN 
    blade_servers ON assets.asset_id = blade_servers.asset_id
  LEFT JOIN 
    kvms ON assets.asset_id = kvms.asset_id
  LEFT JOIN 
    monitors ON assets.asset_id = monitors.asset_id
  LEFT JOIN 
    servers ON assets.asset_id = servers.asset_id
  LEFT JOIN 
    shelfs ON assets.asset_id = shelfs.asset_id
  WHERE 
    rack_id = DEBUG_$rack_id AND type !="rack"
  

Also are these parameters correct or should they be separated by a comma?

shelfs.beginning_ru shelf_beginning_ru

[/off-topic]

It’s aliasing the field. there is an implied AS between them.

1 Like

You are grouping them by type.

Will group by type.

That said, if you’re wanting them in the order ‘blade_server’, ‘server’, ‘shelf’, you could ORDER your query by type, pull the entire set as a fetchAll(PDO::FETCH_ASSOC), and just walk the array in its entirity in one loop:

foreach($assetArray as $asset) {
// At this point, the item name is $asset[substr($asset[type],0,-1)."_name"]; because of how you have named your fields...
}

(Though I do question your database design, if you’ve got that many tables with all the same fields… shouldnt the data just be in the “assets” table?)

1 Like

@lurtnowski any chance of posting the SHOW CREATE TABLE query outputs for the tables involved?

1 Like

sure, heres the assets table

CREATE TABLE assets (
   asset_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
   type ENUM ('ac_unit','ats','chassis','furniture','generator','kvm','monitor','network_card','network_module','network_panel','network_standard','panel','pdu','periphial_storage','power_strip','blade_server','server','recepticle','ups','utility','rack','shelf'),
   ac_unit_id TINYINT UNSIGNED,
   ats_id TINYINT UNSIGNED,
   furniture_id TINYINT UNSIGNED,
   generator_id TINYINT UNSIGNED,  
   pdu_id TINYINT UNSIGNED,
   panel_id SMALLINT UNSIGNED,
   recepticle_id SMALLINT UNSIGNED, 
   power_strip_id SMALLINT UNSIGNED,
   utility_id TINYINT UNSIGNED,
   ups_id TINYINT UNSIGNED,
   rack_id INT UNSIGNED,   
   created_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
   created_by VARCHAR(50) DEFAULT 'lurtnowski@industechnology.com',
   updated_date TIMESTAMP,
   updated_by VARCHAR(50),
   FOREIGN KEY ( ac_unit_id ) REFERENCES ac_units ( ac_unit_id ),
   FOREIGN KEY ( ats_id ) REFERENCES atss ( ats_id ),
   FOREIGN KEY ( furniture_id ) REFERENCES furnishings ( furniture_id ),
   FOREIGN KEY ( generator_id ) REFERENCES generators ( generator_id ),
   FOREIGN KEY ( pdu_id ) REFERENCES pdus ( pdu_id ),
   FOREIGN KEY ( panel_id ) REFERENCES panels ( panel_id ),
   FOREIGN KEY ( recepticle_id ) REFERENCES recepticles ( recepticle_id ),
   FOREIGN KEY ( power_strip_id ) REFERENCES power_strips ( power_strip_id ),
   FOREIGN KEY ( ups_id ) REFERENCES upss ( ups_id ),
   FOREIGN KEY ( utility_id ) REFERENCES utilities ( utility_id ),
   FOREIGN KEY ( rack_id ) REFERENCES racks ( rack_id ),
   PRIMARY KEY ( asset_id )
);

The query to simply get all the assets (of several types) into an array ix

'SELECT type,assets.asset_id,
		blade_servers.name AS blade_server_name,blade_server_id,blade_server_type_id,blade_servers.beginning_ru AS blade_server_beginning_ru,blade_servers.ending_ru AS blade_server_ending_ru,
		kvms.name AS kvm_name,kvm_id,kvm_type_id,kvms.beginning_ru AS kvm_beginning_ru,kvms.ending_ru AS kvm_ending_ru,
		monitors.name AS monitor_name,monitor_id,monitors.beginning_ru AS monitor_beginning_ru,monitors.ending_ru AS monitor_ending_ru,
		network_cards.name AS network_card_name,network_card_id,network_cards.beginning_ru AS network_card_beginning_ru,network_cards.ending_ru AS network_card_ending_ru,
		servers.name AS server_name,server_id,servers.beginning_ru AS server_beginning_ru,servers.ending_ru AS server_ending_ru,
		shelf_id,shelfs.beginning_ru shelf_beginning_ru
		FROM assets 
		LEFT JOIN blade_servers ON assets.asset_id = blade_servers.asset_id
		LEFT JOIN kvms ON assets.asset_id = kvms.asset_id
		LEFT JOIN monitors ON assets.asset_id = monitors.asset_id
		LEFT JOIN network_cards ON assets.asset_id = network_cards.asset_id
		LEFT JOIN servers ON assets.asset_id = servers.asset_id
		LEFT JOIN shelfs ON assets.asset_id = shelfs.asset_id
		WHERE rack_id = '.$rack_id.' AND 
                (type ="blade_server" OR type ="kvm" OR type ="monitor" OR 
                 type ="network_card" OR type ="servers" OR type ="shelfs")'