【问题标题】:extract icon for each tag from database从数据库中提取每个标签的图标
【发布时间】:2013-03-14 04:14:43
【问题描述】:

我想为每个标签显示图标,就像 Stackoverflow 一样。

有 2 个表相关,table_icon 和 table_event。

table_event 用于记录发生的事件,当然每个事件都有标签; table_tag 用于存储标签和图标。

table_icon 的结构如下:

tag_id
tag_name
icon (an url to an img)

table_event的结构如下:

event_id
name
modtime
tag

在table_event中,tag存储在tag cell中的“tag1,tag2,tag3”。

为了更清楚,这里是一个例子 -

事件:“科比受伤”,相关标签:科比、湖人、伤病

table_event:

event_id ---- 1
name -------- Kobe Bryant got hurt
modtime ----- Mar. 13, 2013
tag --------- Kobe,Lakers,injure

对应的table_tag:(假设我们手头有table_tag中的那些数据)

tag_id ------ 24
tag_name ---- Kobe
icon -------- .../icon/kobe.png


tag_id ------ 123
tag_name ---- Lakers
icon -------- .../icon/NBA_team_lakers.png


tag_id ------ 36
tag_name ---- injure
icon -------- .../icon/red_cross.png

这就是我在 PHP 中提取每个标签的方式:

... // Link to Db and table and call smarty.
$t_sql = mysql_query("SELECT * FROM event WHERE 1");

$id = array();
$name = array();
$tag = array();
$icon = array();

while($t_row = mysql_fetch_array($t_sql)){
    array_push($id, $t_row['event_id']);
    array_push($name, $t_row['name']);
    $tag_array = explode(',', $t_row['tag']);
    array_push($tag, $tag_array);
        // Don't know how to extract icon for each tag.
}

$smarty->assign('tid', $tid);
$smarty->assign('name', $name);
$smarty->assign('tag', $tag);
$smarty->display('event.html');

有什么建议吗?提前谢谢!

【问题讨论】:

    标签: php mysql tags


    【解决方案1】:

    将 MySQL joinIN 子句一起使用。

    SELECT table_event.*
    FROM table_event
    WHERE
        EXISTS
        (
            SELECT 1
            FROM table_tag
            WHERE
            table_tag.tag_id IN(table_event.tag)
        )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 2018-08-21
      • 2020-06-18
      相关资源
      最近更新 更多