PHP For Loop jQuery UI选项卡

问题描述 投票:0回答:1

我正在尝试根据距离和类别在表中显示链接列表。我希望每个距离都成为一个选项卡,并在每个选项卡中具有适当的链接。我正在尝试使用PHP Foreach循环和jQuery-ui Tabs完成此任务。

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

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

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>";
?>

目前,所有数据都显示在每个选项卡中,而不是仅在其他选项卡中显示特定类别的链接。我可以看到,第二张男性和女性表格略微位于顶部表格的左侧,因此我认为循环导致出现问题。

Example of Incorrect Data Being Displayed

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

php jquery codeigniter
1个回答
0
投票

通过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++;
}
© www.soinside.com 2019 - 2024. All rights reserved.