drvvepadi289421028 2015-10-25 14:05
浏览 54
已采纳

MySQL发布表处理URL中的父项

I have a table in MySQL with the following structure:

sequence
slug
parent

the URL's look like:

www.domain.com/slug

where slug = result from the database

i want to be able to redirect the user to include all the parent slug items if they visit the wrong URL

here is the code i currently have:

$sql="SELECT * from website_posts where slug = '".$PageID."' ";
$rs=mysql_query($sql,$conn);
$website_posts=mysql_fetch_array($rs);

$slug = '';

if($website_posts["parent"] != '') {
    $sql="SELECT * from website_posts where sequence = '".$website_posts["parent"]."' ";
    $rs=mysql_query($sql,$conn);
    $result=mysql_fetch_array($rs);

    $slug.= $result["slug"].'/'.$website_posts["slug"]; 

    $loop=true;

    while($loop) {
        if($_SERVER["REQUEST_URI"] != $slug) {
            $sql="SELECT * from website_posts where sequence = '".$result["parent"]."' ";
            $rs=mysql_query($sql,$conn);
            if(mysql_num_rows($rs) > 0) {
                $result=mysql_fetch_array($rs);

                $slug.= '/'.$result["slug"];
            } else {
                $loop=false;
            }
        }
    }
}

i thought maybe creating a loop until the current URL equals the full slug but its not processing it correctly.

here is some sample data:

sequence    slug          parent
5553        about
5554        the-company   5553
5555        the-company2  5554

based on this data, the URL should be:

domain.com/about/the-company/the-company2 but when i echo $slug in the above code is returning the-company/the-company2/about

New table structure:

sequence    slug          parent
5553        one
5554        two           5553
5555        three         5554
5556        four          5555
5557        five          5556
  • 写回答

1条回答 默认 最新

  • douqingnao9246 2015-10-25 14:49
    关注

    This is like tree but reversed order , you can use this

    $slug=$PageID;
        $query=mysql_query("SELECT sequence, slug, parent FROM website_posts where slug='".$slug."'");
        $row=mysql_fetch_assoc($query);
        $url=array();
        if($row){
        $url[]=$row["slug"];
            if($row["parent"]){
                    $query=mysql_query("SELECT sequence, slug, parent FROM website_posts where sequence='".$row["parent"]."'");
                    $r=mysql_fetch_assoc($query);
        while($r){ 
                        $url[]=$r["slug"];
    
                        if(!$r["parent"]){$r=false;}
                        else{
                            $q=mysql_query("SELECT sequence, slug, parent FROM website_posts where sequence='".$r["parent"]."'");
                        $r=mysql_fetch_assoc($q);
                        }
                    }
    
            }
        }
    
        $url=array_reverse($url);
        //print_r($url);
        $url=implode("/", $url);
        echo $url;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!