loading data into 2 dimensional array from sql

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 »

OK here is my code:

Code: Select all

$patches = array();



// FETCH CURRENT YEAR PATCHES

while($row = mssql_fetch_array($result)) 

{ 

//echo "<td>" . $row["patch"] . "</td>"; 

$mspatch = $row["patch"];



$patches[] = $mspatch;

}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$patches = array();



// FETCH CURRENT YEAR PATCHES

while($row = mssql_fetch_array($result)) 
{ 

//echo "<td>" . $row["patch"] . "</td>"; 
$patches[] = array($row['patch']);

}
What's the query being run?
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post by kingconnections »

here is the query and code...
edited cause wrong code was inserted.

Code: Select all

$query2 = "SELECT patch, COUNT(server) AS count FROM PatchStatus WHERE site = '$site'	AND status <> 'Patched' AND status <> 'Unsupported OS' and patch like '$patch' GROUP BY patch ORDER BY patch asc ";
	
$result2 = mssql_query($query2); 
$numRows = mssql_num_rows($result2); 

//$cols2 = array('MS06-001'=>array('1',),'MS06-007'=>array('8',),'MS06-008'=>array('50',));

$cols = array('patch_col'=>array('count_col'),);

while($row = mssql_fetch_array($result2)) 
{
    
    //echo "<td>".$row["patch"]."</td><td>" . $row["count"] . "</td>"; 
   
    $cols[$row["patch"]] = array($row["count"]);  
  
    
}
Last edited by kingconnections on Mon Feb 20, 2006 12:34 am, edited 1 time in total.
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post by kingconnections »

Even when I had this in there to load the array it didn't work.

Code: Select all

$cols[$row["patch"]] = array($row["count"]);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you var_export($cols) afterward, what shows?
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post by kingconnections »

Seems like it is working now, I just forgot a ;. But I have another issue that I didn't realize until now, and that is that I need to make it a 3d array instead of a 2d array.

Is this right?

Code: Select all

foreach ($sitecodes as $site) {
//echo "<tr><td>$site";
$query2 = "SELECT  site, patch, COUNT(server) AS count FROM PatchStatus WHERE site = '$site' AND status <> 'Patched' AND status <> 'Unsupported OS' and patch like '$patch' GROUP BY patch ORDER BY patch asc ";
	
$result2 = mssql_query($query2); 
$numRows = mssql_num_rows($result2); 

//$cols2 = array('MS06-001'=>array('1',),'MS06-007'=>array('8',),'MS06-008'=>array('50',));

//$cols = array('patch_col'=>array('count_col'),);

	while($row = mssql_fetch_array($result2)) 
		{
    
    //echo "<td>".$row["patch"]."</td><td>" . $row["count"] . "</td>"; 
   
    		[b]$cols[$row["site"]] = array($row["patch"])=>array($row["count"]);  [/b]
      } 

echo "</tr>";
var_export($cols);
there is something wrong with the bold line, I get a
"Parse error: syntax error, unexpected T_DOUBLE_ARROW in d:\wwwroot\Patches\scripts\test.php on line 71"

Thanks a ton for your help guys. I really really am grateful.

Thanks

Dan
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

replace that line with the following

Code: Select all

// make sure the main index exists
if (!isset($cols[$row['site']]))
{
  $cols[$row['site']] = array();
}
$cols[$row['site']][$row['patch']] = array($row['count']);
[edit] fixed a little bug
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post by kingconnections »

Great!...... Thanks a ton... Next question



I am trying to make a table like so

site|patch1|patch2|patch3| and so on
loc1| 5 | 10 | 15 |
loc2| 2 | 11 |



the problem is that not every site has 3 patches that return in the sql query, infact there is no data in there for that record at all because it is not vulerable.

So how can i check to the 3d array and ensure that every site has every patch, and if the patch isn't there put it there and give the count a 0 value.

Unless you know a better way to display a blank.

I also need to know how I display the foreach loop to make each record go sideways like that.

here is what I have currently:

Code: Select all

//////////// output table 
//echo '<p>There are '.sizeof($cols)." Patches.</p>\n";


?>
/////////// list patches going sideways ---- this section works fine.
echo "<tr><th>Vulerable Patches</th></tr><tr><td>Site</td>";


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

/// list sites going across in site patch1(count), patch2(count)  -- this section does not work


foreach ( $cols as $site)
{
echo"<tr><td>$site</td></tr>";
}


</tr>
</table>
</body>
</html>
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post by kingconnections »

OK, I am getting close!

here is my updated code:

the problem is it displays in the correct format, just says Array in all the values
where the count should be, and also it still does not take into account for those
records that do not exisit.

Code: Select all

/////////// list patches going sideways
echo "<tr><th>Vulerable Patches</th></tr><tr><td>Site</td>";


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

///////////// list site and patch count going sideways......

foreach ( $cols as $site=>$patch)
{
echo"<tr><td>$site</td>";

foreach ( $cols as $patch=>$count)
{ 
}
foreach ($cols as $patch=>$count)
{
 echo "<td>   ".$count." </td>";
} 
echo "</tr>";


}

?>

</tr>
</table>
</body>
</html>
Post Reply