[英]PHP For Loop jQuery UI Tabs
我正在嘗試根據距離和類別在表格中顯示鏈接列表。 我希望每個距離都是一個標簽,並在每個標簽中都有適當的鏈接。 我正在嘗試使用 PHP Foreach 循環和 jQuery-UI 選項卡來完成此操作。
這是獲取數據並將其顯示在每個選項卡的表格中的代碼。
控制器中View的索引函數和獲取表數據的函數:
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>";
?>
目前,所有數據都顯示在每個選項卡中,而不是僅在不同選項卡中顯示特定類別的鏈接。 我可以看到男性和女性的第 2 個表稍微靠在頂部的左側,所以我認為循環導致了一些問題。
我嘗試重新安排循環在視圖中顯示數據的方式,但似乎無法在 10KM 選項卡中僅獲得 10KM,在 21KM 選項卡中獲得 21KM,等等。
通過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++;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.