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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效