PHP: Bir
  • oluşturmak için nasıl

3 Cevap php

Sanırım bir <ul><li> etiketini ama benim dizi girişi oldukça karmaşık oluşturmak için nasıl birçok PHP fonksiyonu gördük. Bu xml2assoc denilen özel bir işlev döndürülen bir dizidir

Benim soru nasıl döndü xml2assoc dizi PHP kullanarak bir <ul><li> biçimlendirilmiş HTML kodu neden dönüştürebilirsiniz olduğunu.

Teşekkürler.

$tree = array(
    0 => array(
    	'tag' => 'NavigationMode',
    	'value' => array(
    		0 => array(
    			'tag' => 'Title',
    			'value' => 'Introduction'
    		),
    		1 => array(
    			'tag' => 'NavigationNode',
    			'value' => array(
    				0 => array(
    					'tag' => 'Title',
    					'value' => 'Sub Intro'
    				)
    			)
    		)
    	)
    ),
    1 => array(
    	'tag' => 'NavigationMode',
    	'value' => array(
    		0 => array(
    			'tag' => 'Title',
    			'value' => 'Module 1'
    		)
    	)
    )
);

Ben oluşturmak için gereken nihai çıktı şu şekildedir:

<ul>
    <li>
    Introduction
    <ul>
        <li>Sub Intro</li>
    </ul>
    </li>

    <li>Module 1</li>
</ul>

3 Cevap

i demo verileri varyasyonları için test etmedi ...

<?php

function getTitle($node) {
    foreach ($node['value'] as $cnode) {
            if ($cnode['tag'] == 'Title') {
                return $cnode['value'];
            }
    }

    return 'untitled';
}

function getCNodes($node) {
    $cnodes = array();

    foreach ($node['value'] as $cnode) {
        if ($cnode['tag'] == 'NavigationNode') {
            $cnodes[] = $cnode;
        }
    }

    return $cnodes;
}

function runTree($node) {
    $title  = getTitle($node);
    $cnodes = getCNodes($node);

    if (count($cnodes) > 0) {
        $out = '<li>' . $title . "\n" . '<ul>';
        foreach ($cnodes as $cnode) {
            $out .= runTree($cnode);
        }
        $out .= '</ul>' . "\n" . '</li>' . "\n";

        return $out;
    } else {
        return '<li>' . $title . '</li>' . "\n";
    }
}


$tree = array(
    0 => array(
        'tag' => 'NavigationMode',
        'value' => array(
                0 => array(
                        'tag' => 'Title',
                        'value' => 'Introduction'
                ),
                1 => array(
                        'tag' => 'NavigationNode',
                        'value' => array(
                                0 => array(
                                        'tag' => 'Title',
                                        'value' => 'Sub Intro'
                                )
                        )
                )
        )
    ),
    1 => array(
        'tag' => 'NavigationMode',
        'value' => array(
                0 => array(
                        'tag' => 'Title',
                        'value' => 'Module 1'
                )
        )
    )
);



echo '<ul>';
foreach ($tree as $node) {
    echo runTree($node);
}
echo '</ul>';

?>

Eğer girdi olarak XML varsa, neden onu dönüştürmek için XSLT kullanmayın <ul>?

Ben senin girişi böyle bir şey (ben "Navigasyon * M * gazel" bir yazım hatası olduğunu varsayalım) görünüyor sanırım:

<tree>
  <NavigationNode>
    <title>Introduction</title>
    <NavigationNode>
      <title>Sub Intro</title>
    </NavigationNode>
  </NavigationNode>
  <NavigationNode>
    <title>Module 1</title>
  </NavigationNode>
</tree>

Küçük bir XSLT 1.0 biçembente ile:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output omit-xml-declaration="yes" indent="yes" />

  <xsl:template match="/tree">
    <ul>
      <xsl:apply-templates select="NavigationNode" />
    </ul>
  </xsl:template>

  <xsl:template match="NavigationNode">
    <li>
      <xsl:value-of select="title" />
      <xsl:if test="NavigationNode">
        <ul>
          <xsl:apply-templates select="NavigationNode" />
        </ul>
      </xsl:if>
    </li>
  </xsl:template>

</xsl:stylesheet>

Bu çıkış üretilir:

<ul>
  <li>
    Introduction
    <ul>
      <li>Sub Intro</li>
    </ul>
  </li>
  <li>Module 1</li>
</ul>

The PHP documentation shows how to use XSLT. Çok basit.

İşte başlamak için dizi yapısı için hızlı bir PHP uygulamasıdır:

function create_html_list($nodes)
{
    echo '<ul>';

    foreach ($nodes as $node) {
        $childNodes = $node['value'];
        $titleNode = array_shift($childNodes);

        echo "<li>", $titleNode['value'];

        if (count($childNodes) > 0) {
            create_html_list($childNodes);
        }

        echo "</li>";
    }

    echo '</ul>';
}