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 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