Nathan Nathan - 7 months ago 16
SQL Question

Build string to be used externally in while loop

i am looking to build a string while inside a mysqli while loop. i have a pages table that contains all the pages for my webapp however it has come to seo friendly these urls

my table looks like this

ID | Page Slug | Parent
------------------------
1 | Accounts | 0
2 | Customers | 1
3 | Details | 2


say i call the below function WebApp_UOC_Construct_Url(3) with the page id 3 it will echo out a url with one that looks like the below

/accounts/customers/details


this url is how i want it to be passed to my menus so i know the function is working however this will only echo on the page i need it to return a value inside the href and so if i change the echo to return and call the function like this

<a hef='" . WebApp_UOC_Construct_Url(3) . "'>Customer Details</a>


i only receive the last entry in the database so the url will look like this

/details


what other possible methods are there of creating the above

function WebApp_UOC_Construct_Url($page_id)
{
global $webapp_db;

$get_URI = mysqli_query($webapp_db,
"SELECT * FROM wa_sys_navigation WHERE wa_nav_id = '" . $page_id . "' ");

while($found_URI = mysqli_fetch_array($get_URI))
{
WebApp_UOC_Construct_Url($found_URI['wa_nav_parent']);
echo "/" . $found_URI['wa_nav_slug'];
}
}

Answer

If you change only echo to return you will get only the top level function call result. You need summarize the recursive calls results.

Example:

$parent = WebApp_UOC_Construct_Url($found_URI['wa_nav_parent']);
return $parnet . "/" . $found_URI['wa_nav_slug'];