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

Post by kingconnections »

Well, I am not sure how I am to fix it. I am sorry if you guys say that you are giving me the answer, but I am still not seeing it. so should i be echo'ing status[variable] like so?

Thanks

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

Post by kingconnections »

OK, so 1st off I would like to apologize for my lack of under standing. If I did something to <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> you guys off at me I am sorry. I just want to understand how I get the data out of the status array so that I can display it on the page in the format that was asked. I can not help that I am not understanding you guys, as it is lack of knowlegde of php or lack of knowledge of how the arrays would be displayed. That is why I posted on this forum to seek help. I Kingconnections am a NOOB! LOL Any further help you could give me on how to understand what I need to do would be great, but if you don't want to input thats ok too. Thank you everyone for your time.

Dan
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

If your code were not so messy I would jump at the chance to help, can you post the output of print_r() on your array so I don't have to un-obfuscate all that code to get what you are dealing with?
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post by kingconnections »

As far as the results of the array for $status if I do a print_r($status) it displays one check mark- because it is a img tag.

http://ecmsahq01/PatchManagement/images/check.gif <-------- This link goes no where as it is local to my work. but it is the result of print_r($status)

That is the results of the $status array.

edit'd to take out un
Last edited by kingconnections on Tue Feb 21, 2006 9:56 am, edited 3 times in total.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Ahh, that clears it up a bit :wink:


you want an aggregate function
http://dev.mysql.com/doc/refman/5.0/en/ ... tions.html
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post by kingconnections »

OK say i have this info: How do i access the [status] and [install] keys

print_r of my array below:

