【问题标题】:Parse XML records into multiple tabs (with PHP or jQuery)将 XML 记录解析为多个选项卡(使用 PHP 或 jQuery)
【发布时间】:2026-02-07 03:10:01
【问题描述】:

我目前正在开发一个 DJ 网站,他使用 GigaTools 进行演出。 目前,我有用 php 解析的 GigaTools 提要(即 xml),并在网站上显示一个表格。但这仅限于显示的最大记录数 (12),因为这适合网站。

这是我目前正在使用的脚本:

      <?php 

    $gigatools = simplexml_load_file('http://gigs.gigatools.com/user/MikeRavelli.xml');
    echo "<table id='gigs_parse'>\n";

    for($i = 0; $i < 12; $i++) {
      $event = $gigatools->event[$i];
      $day=$event->day;
      $month=$event->month;
      $name=$event->name;
      $venue=$event->venue;
      $city=$event->city;
      $country=$event->country;

      if($month == 1){
        $maand = 'JAN';
      } else if($month == 2){
        $maand = 'FEB';
      } else if($month == 3){
        $maand = 'MAR';
      } else if($month == 4){
        $maand = 'APR';
      } else if($month == 5){
        $maand = 'MAY';
      } else if($month == 6){
        $maand = 'JUN';
      } else if($month == 7){
        $maand = 'JUL';
      } else if($month == 8){
        $maand = 'AUG';
      } else if($month == 9){
        $maand = 'SEP';
      } else if($month == 10){
        $maand = 'OCT';
      } else if($month == 11){
        $maand = 'NOV';
      } else if($month == 12){
        $maand = 'DEC';
      }

      echo "<tr class='row'><td class='date'><span>",$day,"</span><br/>",$maand,"</td>\n";
      echo "<td class='party'><a href='",$url,"' target='_blank'>",$name,"</td>\n";
      echo "<td class='location'>",$venue,", ",$city,", ",$country,"</td>\n";
    }
    echo "</table>";
    ?>

我希望 xml 提要显示在多个选项卡中,以便显示更多的演出然后 12。例如,如果提要中有 15 场演出,我希望前 12 场在第一个选项卡上,另一个3 在第二个选项卡上。 我想根据提要中的演出数量显示尽可能多的标签。因此,选项卡的数量必须根据提要中的演出数量自动更改。所以我不会得到任何空标签。

我现在搜索了半天,在互联网上找不到任何好的解决方案。 我编程不是很好,所以我真的需要一个可以调整的教程或脚本。

有人可以通过例如教程为我指明正确的方向或帮助我为此编写脚本吗?

提前致谢! 顺便说一句。对不起我的英语不好;)

【问题讨论】:

    标签: php jquery parsing tabs xml-parsing


    【解决方案1】:

    你可以使用JQuery UI Tabs

    您可以找到一个非常简单的示例here。更改脚本以生成这样的表格应该很容易

    <div id="tabs">
    <ul>
        <li><a href="#tabs-1">First</a></li>
        <li><a href="#tabs-2">Second</a></li>
        <li><a href="#tabs-3">Third</a></li>
    </ul>
    <div id="tabs-1">
        <p>First tab contents</p>
    </div>
    <div id="tabs-2">
        <p>Second tab contents</p>
    </div>
    <div id="tabs-3">
        <p>Third tab contents</p>
    </div>
    </div>
    

    之后,您必须在文档的&lt;head&gt; 中添加 JQuery UI 所需的所有 js 和 css:

    <link rel="stylesheet" href="../../themes/base/jquery.ui.all.css">
    <script src="../../jquery-1.8.0.js"></script>
    <script src="../../ui/jquery.ui.core.js"></script>
    <script src="../../ui/jquery.ui.widget.js"></script>
    <script src="../../ui/jquery.ui.tabs.js"></script>
    <link rel="stylesheet" href="../demos.css">
    

    最后,只需调用实际创建选项卡的函数

    <script>
    $(function() {
        $( "#tabs" ).tabs();
    });
    </script>
    

    【讨论】:

    • 我已经看过了,但问题是我必须预先定义选项卡。我想要的是脚本将自动创建选项卡,具体取决于我从 xml 提要的解析中获得的记录数。
    • 我认为调整服务器端 php 脚本可能更容易做到这一点。您知道事件的数量,因此选项卡的数量将是 ceil(numEvents/12)。现在您知道了选项卡的数量,您只需将当前循环重复更多次即可。
    • 感谢您的回答!我调整了我的 php 脚本并向其中添加了 jQuery UI 选项卡。现在我只需要样式化它;)
    【解决方案2】:

    谢谢大佬!正在为 Gigatools 搜索 PHP XML 解析器...

    我也是 PHP 新手,但是这样会不会更好:

    <?php 
    
        $feed = 'http://gigs.gigatools.com/user/MikeRavelli2.xml';
        $gigatools = simplexml_load_file($feed)or
              die ("Fehler beim Laden des Feeds: ".$feed."\n");;
        $countgigs = count($gigatools->event);
        echo "<table id='gigs_parse'>\n";
        if ($countgigs > 12) {
            $gigs = 12;
        } else {
            $gigs = $countgigs;
        }
    
        for($i = 0; $i < $gigs; $i++) {
          $event = $gigatools->event[$i];
          $day=$event->day;
          $month=$event->month;
          $name=$event->name;
          $venue=$event->venue;
          $city=$event->city;
          $country=$event->country;
    
          if($month == 1){
            $maand = 'JAN';
          } else if($month == 2){
            $maand = 'FEB';
          } else if($month == 3){
            $maand = 'MAR';
          } else if($month == 4){
            $maand = 'APR';
          } else if($month == 5){
            $maand = 'MAY';
          } else if($month == 6){
            $maand = 'JUN';
          } else if($month == 7){
            $maand = 'JUL';
          } else if($month == 8){
            $maand = 'AUG';
          } else if($month == 9){
            $maand = 'SEP';
          } else if($month == 10){
            $maand = 'OCT';
          } else if($month == 11){
            $maand = 'NOV';
          } else if($month == 12){
            $maand = 'DEC';
          }
    
          echo "<tr class='row'><td class='date'><span>",$day,"</span><br/>",$maand,"</td>\n";
          echo "<td class='party'><a href='",$url,"' target='_blank'>",$name,"</td>\n";
          echo "<td class='location'>",$venue,", ",$city,", ",$country,"</td>\n";
        }
        echo "</table>";
        ?>
    

    【讨论】: