【问题标题】:PHP For Loop jQuery UI TabsPHP For 循环 jQuery UI 选项卡
【发布时间】:2020-02-13 07:27:42
【问题描述】:

我正在尝试根据距离和类别在表格中显示链接列表。我希望每个距离都是一个选项卡,并在每个选项卡中都有适当的链接。我正在尝试使用 PHP Foreach 循环和 jQuery-UI 选项卡来完成此操作。

这是获取数据并将其显示在每个选项卡的表格中的代码。

视图控制器中的索引函数和获取表数据的函数:

public function index() {
    $data = array();
    $db_name = $this->uri->segment(2);
    $this->db->db_select($db_name);
    $tables = $this->db->get('tableinfo');

    $data['distances'] = array();
    $data['tables'] = array(
        'men' => array(),
        'women' => array()
    );

    foreach($tables->result() as $row) {
        if(!in_array($row->distance, $data['distances'])) {
            array_push($data['distances'], $row->distance);
        }

        if(substr($row->displayname, 0, 4) == "Male") {
            array_push($data["tables"]['men'], $row->displayname);
        } else {
            array_push($data["tables"]['women'], $row->displayname);
        }
    }

    $data['dbname'] = $db_name;

    $this->parser->parse('templates/header', $data);
    $this->parser->parse('select/index', $data);
    $this->parser->parse('templates/footer', $data);
}

public function gettable($table) {
    $db_name = "championchipco_sowetomarathon2019";
    $this->db->db_select($db_name);
    redirect("results/index/" . $db_name . "/" . $table);
}

还有观点:

<?php
    $i = 1;
    echo "<div class='row'>";
    echo "<div class='col' id='tabs'>";
    echo "<ul>";
    foreach($distances as $distance) {
        echo "<li><a href='#tabs-" . $i . "'>" . $distance . "</a></li>";
    }
    echo "</ul>";
    foreach($distances as $distance) {
        echo "<div id='tabs-" . $i . "'>";
        echo "<table class='table' cellspacing='10'  cellpadding='10'>";
        echo "<tr><th style='font-size: 20px;'>Men</th><th style='font-size: 20px;'>Women</th><th style='font-size: 20px;'></tr>";
        foreach($tables['men'] as $table) {
            if(substr($table, -4) == $distance) {
                echo "<tr>";
                echo '<td><a href="' . site_url("select/gettable/" . $tables['men'][$i])  . '" class="link-class">' . $tables['men'][$i] . '</a></td>';
                echo '<td><a href="' . site_url("select/gettable/" . $tables['women'][$i])  . '" class="link-class">' . $tables['women'][$i] . '</a></td>';
                echo "</tr>";
            }
        }
        echo "</table>";
        echo "</div>";
        $i++;
    }
    echo "</div>";
    echo "</div>";
?>

目前,所有数据都显示在每个选项卡中,而不是仅在不同选项卡中显示特定类别的链接。我可以看到男性和女性的第二张桌子稍微偏左一点,所以我认为循环导致出现问题。

我尝试重新安排循环在视图中显示数据的方式,但似乎无法仅在 10KM 选项卡中获得 10KM,在 21KM 选项卡中获得 21KM 等。

【问题讨论】:

    标签: php jquery codeigniter


    【解决方案1】:

    通过 Ajax 获取第二次 foreach 的数据,这将使您的需求变得简单 我提到要删除下面的 foreach

     foreach($distances as $distance) {
        echo "<div id='tabs-" . $i . "'>";
        echo "<table class='table' cellspacing='10'  cellpadding='10'>";
        echo "<tr><th style='font-size: 20px;'>Men</th><th style='font-size: 20px;'>Women</th><th style='font-size: 20px;'></tr>";
        foreach($tables['men'] as $table) {
            if(substr($table, -4) == $distance) {
                echo "<tr>";
                echo '<td><a href="' . site_url("select/gettable/" . $tables['men'][$i])  . '" class="link-class">' . $tables['men'][$i] . '</a></td>';
                echo '<td><a href="' . site_url("select/gettable/" . $tables['women'][$i])  . '" class="link-class">' . $tables['women'][$i] . '</a></td>';
                echo "</tr>";
            }
        }
        echo "</table>";
        echo "</div>";
        $i++;
    }
    

    【讨论】:

    • 嗨!对不起,我对你的意思有点困惑。你的意思是我应该通过 Ajax 从另一个页面加载该部分吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多