Page 1 of 1

splitting arrays

Posted: Thu Mar 22, 2012 5:17 am
by IGGt
I have an array which contains a list of databases, similar to the following:
[text]$ListDBArray
[$groupID]
[$dbID] [$Item] => Value[/text]

which prints out the following

Code: Select all

Array ( 
	[1] => Array ( 
		[0] => Array ( [database] => Slave 1 [dbGroup] => 1 [IO] => Yes [SQL] => Yes [Seconds] => 0 [Last_Error] => ) 
		[1] => Array ( [database] => Slave 2 [dbGroup] => 1 [IO] => Yes [SQL] => Yes [Seconds] => 0 [Last_Error] => ) )
	[2] => Array ( 
		[2] => Array ( [database] => A Slave [dbGroup] => 2 [IO] => Yes [SQL] => Yes [Seconds] => 0 [Last_Error] => ) 
		[3] => Array ( [database] => O Slave [dbGroup] => 2 [IO] => Yes [SQL] => Yes [Seconds] => 0 [Last_Error] => )  
	)
)
I am then trying to break this into individual arrays, based on the groupID, so I would get something like:

Code: Select all

//Group ID 1
	Array ( 
		[0] => Array ( [database] => Slave 1 [dbGroup] => 1 [IO] => Yes [SQL] => Yes [Seconds] => 0 [Last_Error] => ) 
		[1] => Array ( [database] => Slave 2 [dbGroup] => 1 [IO] => Yes [SQL] => Yes [Seconds] => 0 [Last_Error] => ) )
//Group ID  2
	Array ( 
		[2] => Array ( [database] => A Slave [dbGroup] => 2 [IO] => Yes [SQL] => Yes [Seconds] => 0 [Last_Error] => ) 
		[3] => Array ( [database] => O Slave [dbGroup] => 2 [IO] => Yes [SQL] => Yes [Seconds] => 0 [Last_Error] => ) ) 
	
)
I'm sure that this must be quite straightforward, but I can't for the life of me get my head around how to do this.

Re: splitting arrays

Posted: Thu Mar 22, 2012 12:39 pm
by AbraCadaver
I don't see the practicality of this. You would have multiple arrays with different instead of the one array with multiple indexes.

Re: splitting arrays

Posted: Fri Mar 23, 2012 11:15 am
by IGGt
Maybe I'm going about this the wrong way then.

Essentially what I have is a list of Databases in the original array, which are split up into 4 groups.

I then wanted to use those 4 groups to create 4 tables, which I could then use a javascript function to hide and show the rows of each table in turn.

I have managed to achieve this aim, but I have had to do it creating 4 distinct parts:

e.g.

Code: Select all

$sqDB1 = "";
$sqDB2 = "";
$sqDB3 = "";
$sqDB4 = "";

foreach($ss as $dbs) {
	foreach($dbs as $dbg) {
		if ($dbg['dbGroup'] == 1) {
			if ($dbg['IO'] == "No" || $dbg['SQL'] == "No") {	
				$sqCol1 = "#$c7";
				if ($sqDB1 == ""){$sqDB1 = $dbg['database'];}else{$sqDB1 = $sqDB1.",".$dbg['database'];	}		
			}
		}else if ($dbg['dbGroup'] == 2) {
			...
		}else if ($dbg['dbGroup'] == 3) {
			....
		}else if ($dbg['dbGroup'] == 4) {
			.....
		}
	}
};

if ($sqDB1 == ""){
	$sqDB1 = "All OK";
	$sqDBArray1[0] = "All OK";
	$sqDBArray1[1] = "All OK";
	$sqCol1 = "#$c2";
	$sqDB1C = 0;
}else{
	$sqDBArray1 = explode(',',$sqDB1);	//Create a PHP array
	$sqDB1C = count($sqDBArray1);
	if ($sqDB1C == 1 ) {array_push($sqDBArray1,$sqDB1);};
};


//REPEAT THE ABOVE SECTION 4 TIMES WITH $sqDB2, $sqDB3 etc.

Code: Select all

<script language="javascript">
			//HIDE ALL THE ROWS
			function hideRows()
			{
				var a = document.getElementById("dbTable1");
				var len = a.rows.length;
				for(i=1 ; i< len; i++)
				{
					a.rows[i].style.display = 'none';
				}
				
				//REPEAT 4 TIMES FOR dbTable2, dbTable3 etc.
			}
		
			//SHOW EACH ROW	
			var a = 1;
			var b = 1;
			var c = 1;
			var d = 1;
			function showRows()
			{
				var e = document.getElementById("dbTable1");
				var len = e.rows.length;
				e.rows[a].style.display = 'block';
				a=a+1;
				if(a==len)
				{
					a=1;
				};
				//REPEAT 4 TIMES FOR dbTable2, dbTable3 etc.
				
			}
			
			//TIMER
			function hideShow()
			{
				hideRows();
				showRows();
			}
		</script>

Code: Select all

<div style="position:absolute; left:0%; top:50%; display:block;">	
		<?php
		print "	<table id=\"dbTable1\" class=\"Round\" style=\"width:280px; position:relative; background-color:$sqCol1; padding:2px; z-index:2; display:block;\">\n";
		print "		<tr $hcol>\n";
		print "			<th class=\"RoundTop\">GROUP1 ( $sqDB1C )</th>\n";
		print "		</tr>\n";
		$a = 0;
		foreach($sqDBArray1 AS $sq){
			if($a == 1){$disp = 'block';}else{$disp = 'none';};	//INITIAL DISPLAY STATUS
		print "		<tr id=\"$a\" style=\"display:$disp;\">\n";
		print "			<th class=\"RoundBottom\" style=\"vertical-align:middle; width:280px; height:50px;\">";
		print $sq;
		print "			</th>\n";
		print "		</tr>\n";	
		$a++;
		};	
		print "	</table>\n";
		?>

//REPEAT THIS 4 TIMES FOR dbTable2, dbTable3, etc.
	</div>
It works, but to me it looks very messy, and I'm sure there must be a better option. Any hints would be appreciated.