Really?
1. You currently generate a url using a field called ID. Instead generate a url with a field called 'page_url'.
2. You currently do a database query with a search on ID. Instead do a database query with a search on 'page_url'.
Clearly, you need a database column in which the 'page_url' is stored beside your current ID.
That is it.
I can run through your code and replace 'id' with 'page_url' but do you really need me to do that? Also, the column in which your page name is kept might be called 'page_slug', or 'page_code', or 'page_url_name'.
I hope this helps,
Paul.