Forum


Replies: 1   Views: 3575
How to create table using mysql and php - solved
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by mightycpa  · 19-04-2013 - 18:17

Hi,



I wanted to create a table using a query on my php page.



I created a query that returns several rows of 3 columns.  I had trouble figuring out a way to format the results of the query so that they fit your syntax.  I understand your syntax to basically describe a table as an array that contains one array for each row to be included in the table:



$wordtable=array(



array('row1col1','row1col2','row1col3'),



array('row2col1','row2col2','row2col3'),



array('row3col1','row3col2','row3col3'),



);



Finally, I came up with this, and it works very well:



$sql = "SELECT A, B, C FROM WHEREVER";    

$result = mysql_query($sql) or die (mysql_error());

$i=0;

$rows_ra = array();

while ($row = mysql_fetch_assoc($result)) {

    foreach ($row as $key => $value) {

      $rows_ra[$i][$key] = $value;

    }

    $i++;

}



$paramsTable = array(

'border' => 'single',

'border_sz' => 10

);

$docx->addTable($rows_ra, $paramsTable);

$docx->createDocx('./roster_doc/hello_world_table');



This is a big breakthrough for me!  Thanks for the tool!