dsfdsfdsfdsf1223 2015-07-30 11:23
浏览 88

超链接数据库内容

I'm building a database-driven gaming fansite and I want to be able to hyperlink mentions of items and other words that have their own page. For example, assume this is a page.

Item Name

This is a description of the item being pulled from the database. It can be combined with Item2 and Item3 to make Item4.

I would like to hyperlink Item2, Item3 and Item4 to their respective pages so that a user can navigate through the website more efficiently rather than having to type each name into the search bar manually.

I know that this can be achieved by inserting HTML into the description cell and have the browser to output it, but besides the obvious security risk of outputting unsantiised HTML, it would also be incredibly tedious to do this for each item and the table could begin to look rather ugly as it gets filled with anchor tags.

Nevertheless, I've seen such functionality on other websites and I'm wondering what's the safest and/or most efficient way of achieving this?

  • 写回答

1条回答 默认 最新

  • douzhuo5671 2015-07-30 11:30
    关注

    Create file like item.php.

    if(isset($_GET['name']) && !empty($_GET['name'])) {
    
        $item_name = $_GET['name'];
        //Your code like fetching from database WHERE name LIKE $item_name
        //And also printing your description with the hyperlinks to the  actualy item from $_GET['name']
    
    }
    

    If you have not so much different items you can add the achors with str_replace. Heres an example.

    <?php
    $text = "This is a description of the item being pulled from the database. It can be combined with Item2 and Item3 to make Item4.";
    
    $item_names = array("Item2", "Item3", "Item4");
    
    foreach($item_names as $item_name) {
        $text = str_replace($item_name, "<a href='item.php?name=".$item_name."'>".$item_name."</a>", $text);
    }
    
    echo $text;
    ?>
    

    $item_names must be a array with all you item names.

    And Hyperlinks like:

    This is a description of the item being pulled from the database. It can >be combined with Item2 (item.php?name=item2) and Item3 >(item.php?name=item3) to make Item4 (item.php?name=item4).`

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看