php codeigniterに特定の属性をタグ付けする場合にのみxmlオブジェクトを表示します

StackOverflow https://stackoverflow.com/questions/19855163

  •  29-07-2022
  •  | 
  •  

質問

次の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>

そして、私がやりたいのは、その1つのページに表示され、他のカテゴリ番号を無視する3のカテゴリがあるすべてのビデオを持っていることです。最新のタグは新しいビデオに使用されるため、これはアクティブの場合は1または表示されない場合は0になります。

各ページには別のビューページがあるが、基本的に同じ形式を使用していることを指摘する必要があります。データベースはありません。したがって、誰かが私にいくつかのポインターを与えることができる場合、これを制御するためにXMLを使用しています。

役に立ちましたか?

解決 2

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

私を助けるために。これが他の人に役立つことを願っています:)

他のヒント

ビュー/モデルでケース全体をどのように処理するかはわかりません。しかし、実際のアーキテクチャを使用すると、希望の結果を得ることができません。

私は今、私がこれをどのように実装するかを簡単に汚いバージョンにしました。

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を維持したいならば:)それはただのアイデアのためだけです

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top