0

this my MySQL to generate the array :

$sql_name = "SELECT * FROM ".TABLE_PREFIX."members WHERE member_id IN ($member_id) order by member_id DESC";

$result_name = mysql_query($sql_name, $db);

$name = array();
while ($row = mysql_fetch_assoc($result_name)) {
    $arr1= $row['first_name'];
    $arr = array_push($name ,$arr1);
}

$name1[] = $name;
$objPHPExcel->setActiveSheetIndex()->fromArray($name1, NULL, 'C20');

This is the result that i get :

enter image description here

Output that i want is each array will fill 2 rows means it will take next column and merge it like this below image. How can possible to use merge?.. i new to this. please help me. thanks!

enter image description here

SOLUTION FOR MY QUESTION FOR REFERENCE LATER :

for($row = 20; $row <= 2; $row++) { 

$objPHPExcel->setActiveSheetIndex()->fromArray($name1, NULL, 'C20')->mergeCells('C' . $row . ':D' . $row);
}

and i change my sql .. by adding empty space to the array :

  $arr = array_push($name ,$arr1,'');
2
  • How is the data structured in your database? What columns from the database table do you need merging? PHPExcel won't do this for you, so you'll need to do it in your database query, or in your array before putting it in the PHPExcel object Commented Feb 12, 2014 at 8:01
  • like in the 1st picture the output i received is C and D colums ... what i need if the array is 2 output .. first array will merge with next column .. C+D, E+F ...like my desire output 2nd picture.. Commented Feb 12, 2014 at 8:04

1 Answer 1

2

To merge cells in PHPExcel, you can use

$objPHPExcel->getActiveSheet()
    ->mergeCells('C29:D29');

as described in section 4.6.34 of the developer documentation, entitled "Merge/unmerge cells", but it's up to you to ensure that the correct data is written and formatted in those cells

EDIT

The range argument is simply a string, so you can use standard PHP concatenation to build it dynamically in a loop.

for($row = 1; $row <= 100; $row++) {
    $objPHPExcel->getActiveSheet()
        ->mergeCells('C' . $row . ':D' . $row);
}
Sign up to request clarification or add additional context in comments.

4 Comments

yes i have tried this but.. how to make ("C29:D29") <-- loop dynamicly since the data is from array.. i meas like i++ in for
this is my code to get the C,D column .. based on your edit how should i do.. sorry im kinda new... $objPHPExcel->setActiveSheetIndex()->fromArray($name1, NULL, 'C20'); ... where should i put the code basicly?
I'm really having great difficulty understanding the problems you're having, mainly because I don't understand what you're doing with the mass of arrays you're working with in your code to post a single value in a cell; and where you're manipulating data in your loop
ok let say from the mysql i got two array ... the output is in C,D column ... what i want is each array will merge with and empty column next to them starting from C and become like this C+D , E+F ..the new output is : the first array now in C column and second array in E column ... D and F is empty row that they merge

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.