Array ( [CLDBABRF02] => Array ( [MS06-001] => Array ( [status] =>Patched [install] => Jan 17 2006 10:52AM )


This is how i am attempting to do it now. the patches show up correctly, but the status and install just show up as text. If someone can help me do this it will solve all my problems. Thanks Dan


Code: Select all

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

foreach( $current_patchs as $patchs )
{ 
  echo "<td>$cols[$server][$patchs][status][install]</td>";

}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
Array ( 
    [CLDBABRF02] => Array ( 
        [MS06-001] => Array ( 
            [status] => Patched, 
            [install] => Jan 17 2006 10:52AM )
        )
    )
?>
So you have a three dimensional array and you want information from the values in the second dimension of the array (which happens to be an array itself). I think you are going to need one more foreach loop.

Code: Select all

<?php
$server_array = Array ( 
    [CLDBABRF02] => Array ( 
        [MS06-001] => Array ( 
            [status] => Patched, 
            [install] => Jan 17 2006 10:52AM 
        )
    )
);

foreach ($server_array as $server_key => $server_val)
{
    foreach ($server_val as $server_name => $server_data)
    {
        foreach ($server_data as $key => $value)
        {
            echo "The 3rd Dimension key is $key and the value is $value...<br />\n";
        } // 3rd dimension
    } // 2nd dimension
} // 1st dimension

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

Post by kingconnections »

OK, All I got to say is that you guys ROCK!!!!!!!!!!!!!!!!!!!!! WoOOOOOOOOOOOOTTTTTTTTTTTTTTTTTTTT Thank you! Thank you!


Can you give +'s on this board? I would give you all one if I can.


Thanks

Dan
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

So I take it this thing worked out for you, huh? Kudos, glad this community could help you through it.
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post by kingconnections »

OK - so that worked great for me, I was able to apply it to many pages. I only have 1 minor issue, with the logic more then the code.


ok so say i have a table that has 3 columns ie:
This will produce the following output. Which is great for sitea but not for siteb, siteB in reality has patch 2, and 3 but no record or value for patch 1. I need to somehow distiguish that there is no value for patch1 and place a 0 there. You know what I mean?

site |Patch1|patch2|patch3
siteA --- x ------ x------- x
siteB ----x ------ x <---- wrong entry
SiteB -------------x--------x <----Correct entry


Or is that something I could do when I input the data into the array? Or is there some kind of array compary method that would allow me to insert the missing patch and value?


Code: Select all

foreach ($server_array as $server_key => $server_val) 
{ 
		echo "<tr><td>$server_key</td>";
    foreach ($server_val as $server_name => $server_data) 
    { 
        foreach ($server_data as $key => $value) 
        { 
           If ($key=='status')
           {
            echo "<td>$value</td> "; 
            }
            
        } // 3rd dimension 
       
    } // 2nd dimension 
    echo" </tr>";
} // 1st dimension
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

At some point in the loop iteration you will need to check against the value held in that array index...

Code: Select all

<?php
foreach ($server_val as $server_name => $server_data) 
{ 
    foreach ($server_data as $key => $value) 
    { 
        If ($key=='status')
        {
            if ($value == 0)
            {
                $new_value = 'This is empty';
            }
            else
            {
                $new_value = $value;
            }
            echo "<td>$new_value</td> "; 
        }
    } 
}
?>
Something along these lines. You will of course have to tailor this to your own needs, but it will be something like this.
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post by kingconnections »

Can compare a value to an entire array?


Here is what it I have now: The problem is that there is 3 patches in $current_ patches array and it does it 3 times in a row?

I guess instead of looping the $current patches array I have to loop the variable that i want to compare it too?

Code: Select all

foreach ($site_array as $site_key => $site_val) 
{

foreach ($site_val as $site_name => $site_data)  
{  
    foreach ($site_data as $key => $value)  
    {  
    	foreach ($current_patchs as $item) [b]//This section loops through array of all the patches for comparison
    	{[/b]        
       			   
             echo "$site_key--$value---$item<br><br>";
         //   echo "<td>$value</td> ";  
        } 
    }  
} 
}

What if the outside loop is the $current patches and the inside loops are the normal loops, but not printing anything at this point just evaluating the loop 1 time per number of items in the $current_patchs array, if it finds a patch with no value then u set it? Does that make sense?

LOL and yes I know I spelled patches incorrectly! LOL
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Is there a reason in particular that you are looping patches at that point in the other loop? You can compare just about anything to anything regardless of where you are, so yeah, you can compare whatever you want where ever you want to compare it.

In your example, is there a use for the placement of your inner most loop? If it is for comparison, try comparing it to something and see what comes out of it.
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post by kingconnections »

Yes it was for comparison- I had the output and it was what I wanted times 3.


Basically i have $current_patchs array which is a 1 dimension array that contains all the current patches.
so for this example it has 3.
That is why I got 3 times the output. Ok let me rework this code and see what I get when I put the $current_patchs array around the outside.

Thanks

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

Post by kingconnections »

Is there anyway i can do a "If does not exist"? The problem is that there is no record in the array at all for the item I want to add.

So basically in my loop right now i am getting


Reference -------- Actual
Patch----------- Array data-------Value

MS06-001------MS06-007-------- 1----
MS06-001------MS06-008-------- 1----
MS06-001------MS06-001-------- 1----
MS06-001------MS06-007-------- 2----



so you can see here that the 1st entry in "actual array data" does not have a ms06-001 value

but i can't do a if patch <> patch cause it would just change all the data all the way down?


Anyone have any ideas. Isn't there some array functions that could do this better?

ok so here is the results of my print_r for that data above:

Code: Select all

array ( 'ABRF' => array ( 'MS06-007' => array ( 0 => 1, ), 'MS06-008' => array ( 0 => 1, ), )

array('ABRF'=>array([b]'MS06-001'=>array(0=>0,),[/b]'MS06-007' => array ( 0 => 1, ), 'MS06-008' => array ( 0 => 1, ), )
The value in bold is missing and that is what I am trying to add, but it will have to be dynamic, It would need to be what ever patch in the Reference patch array(could be 1, could be 10) that are missing and assign them a value of 0.

so basically it will report for the 1st record that.
But when those items are added it would print fine in the output loop:


Vunerable Servers

MS06-001----MS06-007--------MS06-008
---- 0---------------1-------------------1------
Post Reply