|
 |
|
ถามวิธีสร้าง Rss จาก MySQL Database และวิธีดึง Rss ไปแสดงที่หน้าเว็บอื่น ๆ หน่อยครับ |
|
 |
|
|
 |
 |
|
ตัวอย่างข้อมูลใน DB

ไฟล์การสร้างเอกสาร XML จากฐานข้อมูล MySQL
<?
function writeXML($xml)
{
$fp = fopen('student.xml', 'w+'); //save file
fwrite($fp,$xml);
fclose($fp);
}
function connect()
{
global $conn;
$hostname = "localhost";
$username = "root";
$password = "1234";
$dbname = "student";
$conn = mysql_connect( $hostname, $username, $password );
if ( ! $conn ) die ( "ไม่สามารถติดต่อกับ MySQL ได้" );
mysql_select_db ( $dbname, $conn )or die ( "ไม่สามารถเลือกฐานข้อมูลได้" );
$charset = "SET NAMES 'tis620'";
mysql_query($charset);
}
connect();
$query = "SELECT * FROM student";
$rs = mysql_query($query,$conn);
$doc = new DOMDocument('1.0', 'UTF-8');
$root = $doc->createElement('root'); //สร้าง RootNode <root>
$root = $doc->appendChild($root);
while($row = mysql_fetch_assoc($rs))
{
$std = $doc->createElement('student'); //สร้าง Node <student>
$std = $root->appendChild($std);
foreach($row as $fieldname=>$fieldvalue)
{
$child = $doc->createElement($fieldname,$fieldvalue); //สร้าง node โดยใช้ชื่อ node ตามชื่อ field และ ข้อมูลแต่ละแถว
$child = $std->appendChild($child);
}
}
header( "content-type: application/xml; charset=utf-8" );
echo $text = $doc->saveXML();
writeXML($text);
?>
ตัวอย่างเอกสาร XML ที่สร้างขึ้นมาจาก Source ด้านบนครับ

ไฟล์เปิดอ่านเอกสาร XML ที่สร้างขึ้นมาครับ
<?php
$xml = new DOMDocument('1.0','utf-8');
$xml->load( 'student.xml' );
$root = $xml->getElementsByTagName( "root" );
echo "<b>Show Student Data<b><p>";
echo "<table border='1' cellspacing='0'>";
echo "<tr><td>ID</td><td>Name</td><td>Lastname</td><td>Picture</td><td>My Site</td></tr><tr>";
foreach( $root as $child )
{
$std = $child->getElementsByTagName( "student" );
foreach($std as $sid)
{
//Stand at <sid>...</sid>
$id = $sid->getElementsByTagName( "sid" );
foreach($id as $nodeid)
{
echo "<td>".$nodeid->nodeValue."</td>";
}
//Stand at <name>...</name>
$name = $sid->getElementsByTagName( "name" );
foreach($name as $nodename)
{
echo "<td>".$nodename->nodeValue."</td>";
}
//Stand at <lname>...</lname>
$lname = $sid->getElementsByTagName( "lname" );
foreach($lname as $nodelname)
{
echo "<td>".$nodelname->nodeValue."</td>";
}
//Stand at <pic>...</pic>
$pic = $sid->getElementsByTagName( "pic" );
foreach($pic as $nodepic)
{
echo "<td><img src='".$nodepic->nodeValue."' width='100' height='100'> </td>";
}
$link = $sid->getElementsByTagName( "link" );
foreach($link as $nodelink)
{
echo "<td><a href='".$nodelink->nodeValue."'>".$nodelink->nodeValue."</a></td></tr>";
}
}
}
echo "</table>"
?>
Code นี้เขียนบน Extension ของ DOM/API ใน php5 นะครับ
|
 |
 |
 |
 |
Date :
2009-10-29 17:39:14 |
By :
extenser |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อืมโดนๆ +1
|
 |
 |
 |
 |
Date :
2009-10-29 19:17:30 |
By :
peterxp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณค้าบบ จะลองดูนะครับ
|
 |
 |
 |
 |
Date :
2009-10-30 00:24:28 |
By :
tarorio |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แนะนำเว็บนี้เลยครับ ให้บริการฟรี และง่ายสุดๆ
ดึงข่าวจากเว็บต่างๆ มาแปะติดที่หน้าเว็บของคุณอย่างง่ายดาย สามารถติดข่าวไว้ที่ Fan page ของคุณได้ด้วย
http://xml-rss.com
http://xml-rss.com
ตัวอย่าง การดึงข่าวมาติดที่ Fan page
http://www.facebook.com/pages/RSS/267729689954779?sk=app_156218351098324
http://www.facebook.com/pages/RSS/267729689954779?sk=app_208195102528120
|
 |
 |
 |
 |
Date :
2012-01-07 11:05:31 |
By :
Easy RSS |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|