Page 1 of 2
loading data into 2 dimensional array from sql
Posted: Fri Feb 17, 2006 3:53 pm
by kingconnections
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
Posted: Fri Feb 17, 2006 3:57 pm
by feyd
Posted: Fri Feb 17, 2006 4:01 pm
by kingconnections
feyd | Please use Code: 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
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Fri Feb 17, 2006 4:09 pm
by feyd
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
Code: Select all
$sitecodes[$row['namefield']] = $row;
guess i am dumb.
Posted: Fri Feb 17, 2006 4:22 pm
by kingconnections
feyd | Please use Code: 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
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Fri Feb 17, 2006 4:42 pm
by kingconnections
is this close?
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
Posted: Fri Feb 17, 2006 4:46 pm
by feyd
Code: Select all
$cols[$row['patch'] = array($row['count']);

Posted: Fri Feb 17, 2006 4:55 pm
by kingconnections
LOL guess i am still not getting it. Now I am getting a parse error.
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"]);
}
Thanks again dan
Posted: Fri Feb 17, 2006 5:08 pm
by kingconnections
I think i am missing a [ from that line?
Code: Select all
$cols[$row["patch"] = array($row["count"]);
Posted: Fri Feb 17, 2006 5:20 pm
by josh
you're missing a ] after patch"]
Posted: Fri Feb 17, 2006 5:22 pm
by RobertGonzalez
kingconnections wrote:I think i am missing a [ from that line?
Code: Select all
<?php
$cols[$row["patch"] = array($row["count"]);
?>
Yeah, you need to close the $cols array var...
Code: Select all
<?php
$cols[$row["patch"]] = array($row["count"]);
?>
Posted: Fri Feb 17, 2006 5:39 pm
by feyd
oops.
Posted: Fri Feb 17, 2006 6:02 pm
by RobertGonzalez
Feyd wrote:oops.
Lets see...
14726 total posts
[6.30% of total / 21.34 posts per day]
Inumerable amount of coders assisted by you...
A deftly thwarted hostile coup attempt...
An unusual ability to post fifteen replies within the same 3 second period of time...
I think a missing "]" is excusable.
Posted: Sun Feb 19, 2006 9:28 pm
by kingconnections
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.
Posted: Sun Feb 19, 2006 9:31 pm
by feyd
Sounds like an infinite loop. Post your updated code.