how to display elements in an array in foreach loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

how to display elements in an array in foreach loop

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

each $status is an array.
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post 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);  
      }
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

to illustrate what I said: var_dump($status) or var_export($status) or print_r($status)
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post by kingconnections »

hmm when I do that I only see one result, unlike the other time.
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post 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
Last edited by kingconnections on Mon Feb 20, 2006 2:00 pm, edited 1 time in total.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

read the manual on var_export() and your answer you shall recieve
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post 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.
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

foreach() reset's the internal pointer automatically.
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post 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
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post by kingconnections »

The end result of what this should look like is:

Image


so what do you think?

[/img]
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post by kingconnections »

Except for on mine I am filtering for MS06 patches only, so only 3 colums will show up.
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post 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>
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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".
Post Reply