|
 |
|
ถ้าหากเราต้องการดึงรายชื่อจากฐานข้อมูลมาแสดง ใน template word ควรทำอย่างไรให้ข้อมูลแสดงออกมาหมดค่ะ |
|
 |
|
|
 |
 |
|
Code (PHP)
<?
$objConnect = mysql_connect("localhost","root","1234") or die("Error Connect to Database");
$objDB = mysql_select_db("db_cw");
mysql_query("SET NAMES UTF8");
$strSQL = "SELECT * FROM detail_sp";
$query_Recordset1 = "SELECT * FROM detail_sp ORDER BY id_sp ASC";
$objQuery = mysql_query($strSQL);
?>
<?
require_once '../PHPWord.php';
$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate('Solarsystem1.docx');
while($objResult = mysql_fetch_array($objQuery))
{
//$table->addRow();
$document->setValue('Value1', $objResult['name1_sp']);
$document->setValue('Value1', $objResult['name1_sp']);
$document->setValue('Value2', $objResult['schoolname_sp']);
$document->setValue('Value3', $objResult['name2_sp']);
//$document->setValue('Value4', $objResult["school_name"]);
}
$document->setValue('weekday', date('l'));
$document->setValue('time', date('H:i'));
$document->save('Solarsystem.docx');
//$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
//$objWriter->save('CreateWord2.docx');
?>
<?
mysql_close($objConnect);
?>
จากโค้ดด้านบน ข้อมูลแสดงออกมาแค่ ข้อมูลแรกแถวเดียว จะเขียนยังไงให้ข้อมูลทั้งหมดใน MySQL แสดงใน template ที่เราสร้างไว้ได้ค่ะ
Tag : PHP

|
|
 |
 |
 |
 |
Date :
2013-01-09 01:07:59 |
By :
ลูกอม |
View :
1210 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<?
$objConnect = mysql_connect("localhost","root","1234") or die("Error Connect to Database");
$objDB = mysql_select_db("db_cw");
mysql_query("SET NAMES UTF8");
$strSQL = "SELECT * FROM detail_sp";
$query_Recordset1 = "SELECT * FROM detail_sp ORDER BY id_sp ASC";
$objQuery = mysql_query($strSQL);
?>
<?
require_once '../PHPWord.php';
$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate('Solarsystem1.docx');
$num = mysql_num_rows($objQuery); # เช็คจำนวนแถวว่ามีกี่แถว
if($num>0){
while($objResult = mysql_fetch_array($objQuery))
{
//$table->addRow();
$document->setValue('Value1', $objResult['name1_sp']);
$document->setValue('Value1', $objResult['name1_sp']);
$document->setValue('Value2', $objResult['schoolname_sp']);
$document->setValue('Value3', $objResult['name2_sp']);
//$document->setValue('Value4', $objResult["school_name"]);
}
}
$document->setValue('weekday', date('l'));
$document->setValue('time', date('H:i'));
$document->save('Solarsystem.docx');
//$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
//$objWriter->save('CreateWord2.docx');
?>
<?
mysql_close($objConnect);
?>
ลองดูนะครับ เพิ่มอันนี้นะคับ
$num = mysql_num_rows($objQuery); # เช็คจำนวนแถวว่ามีกี่แถว
if($num>0){
|
ประวัติการแก้ไข 2013-01-09 02:32:50
 |
 |
 |
 |
Date :
2013-01-09 02:31:54 |
By :
JindaCpe8 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เพิ่มโค้ด
$num = mysql_num_rows($objQuery); # เช็คจำนวนแถวว่ามีกี่แถว
if($num>0){
เข้าไปแล้วก็ยังออกมาแค่แถวแรกแถวเดียวเหมือนเดิมค่ะ ต้องเพิ่มตรงใหนอีกไหมค่ะ
|
 |
 |
 |
 |
Date :
2013-01-09 08:52:11 |
By :
ลูกอม |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|