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