loading data into 2 dimensional array from sql
Moderator: General Moderators
-
kingconnections
- Forum Contributor
- Posts: 137
- Joined: Thu Jul 14, 2005 4:28 pm
loading data into 2 dimensional array from sql
OK guys I am trying to load data into a 2d array from a sql statement during a sql fetch statement.
This is how i am loading a 1d array from sql.
$sitecodes = array();
while($row = mssql_fetch_array($result1))
{
$site_codes = $row["machine_group_name"];
$sitecodes[] = $site_codes;
}
I am not sure how i am going to load the 2d array in the last line?
Thanks
Dan
This is how i am loading a 1d array from sql.
$sitecodes = array();
while($row = mssql_fetch_array($result1))
{
$site_codes = $row["machine_group_name"];
$sitecodes[] = $site_codes;
}
I am not sure how i am going to load the 2d array in the last line?
Thanks
Dan
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
$sitecodes[] = $row;-
kingconnections
- Forum Contributor
- Posts: 137
- Joined: Thu Jul 14, 2005 4:28 pm
feyd | Please use
feyd | Please use
Code: Select all
andCode: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
I know that is how i load the 1d array, i am speaking about the following:
Say u want to load $cols from the msql fetch statement above but for this code:Code: Select all
$cols = array(
'MS06-001'=>array(
'1',
),
'MS06-007'=>array(
'8',
),
'MS06-008'=>array(
'50',
)
);
echo '<p>There are '.sizeof($cols)." Patches.</p>\n";
/////////// list patches going sideways
echo "<table border ='1'<tr><th>Vulerable Patches</th>";
foreach( $cols as $patch=>$count )
{
//echo ' '.$patch."\n";
echo " ";
echo " ";
foreach( $count as $count )
{
echo "<td> ".$patch."</td> ";
}
echo " ";
}
echo "</tr><br><tr>";
//////////List count going sideways
echo"<td>site</td>";
foreach( $cols as $patch=>$count )
{
foreach( $count as $count )
{
echo "<td> ".$count." </td>";
}
echo " ";
}
echo "</tr> </table>";feyd | Please use
Code: Select all
andCode: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
The snip I posted will always create a two-dimensional array given the code you are running it in. Whether your query returns one column, or a thousand, it'll work the same.
If you want to name each element of the array it'd be
If you want to name each element of the array it'd be
Code: Select all
$sitecodes[$row['namefield']] = $row;-
kingconnections
- Forum Contributor
- Posts: 137
- Joined: Thu Jul 14, 2005 4:28 pm
guess i am dumb.
feyd | Please use
feyd | Please use
Code: Select all
andCode: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Guess I am dumb. I am not sure how the data is getting loaded into the right element of the array. Can I use this specific example:Code: Select all
// this is what the static code looks like and type of data I want to load in the fetch
//$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))
{
$patch_col = $row["patch"] ;
$count_col = $row["count"] ;
$cols[$row['patch']] = $row;
echo "<b>$patch_col</b><br>";
echo "$count_col";
}feyd | Please use
Code: Select all
andCode: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]-
kingconnections
- Forum Contributor
- Posts: 137
- Joined: Thu Jul 14, 2005 4:28 pm
is this close?
guess not cause I am now getting this:
Notice: Undefined index: patch_col in d:\wwwroot\Patches\scripts\test.php on line 89
Thanks a ton,
dan
Code: Select all
//$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>";
$patch_col = $row["patch"] ;
$count_col = $row["count"] ;
$cols[$row['patch_col']] = $patch_col;
$cols[$row['count_col']] = $count_col;
echo "<b>$patch_col</b><br>";
echo "$count_col";
}guess not cause I am now getting this:
Notice: Undefined index: patch_col in d:\wwwroot\Patches\scripts\test.php on line 89
Thanks a ton,
dan
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
$cols[$row['patch'] = array($row['count']);-
kingconnections
- Forum Contributor
- Posts: 137
- Joined: Thu Jul 14, 2005 4:28 pm
LOL guess i am still not getting it. Now I am getting a parse error.
Thanks again dan
Code: Select all
$cols = array('patch_col'=>array('count_col'),); // is this line needed to define the array???
while($row = mssql_fetch_array($result2))
{
//echo "<td>".$row["patch"]."</td><td>" . $row["count"] . "</td>";
$cols[$row["patch"] = array($row["count"]);
}-
kingconnections
- Forum Contributor
- Posts: 137
- Joined: Thu Jul 14, 2005 4:28 pm
I think i am missing a [ from that line?
Code: Select all
$cols[$row["patch"] = array($row["count"]);- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Yeah, you need to close the $cols array var...kingconnections wrote:I think i am missing a [ from that line?
Code: Select all
<?php $cols[$row["patch"] = array($row["count"]); ?>
Code: Select all
<?php
$cols[$row["patch"]] = array($row["count"]);
?>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
-
kingconnections
- Forum Contributor
- Posts: 137
- Joined: Thu Jul 14, 2005 4:28 pm
Ok, so when I added the other ] it still didn't work? Any ideas now? It gives me a web server time out page. And when i run the query in sql analyzer it takes about 1 sec to finish.
I still don't think I have the idea of how this works down properly, anyone that could help would be much appreciated.
I still don't think I have the idea of how this works down properly, anyone that could help would be much appreciated.