duai1683 2015-10-11 03:55
浏览 24
已采纳

切换数据库值

I have a website with URL's based on scientific names. For example, if the value 'Canidae' is in my database, then my site will display a page at MySite/canidae.

There are a few scientific names that are duplicates. For example, there are two fish general named Leptocephalus, while Phyllanthus is the name of both a plant genus and a bird genus.

If I navigate to MySite/bison - then it recognizes its status as a mammal page through its parent-child relationships. Genus Bison's parent is Bovidae, and its parent is Artiodactyla, and so on until we get to mammals (Mammalia).

So imagine we have a duplicate scientific name - Myname. Let's say one is a mammal with a parent named Mammus and the other is a fish with a parent named Fishus.

If we navigate to MySite/myname, the page recognizes itself as a mammal by default. I'd like to know if there's a way to enable visitors to change the page to a "fish" page by clicking a button or link that changes the parent from Mammus to Fishus. In other words, the URL will still be the same - MySite/myname. But if a visitor clicks a button or link, the content will change from the mammal content to the fish content.

  • 写回答

2条回答 默认 最新

  • duandun3178 2015-10-11 11:53
    关注

    I agree with Michael - sqlbot to the extent that you should have more descriptive URLs for two reasons

    1) It's more user friendly
    2) It's more search engine friendly

    So let's start by saying we are at this page www.examplesite.com/fish/myname which is showing us content about "MyName" the "Fish"

    Interestingly there is also a "Mammal" as well. So we provide buttons to allow the visitor to view that content as well.

    This is how we do it...

    Suppose you have three tables, like this...

    enter image description here
    As you can see we have two "Animals" called "MyName"... but if you trace them back you will see that ID #2 is a Mammal and ID #5 is a Fish

    So... "MyName" has two parent IDs

    We query the database to get all the information we need using JOIN

    $name = 'MyName';
    $sql = "SELECT Animals.*, Species.*, ParentTable.* FROM Animals JOIN Species ON Animals.subcatid = Species.id JOIN ParentTable ON Species.parentid = ParentTable.id WHERE Animals.name=:name";
    $query = $conn->prepare($sql);
    $query->bindValue(':name', $name, PDO::PARAM_STR);
    $query->execute();
    $row = $query->fetchAll();
    

    We then echo the buttons to the page linking them to their own unique URL

    <?php foreach($row as $id) { ?>
    <input name="switch" type="button" value="<?php echo $id['name'] ?>" onclick="goToURL('self','/<?php echo $id['name'] ?>/MyName');return document.MM_returnValue">
    <?php } ?>
    

    And here is the js for the button action

    <script type="text/javascript">
    <!--
    function goToURL() { //v3.0
    var i, args=goToURL.arguments; document.MM_returnValue = false;
    for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
    }
    //-->
    </script>
    

    Happy Coding !

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器