读取逗号分隔的数据列,并使用结果显示其他表中的行


Reading a comma seperated data column, and using results to display rows from a different table

目前我对获得所需结果的最佳方法有点困惑。

因此,在我的users表中有一个列名"gshowcase"。这个字段将有一系列用逗号分隔的数字。

这些数字指的是我的"游戏"表中的UID(自动递增)。

因此,如果用户有游戏("4,5,9,15,25"),它将在游戏表的配置文件上显示这些行。

不确定我是否已经很好地描述了这个lol

编辑-----------------------------我哪里错了???

 function getGrelationsList() {
    // The query to select the gamesid from the relations table
    $query = sprintf("SELECT `game` FROM `grelations` WHERE `user` = '%s'", $this->db->real_escape_string($this->id));
    // Run the query
    $result = $this->db->query($query);
    // The array to store the gameids the user plays
    $gamesplayed = array();
    while($row = $result->fetch_assoc()) {
        $gamesplayed[] = $row['game'];
    // Close the query
    $result->close();
    // Return the games info list (e.g: 13,22,19)
    return implode(',', $gamesplayed);
}

function getGshowcase() {
    // The query to select Games info based on gameids returned in previous function
    $query = sprintf("SELECT `idu`, `title`, `image`, `url`, `desc` FROM `games` WHERE `idu` =  $gamesplayed");
    // Run the query
    $result = $this->db->query($query);
    // The array to store the games info
    $gshowcase = array();
    while($row = $result->fetch_assoc()) {
        $gshowcase[] = $row['idu','title','image','url','desc'];
    }
    // Close the query
    $result->close();
    // Return the games info list (e.g: 13,22,19)
    return implode(',', $gshowcase);
}
function sidebarGames($type, $for) {
    global $LNG;
    $result = $this->Gshowcase;
    $title = $LNG['subshowcase'];
    $r = 'showcase';

    // If the select was made
    if($for == 0) {
        $i = 0;
        $output = '<div class="sidebar-container widget-'.$r.'"><div class="sidebar-content"><div class="sidebar-header"><a href="'.$this->url.'/'.((!empty($this->profile)) ? $this->profile : $this->username).'&r='.$r.'">'.$title.' <span class="sidebar-header-light">('.$result[1].')</span></a></div>';
        foreach($result[0] as $row) {
            if($i == 6) break; // Display only the last 6 Games
            // Add the elements to the array
            $output .= '<div class="sidebar-subscriptions"><div class="sidebar-title-container"><a href="'.$row['url'].'"><div class="sidebar-title-name">'.$row['title'].'</div></a></div><a href="'.$row1['url'].'"><img src="'.$this->url.'/thumb.php?src='.$row['image'].'&t=a&w=112&h=112" /></a></div>';
            $i++;
        }
        $output .= '</div></div>';
    } elseif($for == 1) {
        $output = '<strong><a href="'.$this->url.'/'.((!empty($this->profile)) ? $this->profile : $this->username).'&r='.$r.'">'.$result[1].' '.$LNG['people'].'</strong></a>';
    }
    return $output;
}

问候Rhys

您选择的模式设计可以得到改进,以有效地解决您的问题。userDB和gamesDB之间必须存在MANY-TO-MANY关系(假设这些是表而不是dbs:P)。

使用多对多方法,您可以得到用于映射的中间表,例如user_games,它将以userDB's primary key and gamesDB's primary key作为自己的复合主键。

现在我正在用java开发ORM。使用这种模式,我可以在userDB实例中直接拥有list of gamesDB对象。否则,我可以迭代用户的user_games中的每一行,为每个相关游戏准备对象,并将此游戏列表设置为userDB的对象。

现在,使用任何开源lib在JSON中转换这个userDB的对象,并将其返回给客户端。返回的json可能遵循格式

{
  'userid' : 1,
  //other user info...
  games_list : [
     {
        'idu' : 1,
        'title': 'Counter strike...',
        //other info...
     },
     {
        'idu' : 2,
        'title': 'H1Z1',
        //other info...
     },
     //Other associated games
  ]
}

现在在javascript中,您可以轻松地迭代这个json并呈现视图。


如果无法更改数据库模式。你仍然可以使用这种方法。但现在您需要首先通过逗号分割gshowcase来生成game_ids的array,然后对每个id进行迭代,从db中获取游戏行,并从中准备游戏对象。