TShopping

 找回密碼
 註冊
搜索
查看: 2526|回復: 2
打印 上一主題 下一主題

[教學] RSS + PHP + MYSQL 程式語法

  [複製鏈接]
跳轉到指定樓層
1#
發表於 2010-7-2 16:05:47 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook
先建兩個資料表
資料表名為 webref_rss_details
  1. CREATE TABLE 'webref_rss_details' (
  2.   'id' int(11) NOT NULL auto_increment,
  3.   'title' text NOT NULL,
  4.   'description' mediumtext NOT NULL,
  5.   'link' text,
  6.   'language' text,
  7.   'image_title' text,
  8.   'image_url' text,
  9.   'image_link' text,
  10.   'image_width' text,
  11.   'image_height' text,
  12.   PRIMARY KEY  ('id')
  13. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
複製代碼
webref_rss_items
  1. CREATE TABLE 'webref_rss_items' (
  2.   'id' int(11) NOT NULL auto_increment,
  3.   'title' text NOT NULL,
  4.   'description' mediumtext NOT NULL,
  5.   'link' text,
  6.   PRIMARY KEY  ('id')
  7. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
複製代碼
資料表 webref_rss_details 包含10個欄位,有id, title, description, link, language, image_title, image_url, image_link, image_width and image_height

第二個資料表 webref_rss_items 包含四個欄位 id, title, description, link


建立有效的PHP RSS 2.0 feed 標頭,並使用RSS()物件
  1. <?
  2. header("Content-Type: application/xml; charset=ISO-8859-1");
  3. include("classes/RSS.class.php");
  4. $rss = new RSS();
  5. echo $rss->GetFeed();
  6. ?>
複製代碼
建立一個 mysql_connect.php 檔案連接資料庫,裡面定義著所有連接資料庫的資訊欄位
  1. <?
  2. DEFINE ('DB_USER', 'your_username');
  3. DEFINE ('DB_PASSWORD', 'your_password');
  4. DEFINE ('DB_HOST', 'localhost');
  5. DEFINE ('DB_NAME', 'your_databasename');

  6. // Make the connnection and then select the database.
  7. $dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );
  8. mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );
  9. ?>
複製代碼
要建立RSS之前,必須要先連接資料庫,先連接 mysql_connect.php
在建立 RSS class 裡的 GetFeed 函式

第一個方法先 called getDetails 函式
然後連接 webref_rss_details 資料表,和取出裡面所有的值,用 while 迴圈方法取出

第二個方法在私有函式裡,GetFeed called getItems,這方法會選取 webref_rss_items 資料表裡所有的值,然後產生RSS規範的xml,然後寫入檔案

RSS.class.php檔案程式如下
  1. <?
  2. class RSS {
  3.   public function RSS() {
  4.     require_once ('pathto.../mysql_connect.php');
  5.   }

  6.   public function GetFeed() {
  7.     return $this->getDetails() . $this->getItems();
  8.   }

  9.   private function dbConnect() {
  10.     DEFINE ('LINK', mysql_connect (DB_HOST, DB_USER, DB_PASSWORD));
  11.   }

  12.   private function getDetails() {
  13.     $detailsTable = "webref_rss_details";
  14.     $this->dbConnect($detailsTable);
  15.     $query = "SELECT * FROM ". $detailsTable;
  16.     $result = mysql_db_query (DB_NAME, $query, LINK);

  17.     while($row = mysql_fetch_array($result)) {
  18.       $details = '<?xml version="1.0" encoding="ISO-8859-1" ?>
  19.       <rss version="2.0">
  20.       <channel>
  21.       <title>'. $row['title'] .'</title>
  22.       <link>'. $row['link'] .'</link>
  23.       <description>'. $row['description'] .'</description>
  24.       <language>'. $row['language'] .'</language>
  25.       <image>
  26.       <title>'. $row['image_title'] .'</title>
  27.       <url>'. $row['image_url'] .'</url>
  28.       <link>'. $row['image_link'] .'</link>
  29.       <width>'. $row['image_width'] .'</width>
  30.       <height>'. $row['image_height'] .'</height>
  31.       </image>';
  32.     }
  33.     return $details;
  34.   }
  35.   private function getItems() {
  36.     $itemsTable = "webref_rss_items";
  37.     $this->dbConnect($itemsTable);
  38.     $query = "SELECT * FROM ". $itemsTable;
  39.     $result = mysql_db_query (DB_NAME, $query, LINK);
  40.     $items = '';
  41.     while($row = mysql_fetch_array($result)) {
  42.       $items .= '<item>
  43.       <title>'. $row["title"] .'</title>
  44.       <link>'. $row["link"] .'</link>
  45.       <description><![CDATA['. $row["description"] .']]></description>
  46.       </item>';
  47.     }
  48.     $items .= '</channel>
  49.     </rss>';
  50.     return $items;
  51.   }
  52. }
  53. ?>
複製代碼
參考原文 http://www.webreference.com/authoring/languages/xml/rss/custom_feeds/

 

臉書網友討論
2#
發表於 2010-7-2 17:38:25 | 只看該作者
讚啦~~哈哈

版主招募中

3#
發表於 2011-10-9 18:10:54 | 只看該作者
看不懂~~~~:shutup:


*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-5-2 08:55 , Processed in 0.064394 second(s), 19 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表