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

loading data into 2 dimensional array from sql

Post 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
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

$sitecodes[] = $row;
:?:
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post by kingconnections »

feyd | Please use

Code: Select all

and

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

and

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

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

guess i am dumb.

Post by kingconnections »

feyd | Please use

Code: Select all

and

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

and

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

Post 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
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

$cols[$row['patch'] = array($row['count']);
:?:
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

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

Post by kingconnections »

I think i am missing a [ from that line?

Code: Select all

$cols[$row["patch"] = array($row["count"]);
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

you're missing a ] after patch"]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

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

Post by feyd »

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

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

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

Post by feyd »

Sounds like an infinite loop. Post your updated code.
Post Reply