我有以下XML文件:
<?xml version="1.0" ?>
<Videos>
<video>
<Title>Video Title</Title>
<SubHeading>SubTitle</SubHeading>
<url>UIIArHNaKtE</url>
<image>image.png</image>
<category>3</category>
<latest>0</latest>
</video>
<video>
<Title>Video Title</Title>
<SubHeading>SubTitle</SubHeading>
<url>UIIArHNaKtE</url>
<image>image.png</image>
<category>3</category>
<latest>0</latest>
</video>
<video>
<Title>Video Title</Title>
<SubHeading>SubTitle</SubHeading>
<url>UIIArHNaKtE</url>
<image>image.png</image>
<category>3</category>
<latest>0</latest>
</video>
</Videos>
使用模型文件:
<?php
class xmlmodel extends CI_Model{
public function catalog(){
$doc = new DOMDocument();
$path = '../application/libraries/VideoData.xml';
$doc->load($path);//xml file loading here
$data = $doc->getElementsByTagName('video');
return $data;
}
}
控制器:
<?php
class SixD extends CI_Controller {
public function index($sixD = 'latestadditions') {
if (!file_exists('../application/views/page/' . $sixD . '.php')) {
// Whoops, we don't have a page for that!
show_404();
}
$this->load->model('xmlmodel');
$data['category_catalog_entity'] = $this->xmlmodel->catalog();
$this->lang->load('common/menu.php');
$this->lang->load('common/headings.php');
$this->lang->load('common/links.php');
$this->lang->load('common/footer.php');
$this->lang->load('page/' . $sixD . '.php');
$this->load->view('templates/common/header');
$this->load->view('page/' . $sixD, $data);
$this->load->view('templates/common/footer');
}
}
并查看:
<div class="container">
<h1 class="pageheading">
<?php echo lang('heading_pageheading'); ?>
</h1>
<hr class="heading" />
<div class="row">
<div class="col-sm-12">
<div class="row">
<div class="col-sm-8 col-lg-9">
<?php foreach($category_catalog_entity as $result){ ?>
<div class="row videopane software">
<div class="col-sm-7 col-sm-push-5">
<h3 class="pageheading"><?php echo anchor('http://youtu.be/' . $result->getElementsByTagName('url')->item(0)->nodeValue, $result->getElementsByTagName('Title')->item(0)->nodeValue . '<br /><small>' . $result->getElementsByTagName( "SubHeading" )->item(0)->nodeValue . '</small>', array('class' => 'fancybox-media'));?></h3>
</div>
<div class="col-sm-5 col-sm-pull-7 text-center">
<?php echo anchor('http://youtu.be/' . $result->getElementsByTagName('url')->item(0)->nodeValue . '?autoplay=1', img("Thumbnails/" . $result->getElementsByTagName('image')->item(0)->nodeValue, $result->getElementsByTagName('Title')->item(0)->nodeValue, $result->getElementsByTagName('Title')->item(0)->nodeValue, "img-responsive img-thumbnail"), array('class' => 'fancybox-media')); ?>
</div>
</div>
<?php }
} ?>
</div>
<div class="col-sm-4 col-lg-3 software">
<?php $this->load->view('modules/menu'); ?>
</div>
</div>
</div>
</div>
</div>
这一切都有效但是我想定义哪个视频显示在哪个页面上,所以我有2个标签:
<category>3</category>
<latest>1</latest>
我要做的是让所有类别为3的视频显示在该页面上并忽略任何其他类别编号。最新的标签用于新视频,因此这将是1表示活动,0表示不显示。
我应该指出每个页面都有一个单独的视图页面,但基本上使用相同的格式。没有数据库因此我使用XML来控制这个,如果有人能给我一些指针会非常有用。
答案 0 :(得分:0)
我不知道你如何使用views / models处理整个案例。但是根据实际的架构,你不会得到希望的结果。
我现在制作了一个快速且简洁的版本,我将如何实现这一点。
public function catalog($category_id = 1)
{
$data = array();
//$path = '../application/libraries/VideoData.xml';
$path = '1.xml';
$xml = simplexml_load_file($path);
$json = json_encode($xml);
$array = json_decode($json, TRUE);
foreach ($array['video'] as $video)
{
if ($video['category'] == $category_id)
{
$data[] = $video;
}
}
return $data;
}
当然,您可以保留原始的calatog方法并使用新方法来获取想要的视频。
通过这种方式,你可以通过url-parameter调用你的控制器类别,并只将过滤后的结果返回给你的视图
*编辑:正如我所说,这只是快速和肮脏...如果你想保持你的domdocument方式:)它只是为了这个想法
答案 1 :(得分:0)
Ok解决了这个问题,最终删除了模型文件并将属性放在xml文件中:
<Videos>
<video type="Module">
<Title>TITLE</Title>
<SubHeading>SUBHEADING</SubHeading>
<url>YOUTUBECODE</url>
<image>IMAGE.png</image>
<description>DESCRIPTIOM</description>
<link><![CDATA[<a href="/URL"><i class='fa-li fa fa-angle-double-right'></i>LINK</a>]]></link>
<link2><![CDATA[<a href="/URL"><i class='fa-li fa fa-angle-double-right'></i>LINK</a>]]></link2>
</video>
<video type="Module" id="1">
<Title>TITLE</Title>
<SubHeading>SUBHEADING</SubHeading>
<url>YOUTUBECODE</url>
<image>IMAGE.png</image>
<description>DESCRIPTIOM</description>
<link><![CDATA[<a href="/URL"><i class='fa-li fa fa-angle-double-right'></i>LINK</a>]]></link>
<link2><![CDATA[<a href="/URL"><i class='fa-li fa fa-angle-double-right'></i>LINK</a>]]></link2>
</video>
<video type="Common" id="1">
<Title>TITLE</Title>
<SubHeading>SUBHEADING</SubHeading>
<url>YOUTUBECODE</url>
<image>IMAGE.png</image>
<description>DESCRIPTIOM</description>
<link><![CDATA[<a href="/URL"><i class='fa-li fa fa-angle-double-right'></i>LINK</a>]]></link>
<link2><![CDATA[<a href="/URL"><i class='fa-li fa fa-angle-double-right'></i>LINK</a>]]></link2>
</video>
</Videos>
这使我能够在我想要的页面上显示各个“视频”。使用:
<div class="col-sm-8 col-lg-9">
<?php if(file_exists('../application/libraries/VideoData.xml')) {
$xml = simplexml_load_file('../application/libraries/VideoData.xml');
$count = 0;
$counts = 0;
foreach($xml->children() as $child) {
$role = $child->attributes(); {
if($role["1"])
echo('<div class="media thumbnail software"><div class="' . (++$counts%2 ? "col-sm-5 text-center" : "col-sm-5 col-sm-push-7 text-center") . '">' . anchor('http://youtu.be/' . $child->url . '?autoplay=1', img("Thumbnails/" . $child->image, $child->Title, $child->Title, "media-object img-responsive img-thumbnail"), array('class' => 'fancybox-media')) . '</div><div class="media-body ' . (++$count%2 ? "col-sm-7" : "col-sm-7 col-sm-pull-5") . '"><h3 class="media-heading pageheading">'.anchor('http://youtu.be/' . $child->url . '?autoplay=1', $child->Title . '<br /><small>' . $child->SubHeading . '</small>', array('class' => 'fancybox-media')) . '</h3><hr class="heading"><p>'. $child->description .'</p><ul class="fa-ul"><li>' . $child->link .'</li></ul></div></div>');
}
}
} ?>
</div>
或者:
<div class="col-sm-8 col-lg-9">
<?php if(file_exists('../application/libraries/VideoData.xml')) {
$xml = simplexml_load_file('../application/libraries/VideoData.xml');
$count = 0;
$counts = 0;
foreach($xml->children() as $child) {
$role = $child->attributes(); {
if($role == "Module")
echo('<div class="media thumbnail software"><div class="' . (++$counts%2 ? "col-sm-5 text-center" : "col-sm-5 col-sm-push-7 text-center") . '">' . anchor('http://youtu.be/' . $child->url . '?autoplay=1', img("Thumbnails/" . $child->image, $child->Title, $child->Title, "media-object img-responsive img-thumbnail"), array('class' => 'fancybox-media')) . '</div><div class="media-body ' . (++$count%2 ? "col-sm-7" : "col-sm-7 col-sm-pull-5") . '"><h3 class="media-heading pageheading">'.anchor('http://youtu.be/' . $child->url . '?autoplay=1', $child->Title . '<br /><small>' . $child->SubHeading . '</small>', array('class' => 'fancybox-media')) . '</h3><hr class="heading"><p>'. $child->description .'</p><ul class="fa-ul"><li>' . $child->link2 .'</li></ul></div></div>');
}
}
} ?>
</div>
根据我想要显示的内容。感谢KURN的帮助,因为它让我看到了不同的方法,最终使用了:
http://us3.php.net/manual/en/simplexmlelement.attributes.php#113164
来帮助我。希望这有助于其他任何人:)