【问题标题】:array to xml or xspf make xspf file for embed vlc lugin playlist数组到 xml 或 xspf 制作用于嵌入 vlc lugin 播放列表的 xspf 文件
【发布时间】:2016-03-23 22:47:30
【问题描述】:

我正在尝试输入复选框列表当前文件夹.mp3 使用 gob 函数 glob_grace 使用复选框列出音频文件然后

获取数组复选框值使用php制作xspf文件

我为数组写代码到xml

但是小错误请任何人告诉我什么错误以及发生的错误!

<?php   
foreach (glob("*.{mp3,mp4}", GLOB_BRACE) as $filename) {
    $values = $filename ;
    echo "<form action='p.php' method='POST'>";
    echo "<input type='checkbox' name='foo[]' value='$values'>$values <hr>";    

}
    echo "<input type='submit' name='submit' value='Submit'>";
    echo "</form>";

?>

它会转到下面的下一页

<?php
        $g = $_POST['foo'];
       $cnt = count($g);
        //function definition to convert array to xml
function array_to_xml($array, &$xml_user_info) {
    for ($i=0 ; $i < $cnt ;$i++)
    {
        $track = $xml_user_info->addChild('track');
        $track->addChild("location",$array[$i]);
     }                                       

}

//creating object of SimpleXMLElement
$xml_user_info = new SimpleXMLElement("<?xml version=\"1.0\"?><trackList></trackList>");

//function call to convert array to xml
array_to_xml($g,$xml_user_info);

//saving generated xml file
$xml_file = $xml_user_info->asXML('users.xspf');

//success and error message based on xml creation
if($xml_file){
    echo 'XML file have been generated successfully.';
}else{
    echo 'XML file generation error.';
}
?>

请给我答案解决方案工作代码

提前感谢

【问题讨论】:

    标签: php arrays xml xspf


    【解决方案1】:
    $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><playlist></playlist>');
    $trackList = $xml->addChild('trackList');
    foreach ($_POST['files'] as $video) {
        $track = $trackList->addChild('track');
        $track->addChild('location', 'file://'.$tmp_dir.'/'.$video['path']);
        $track->addChild('title', $video['name']);
    }
    file_put_contents('playlist.xspf',$xml->asXML());
    

    【讨论】:

      【解决方案2】:

      如果glob("*.{mp3,mp4}", GLOB_BRACE) 返回一个包含超过 1 项的数组,您将多次生成 &lt;form action='p.php' method='POST'&gt; 标记。

      也许您可以将表单声明移到 foreach 之外。

      如果我提交表单并加载 p.php,我会收到以下通知:

      注意:未定义变量:cnt

      您正在使用$cnt = count($g); 来计算项目,但您也可以通过此$g 并在function array_to_xml 中计算其项目。

      我认为如果你想从$_POST['foo']获取值,你应该先检查它是否是一个POST,然后检查$_POST['foo']是否被设置。

      也许这个设置可以帮助你:

      <?php
      
      //function definition to convert array to xml
      function array_to_xml($array, &$xml_user_info)
      {
          for ($i = 0; $i < count($array); $i++) {
              $track = $xml_user_info->addChild('track');
              $track->addChild("location", $array[$i]);
          }
      }
      
      if ($_SERVER['REQUEST_METHOD'] === 'POST') {
          if (isset($_POST['foo'])) {
              $g = $_POST['foo'];
      
              //creating object of SimpleXMLElement
              $xml_user_info = new SimpleXMLElement("<?xml version=\"1.0\"?><trackList></trackList>");
      
              //function call to convert array to xml
              array_to_xml($g, $xml_user_info);
      
              //saving generated xml file
              $xml_file = $xml_user_info->asXML('users.xspf');
      
              //success and error message based on xml creation
              if ($xml_file) {
                  echo 'XML file have been generated successfully.';
              } else {
                  echo 'XML file generation error.';
              }
          }
      }
      ?>
      

      【讨论】:

      • 感谢四鸟兄弟
      猜你喜欢
      • 1970-01-01
      • 2016-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-14
      • 2011-03-08
      相关资源
      最近更新 更多