Page 1 of 3

how to display elements in an array in foreach loop

Posted: Mon Feb 20, 2006 12:37 pm
by kingconnections
ok so I have a foreach loop that loops through a series of items. I have another foreach loop that is nested inside of that one. I don't know how to display the 3rd element in my loop. Example

Code: Select all

foreach ($servers as $server)
{
echo "<tr><td>$server</td>";

foreach ($cols as $status)
{
echo "<td>[b]$status[/b]</td>";


}

echo "</tr>";
}
I want to display the Status of each server. The loop above this one loops through the servers.

When i run my page, If i have the inside set to foreach ($cols as $status) I get the correct format table, but the values just say array?

thanks

Dan

Posted: Mon Feb 20, 2006 12:52 pm
by feyd
each $status is an array.

Posted: Mon Feb 20, 2006 12:57 pm
by kingconnections
guess I don't quite follow you, here is the what I put it in:

Code: Select all

if (!isset($cols[$server])) 
{ 
  $cols[$server] = array(); 
} 
$cols[$server][$patch][$status] = array($install);  
      }

Posted: Mon Feb 20, 2006 1:00 pm
by feyd
to illustrate what I said: var_dump($status) or var_export($status) or print_r($status)

Posted: Mon Feb 20, 2006 1:16 pm
by kingconnections
hmm when I do that I only see one result, unlike the other time.

Posted: Mon Feb 20, 2006 1:54 pm
by kingconnections
but what is wierd is, that If i do the var_export of the $cols array they all show up?

So basically i just did a var_export on all the arrays and if i call then in as a single array they only contain what appears to be the last entry, but if i call $cols in var_export I see all the data.


What is up with that?

Dan

Posted: Mon Feb 20, 2006 2:00 pm
by shiznatix
read the manual on var_export() and your answer you shall recieve

Posted: Mon Feb 20, 2006 2:04 pm
by kingconnections
not sure I follow you, as I read the page and it said that it can not every value just parsible ones. But these all show up when i do it on the $cols array.

Posted: Mon Feb 20, 2006 3:42 pm
by kingconnections
ok so i think i found the answer to the issue. I think the internal pointer needs to be rest because i used it in a foreach loop more then once?

now - how do i implement that in my code?


Dan

Posted: Mon Feb 20, 2006 4:04 pm
by feyd
foreach() reset's the internal pointer automatically.

Posted: Mon Feb 20, 2006 4:49 pm
by kingconnections
Ok I think that I found the problem. here is my var_export of the $cols array



basically what u see is a server with the key's of MS06-001, Ok so I think I put these as keys instead of actual values that I can loop through? So under patch it should be patch[0] thru [2] but that does not exisit only one value of whatver [patchname here]
Image

Posted: Mon Feb 20, 2006 4:56 pm
by kingconnections
The end result of what this should look like is:

Image


so what do you think?

[/img]

Posted: Mon Feb 20, 2006 4:59 pm
by kingconnections
Except for on mine I am filtering for MS06 patches only, so only 3 colums will show up.

Posted: Mon Feb 20, 2006 5:00 pm
by kingconnections
Entire php script so you guys can see what is going on:

Code: Select all

<html>
<head>
<title>Net Tools</title>
<link rel="stylesheet" type="text/css" href="/stylesheet.css">

<style type="text/css">
<!--
body { background: #FFFFFF }
.light {
background: #909090;
}
.dark {
background: #808080;
}
//-->
</style>

</head>
<body>

<?php 

include('config_db.php');

/// this is to get the current year patches
$s = @mssql_connect($myServer, $myUser, $myPass) 
or die("Couldn't connect to SQL Server on $myServer"); 
$d = @mssql_select_db($myDB, $s) 
or die("Couldn't open database $myDB"); 
$patch = "MS06%";
$query1 = "SELECT DISTINCT (patch) 
	FROM PatchStatus WHERE patch like '$patch'
	ORDER BY patch";
$result1 = mssql_query($query1); 
$numRows = mssql_num_rows($result1); 
$current_patchs = array();
// Fetch results
while($row = mssql_fetch_array($result1)) 
{ 
$current_patch = $row["patch"];
$current_patchs[] = $current_patch;
} 

echo"<table><tr><th>Server Report</th></tr>";

echo "<tr><th>Server</th>";

// for loop to display patches sideways

foreach( $current_patchs as $patchs )
{
  echo "<td>".$patchs."</td>";
 }
echo "</tr><tr>";

//Passes the site code from url into query
$site = $_GET['scode']; 
//echo "the sitecode is: $site<br>"; 




// Defining Site codes, can get these from ECM DB as well, but did not know if ECM is going away or not.

$d = @mssql_select_db($myDB, $s) 
or die("Couldn't open database $myDB2"); 
$query1 = "SELECT DISTINCT (server) 
	FROM PatchStatus 
	WHERE site = '$site'
	ORDER BY server";
$result1 = mssql_query($query1); 
$numRows = mssql_num_rows($result1); 

// Fetch the servers per site 
while($row = mssql_fetch_array($result1)) 
{ 
$server = $row["server"];
$servers[]=$server;
} 



// input every servers info into 4d query  server,patch,status,install.

foreach ($servers as $server)
{
$query2 = "SELECT patch, status, installed 
	FROM PatchStatus 
	WHERE server = '$server' and patch like 'MS06%'
	ORDER BY server";
$result2 = mssql_query($query2); 
$numRows = mssql_num_rows($result2); 




while($row = mssql_fetch_array($result2)) 
{ 
$patch = $row["patch"];
$status = $row["status"];
$install = $row["installed"];





IF ($status == "Patched"){
$status = '<img src=/PatchManagement/images/check.gif height=12 border=0 alt=\"Patched\">';
}
If ($status =="Reboot"){
$status = "<img src=/PatchManagement/images/reboot.gif height=12 border=0 alt=\"Reboot $install\">";
}
If ($status =="Unknown"){
$status = "<img src=/PatchManagement/images/unknown.gif height=12 border=0 alt=\"Unknown\">";
}

If ($status =="Not Installed"){
$status = "<img src=/PatchManagement/images/nopatch.gif height=12 border=0 alt=\"Not Installed\">";
}
If ($status =="Unsupported OS"){
$status = "<img src=/PatchManagement/images/x.gif height=12 border=0 alt=\"Unsupported OS\">";
}



{
if (!isset($cols[$server])) 
{ 
  $cols[$server] = array(); 
  $cols[$patch] = array();
  $cols[$status] = array();
} 
$cols[$server][$patch][$status] = array($install);  
      } 
}
//echo "<tr><td>$server</td><td> $patch</td><td>$status</td>  ";
} 
//echo "</tr>";



$count1 = count($cols);
$count2 = count($servers);
$count3 = count ($patch);
$count4 = count($status);
$count5 = count($install);

echo "cols = $count1<br>";
echo "servers = $count2<br>";
echo "patch = $count3<br>";
echo "status = $count4<br>";
echo "install = $count5<br>";



foreach ($servers as $server)
{
echo "<tr><td>$server</td>";












echo "</tr>";
}


for ($i=0; $i <= ($count2 - 1); $i++)
{
echo "$servers[$i] <br>";
	for ($w=0; $w<=(7);$w++)
	{echo "$patch[$w] ";}

}

var_export($cols);
//var_export($status, true);

?> 

</body>
</html>

Posted: Mon Feb 20, 2006 8:02 pm
by Jenk
You've already been given the answer.. $status is an array, not a variable of string/integer/float/boolean.

Thus why when you try to echo the array it prints "Array".