Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > PHP > PHP Forum > Codeigniter อ่าน RSS feed หลายๆไฟล์แล้วมันแสดงข้อมูลเดียวกันหลายๆครั้งครับ อ่านได้แค่ไฟล์เดียว



 

Codeigniter อ่าน RSS feed หลายๆไฟล์แล้วมันแสดงข้อมูลเดียวกันหลายๆครั้งครับ อ่านได้แค่ไฟล์เดียว

 



Topic : 068892



โพสกระทู้ ( 190 )
บทความ ( 0 )



สถานะออฟไลน์




ผมอ่าน RSS โดยใช้วิธีตามเว็บนี้ครับ http://codeigniter.com/wiki/RSSParser แต่ถ้าอ่านหลายๆไฟล์แล้วมันแสดงผลจาก RSS แค่อันเดียวครับ

นี้เป็นโค้ดใน Controller ครับ
Code
public function rss_news(){
//Load the shiny new rssparse
$this->load->library('RSSParser', array('url' => 'http://www.manager.co.th/RSS/Home/Breakingnews.xml', 'life' => 0));
//Get six items from the feed
$data = $this->rssparser->getFeed(10);

return $data;
}
public function rss_sport(){
//Load the shiny new rssparse
$this->load->library('RSSParser', array('url' => 'http://www.manager.co.th/rss/sport/sport.xml', 'life' => 0));
//Get six items from the feed
$data = $this->rssparser->getFeed(10);

return $data;
}

public function main_home()
{
$data['rss_news'] = $this->rss_news();
$data['rss_sport'] = $this->rss_sport();

$this->load->view('main_view',$data);
}


นี่เป็นโค้ดส่วน View ครับ
Code
foreach ($rss_news as $item1)
{

echo $item1['title']."<br/>";
}

foreach ($rss_sport as $item2)
{

echo $item2['title']."<br/>";
}


พอรันแล้วมันแสดงข้อมูลจาก $this->rss_news(); เพราะเรียกฟังก์ชั่นนี้ก่อน โดยแสดงข้อมูลที่เหมือนกัน 2 ครั้งครับ พอเรียกฟังก์ชั่น rss_sport มันไม่ได้เปลี่ยน url ที่ใส่ไปครับ ไม่ทราบว่าต้องแก้ยังไงครับ



Tag : PHP









ประวัติการแก้ไข
2011-11-09 15:51:59
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2011-11-09 15:50:15 By : mmc01 View : 1410 Reply : 2
 

 

No. 1



โพสกระทู้ ( 4,706 )
บทความ ( 8 )



สถานะออฟไลน์


codeigniter ไม่เกี่ยวเลยครับ อยู่ที่ library rss ล้วนๆ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2011-11-09 15:52:56 By : mr.v
 


 

No. 2



โพสกระทู้ ( 190 )
บทความ ( 0 )



สถานะออฟไลน์


ไม่ค่อยเข้าใจการเรียกใช้คลาสครับ มีวิธีเปลี่ยน url ในฟังก์ชั่นนี้ไม่ให้จำค่าเดิมได้ไหมครับ

$this->load->library('RSSParser', array('url' => 'http://www.manager.co.th/RSS/Home/Breakingnews.xml', 'life' => 0));

คลาส RSSParser แบบนี้ครับ

Code
class RSSParser
{
// ===================//
//Instance vars //
// ===================//

/* Feed URI */
var $feed_uri;

/* Associative array containing all the feed items */
var $data;

/* Store RSS Channel Data in an array */
var $channel_data;

/* Boolean variable which indicates whether an RSS feed was unavailable */
var $feed_unavailable;

/* Cache lifetime */
var $cache_life;

/* Flag to write to cache - defaulted to false*/
var $write_cache_flag = false;

/* Code Ignitor cache directory */
var $cache_dir;

// ================ //
// Constructor //
// ================ //
function RSSParser($params) {
$this->CI =& get_instance();
$this->cache_dir = ($this->CI->config->item('cache_path') == '') ? BASEPATH.'cache/' : $this->CI->config->item('cache_path');

//$this->cache_dir = '/system/cache';
$this->cache_life = $params['life'];

$this->feed_uri = $params['url'];
$this->current_feed["title"] = '';
$this->current_feed["description"] = '';
$this->current_feed["link"] = '';
$this->data = array();
$this->channel_data = array();

//Attempt to parse the feed
$this->parse();
}

// =============== //
// Methods //
// =============== //
function parse() {
//Are we caching?
if ($this->cache_life != 0)
{

$filename = $this->cache_dir.'rss_Parse_'.md5($this->feed_uri);

//is there a cache file ?
if (file_exists($filename))
{
//Has it expired?
$timedif = (time() - filemtime($filename));
if ($timedif < ( $this->cache_life * 60))
{
//its ok - so we can skip all the parsing and just return the cached array here
$this->data = unserialize(implode('', file($filename)));
return true;
}
//So raise the falg
$this->write_cache_flag = true;

} else {
//Raise the flag to write the cache
$this->write_cache_flag = true;
}
}


//Parse the document
$rawFeed = file_get_contents($this->feed_uri);
$xml = new SimpleXmlElement($rawFeed);

//Assign the channel data
$this->channel_data['title'] = $xml->channel->title;
$this->channel_data['description'] = $xml->channel->description;

//Build the item array
foreach ($xml->channel->item as $item)
{
$data = array();
$data['title'] = $item->title;
$data['description'] = $item->description;
$data['pubDate'] = $item->pubDate;
$data['link'] = $item->link;
$this->data[] = $data;
}

//Do we need to write the cache file?
if ($this->write_cache_flag)
{
if ( ! $fp = @fopen($filename, 'wb'))
{
echo "ERROR";
log_message('error', "Unable to write cache file: ".$cache_path);
return;
}
flock($fp, LOCK_EX);
fwrite($fp, serialize($this->data));
flock($fp, LOCK_UN);
fclose($fp);
}
return true;
}

/* Return the feeds one at a time: when there are no more feeds return false
* @param No of items to return from the feed
* @return Associative array of items
*/
function getFeed($num) {
$c = 0;
$return = array();
foreach($this->data AS $item)
{
$return[] = $item;
$c++;
if($c == $num) break;
}
return $return;
}

/* Return channel data for the feed */
function & getChannelData() {
$flag = false;
if(!empty($this->channel_data)) {
return $this->channel_data;
} else {
return $flag;
}
}

/* Were we unable to retreive the feeds ? */
function errorInResponse() {
return $this->feed_unavailable;
}

}

?>

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2011-11-09 16:02:43 By : mmc01
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : Codeigniter อ่าน RSS feed หลายๆไฟล์แล้วมันแสดงข้อมูลเดียวกันหลายๆครั้งครับ อ่านได้แค่ไฟล์เดียว
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่