0

我有以下 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 来控制它,如果有人能给我一些指示,那将非常有用。

4

2 回答 2

0

好的解决了这个最终删除模型文件并将属性放在 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

来帮助我。希望这对其他人有帮助:)

于 2013-11-11T09:03:50.320 回答
0

我不知道您如何使用视图/模型处理整个案例。但是对于实际的架构,您不会得到想要的结果。

我现在制作了一个快速而肮脏的版本,我将如何实现它。

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 参数在您的控制器中调用类别,并且只将过滤后的结果返回到您的视图

*编辑:正如我所说,这只是快速和肮脏的......如果你想保持你的domdocument方式:)它只是为了这个想法

于 2013-11-08T09:39:43.367 回答