2D Array problem.

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

Post Reply
dwnthk
Forum Newbie
Posts: 13
Joined: Mon Oct 08, 2007 5:55 am

2D Array problem.

Post by dwnthk »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi guys,

I got something like this:

Code: Select all

...
$q1 = "select ... ";
$r1 = $conn->dbExecute($q1);

   $cnt = 0;
   while ($rs1=mssql_fetch_row($r1))   {
        $arr2 = array($cnt => array($rs1[0],$rs1[1],$rs1[2],$rs1[3],$rs1[4],$rs1[5],$rs1[6],$rs1[7],$rs1[8],$rs1[9], ));
        $cnt++;
      }
      
   $cnt1 = 0;   
   while (count($arr2) > $cnt1) {     // foreach {... }  or what ever
      ...
   }


All I got in the $arr2 is the last record of the $q1. Is there something I misunderstood the use of array in the php? What I want is to get the every record in the array and loop thr' the array...

Why there is only one records in the array?

Thanks.


Dave


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: 2D Array problem.

Post by califdon »

dwnthk wrote:

Code: Select all

...
   while ($rs1=mssql_fetch_row($r1))   {
        $arr2 = array($cnt => array($rs1[0],$rs1[1],$rs1[2],$rs1[3],$rs1[4],$rs1[5],$rs1[6],$rs1[7],$rs1[8],$rs1[9], ));
        $cnt++;
      }
All I got in the $arr2 is the last record of the $q1. Is there something I misunderstood the use of array in the php? What I want is to get the every record in the array and loop thr' the array...

Why there is only one records in the array?
Hi Dave,
Do you see that, in your while loop, every time you are completely redefining $arr2? Try something like $arr2[] = ....
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Why go threw all this hassle, when there already is a built in function that does this.

mssql_fetch_array
dwnthk
Forum Newbie
Posts: 13
Joined: Mon Oct 08, 2007 5:55 am

Post by dwnthk »

Got it. :D
Thanks all.

Dave
Post Reply