Jump to content

nesting of array


piyush23424

Recommended Posts

Hi,

I am working on creating excel sheet with php but i am getting difficulites in inserting dynamic array in the main array.

Main array starts from $sheet1.

The first array inside the main array are heading of the excel sheet.

and the array having values like tes1, test2 etc are the values of excel sheet.

If i use the static values then it works fine. but i want to make it dynamic with database

values(see the while loop).

Pls help me to insert the array created by while at the place of test values array.

 

Thanks

 

 

while($row = mysql_fetch_array($res))
   {	
   array("test","test1", "test2","test4", "test5", "test6", "test7","test8","test9");
   }
//print_r($string);
//$string = implode(",",$string);

$sheet1 = array(
  array('Customer\´s Name','Customer E-mail Address','Customer Address 1','Customer Address 2','Billing City Name', 'Customer\´s Billing State', 'Customer\´s Postal Code', 'Country', 'Customer´s Phone Number'),

array("test","test1", "test2","test4", "test5", "test6", "test7","test8","test9"),
array("test","test1", "test2","test4", "test5", "test6", "test7","test8","test9"),
array("test","test1", "test2","test4", "test5", "test6", "test7","test8","test9"),
array("test","test1", "test2","test4", "test5", "test6", "test7","test8","test9"),

);

Link to comment
https://forums.phpfreaks.com/topic/219855-nesting-of-array/
Share on other sites

Hi,

I am working on creating excel sheet with php but i am getting difficulites in inserting dynamic array in the main array.

Main array starts from $sheet1.

The first array inside the main array are heading of the excel sheet.

and the array having values like tes1, test2 etc are the values of excel sheet.

If i use the static values then it works fine. but i want to make it dynamic with database

values(see the while loop).

Pls help me to insert the array created by while at the place of test values array.

 

Thanks

 

 

while($row = mysql_fetch_array($res))
   {	
   array("test","test1", "test2","test4", "test5", "test6", "test7","test8","test9");
   }
//print_r($string);
//$string = implode(",",$string);

$sheet1 = array(
  array('Customer\´s Name','Customer E-mail Address','Customer Address 1','Customer Address 2','Billing City Name', 'Customer\´s Billing State', 'Customer\´s Postal Code', 'Country', 'Customer´s Phone Number'),

array("test","test1", "test2","test4", "test5", "test6", "test7","test8","test9"),
array("test","test1", "test2","test4", "test5", "test6", "test7","test8","test9"),
array("test","test1", "test2","test4", "test5", "test6", "test7","test8","test9"),
array("test","test1", "test2","test4", "test5", "test6", "test7","test8","test9"),

);

 

I want to replace the static array with dynamic array created by while loop.

Link to comment
https://forums.phpfreaks.com/topic/219855-nesting-of-array/#findComment-1139687
Share on other sites

I'm assuming you already know how to connect/disconnect and query a database.

 

If you want your array to appear under $sheet1, you'll have to loop after you create the first array.

 

Try something like

$sheetArray[0] = array('Customer\´s Name','Customer E-mail Address','Customer Address 1','Customer Address 2','Billing City Name', 'Customer\´s Billing State', 'Customer\´s Postal Code', 'Country', 'Customer´s Phone Number');
$count = 1;
while($row = mysql_fetch_array($res))
{
   $sheetArray[$count] = array($row['fieldname1'], $row['fieldname2'], ... );
  ++$count;
}

where fieldnameX are your fieldnames in your database.

Link to comment
https://forums.phpfreaks.com/topic/219855-nesting-of-array/#findComment-1139692
Share on other sites

I'm assuming you already know how to connect/disconnect and query a database.

 

If you want your array to appear under $sheet1, you'll have to loop after you create the first array.

 

Try something like

$sheetArray[0] = array('Customer\´s Name','Customer E-mail Address','Customer Address 1','Customer Address 2','Billing City Name', 'Customer\´s Billing State', 'Customer\´s Postal Code', 'Country', 'Customer´s Phone Number');
$count = 1;
while($row = mysql_fetch_array($res))
{
   $sheetArray[$count] = array($row['fieldname1'], $row['fieldname2'], ... );
  ++$count;
}

where fieldnameX are your fieldnames in your database.

 

Thanks BLaZuRE, your suggestion Worked for me :). i was working on this problem from last 3 hours and you just solved it in seconds....Thanks again...

Link to comment
https://forums.phpfreaks.com/topic/219855-nesting-of-array/#findComment-1139700
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.