简体   繁体   中英

Assigning Values to a 2d array

I am trying to assign values from a db to a 2d array, but its only showing the last iterms.

Here is the code:

while($row = mysql_fetch_array($results)){
$MyData = array( array("Focus Area", $row["FocusArea"]),
               array("Finding Title", $row["FindingTitle"]),
               array("Finding Detail", $row["FindindDetail"]) 
             ); 

}//End While

What am I doing wrong please help.

$MyData[] = $row;

would be enough

I'd also suggest to make a function, as getting an array from db is a very common routine.
So, you'll be able to get your data in one line,

$myData = getRows("SELECT * FROM table");

You're declaring a new array each time the loop runs. Declare it out of the while loop, and add the new values.

$MyData = array();
while($row = mysql_fetch_array($results)){
$MyData[] = array( array("Focus Area", $row["FocusArea"]),
               array("Finding Title", $row["FindingTitle"]),
               array("Finding Detail", $row["FindindDetail"]) 
             ); 

}//End While
$myData = array();
while($row = mysql_fetch_array($results)){
$MyData[] = array( array("Focus Area", $row["FocusArea"]),
               array("Finding Title", $row["FindingTitle"]),
               array("Finding Detail", $row["FindindDetail"]) 
             ); 

}//End While

this will do the trick

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM