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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.