clean url with slug in database

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
JMlab
New php-forum User
New php-forum User
Posts: 3
Joined: Wed Jan 10, 2018 6:38 pm

Wed Jan 10, 2018 6:48 pm

Hello all. I need i title help.
I've made a full CMS (without any framework) and Im stuck in clean url's.
My table contains a clean url field (actually there are 3 tables that contains the 3 levels of menus, each with one clean url field).

Im stuck here:
index.php?page=about-us

Can't pass over here.
Anyone can help?
Thank you

chorn
php-forum Active User
php-forum Active User
Posts: 427
Joined: Fri Apr 01, 2016 2:18 am

Wed Jan 10, 2018 10:52 pm

what? var_dump($_GET['page']);

JMlab
New php-forum User
New php-forum User
Posts: 3
Joined: Wed Jan 10, 2018 6:38 pm

Thu Jan 11, 2018 3:34 am

chorn wrote:
Wed Jan 10, 2018 10:52 pm
what? var_dump($_GET['page']);
Hello.
The "problem" is the structure and complexity of my menu.
I don't know how to call the clean url from database and echo it in url. :(
My knowledge of htaccess and PHP is very limited.

Thank you

chorn
php-forum Active User
php-forum Active User
Posts: 427
Joined: Fri Apr 01, 2016 2:18 am

Thu Jan 11, 2018 4:54 am

how do oyu get anything else from the database?

JMlab
New php-forum User
New php-forum User
Posts: 3
Joined: Wed Jan 10, 2018 6:38 pm

Thu Jan 11, 2018 5:51 am

This is my menu code:

=================================================================

<?php

function find_all_parents_pt($public=true) {
global $dbc;
$query = "SELECT * ";
$query .= "FROM parent_pt ";
if($public) {
$query .= "WHERE visivel = 1 ";
}
$query .= "ORDER BY ordem ASC";
$parent_set = mysqli_query($dbc, $query);
confirm_query($parent_set);
return $parent_set;
}

function find_fchild_for_parents_pt($parent_id, $public=true) {
global $dbc;

$safe_parent_id = mysqli_real_escape_string($dbc, $parent_id);

if(is_numeric($parent_id)) {
$cond = "WHERE parent_id = {$safe_parent_id} ";
} else {
$cond = "WHERE url = '$safe_parent_id' ";
}

$query = "SELECT * ";
$query .= "FROM fchild_pt $cond ";
if($public) {
$query .= "AND visivel = 1 ";
}
$query .= "ORDER BY ordem ASC";
$fchild_set = mysqli_query($dbc, $query);
confirm_query($fchild_set);
return $fchild_set;
}

function find_schild_for_fchild_pt($fchild_id, $public=true) {
global $dbc;

$safe_fchild_id = mysqli_real_escape_string($dbc, $fchild_id);

if(is_numeric($fchild_id)) {
$cond = "WHERE fchild_id = {$safe_fchild_id} ";
} else {
$cond = "WHERE url = '$safe_fchild_id' ";
}

$query = "SELECT * ";
$query .= "FROM schild_pt $cond ";
if($public) {
$query .= "AND visivel = 1 ";
}
$query .= "ORDER BY ordem ASC";
$schild_set = mysqli_query($dbc, $query);
confirm_query($schild_set);
return $schild_set;
}

function find_parent_pt_by_id($parent_id) {
global $dbc;

$query = "SELECT * ";
$query .= "FROM parent_pt ";
$query .= "WHERE parent_id = {$safe_parent_id} ";
$query .= "LIMIT 1";
$parent_set = mysqli_query($dbc, $query);
confirm_query($parent_set);
if($parent = mysqli_fetch_assoc($parent_set)) {
return $parent;
} else {
return null;
}
}

function find_fchild_pt_by_id($fchild_id) {
global $dbc;

$safe_fchild_id = mysqli_real_escape_string($dbc, $fchild_id);

if(is_numeric($fchild_id)) {
$cond = "WHERE fchild_id = {$safe_fchild_id} ";
} else {
$cond = "WHERE url = '$safe_fchild_id' ";
}

$query = "SELECT * ";
$query .= "FROM fchild_pt $cond ";
$query .= "LIMIT 1";
$fchild_set = mysqli_query($dbc, $query);
confirm_query($fchild_set);
if($fchild = mysqli_fetch_assoc($fchild_set)) {
return $fchild;
} else {
return null;
}
}

function find_schild_pt_by_id($schild_id) {
global $dbc;

$safe_schild_id = mysqli_real_escape_string($dbc, $schild_id);

if(is_numeric($schild_id)) {
$cond = "WHERE schild_id = {$safe_schild_id} ";
} else {
$cond = "WHERE url = '$safe_schild_id' ";
}

$query = "SELECT * ";
$query .= "FROM schild_pt $cond ";
$query .= "LIMIT 1";
$schild_set = mysqli_query($dbc, $query);
confirm_query($schild_set);
if($schild = mysqli_fetch_assoc($schild_set)) {
return $schild;
} else {
return null;
}
}

function find_selected_page_pt() {
global $current_parent;
global $current_fchild;
global $current_schild;

if (isset($_GET["parent"])) {
$current_parent = find_parent_pt_by_id($_GET["parent"]);
$current_fchild = null;
$current_schild = null;
} elseif (isset($_GET["fchild"])) {
$current_fchild = find_fchild_pt_by_id($_GET["fchild"]);
$current_parent = null;
$current_schild = null;
} elseif (isset($_GET["schild"])) {
$current_schild = find_schild_pt_by_id($_GET["schild"]);
$current_parent = null;
$current_fchild = null;
} else {
$current_parent = null;
$current_fchild = null;
$current_schild = null;
}
}

function website_menu_pt_public($parent_array, $fchild_array, $schild_array) {
$output = "<ul id=\"accordion\" class=\"parent-menu clearfix\">";
$parent_set = find_all_parents_pt();

while($parent = mysqli_fetch_assoc($parent_set)) {

// Parent Menu li //
$output .= "<li";
if ($parent_array && $parent["id"]) {
$output .= " class=\"selected\"";
} if ($parent["cor"] == 1) {
$output .= " class=\"menu-cor-01\"";
} elseif ($parent["cor"] == 2) {
$output .= " class=\"menu-cor-02\"";
} elseif ($parent["cor"] == 3) {
$output .= " class=\"menu-cor-03\"";
} elseif ($parent["cor"] == 4) {
$output .= " class=\"menu-cor-04\"";
}
$output .= ">";

$output .= "<a href=\"#\">";
$output .= htmlentities($parent["titulo"]);
$output .= "</a>";

$fchild_set = find_fchild_for_parents_pt($parent["parent_id"]);
// First Child Menu ul //
$output .= "<ul>";
while($fchild = mysqli_fetch_assoc($fchild_set)) {
// First Child Menu li //
$output .= "<li>";

$output .= "<a";
if ($fchild_array && $fchild["fchild_id"] == $fchild_array["fchild_id"]) {
$output .= " class=\"seleccionado\"";
}
$output .= " href=\"?fchild=";
$output .= $fchild["url"];
$output .= "\">";
$output .= htmlentities($fchild["titulo"]);
$output .= "</a>";

$schild_set = find_schild_for_fchild_pt($fchild["fchild_id"]);
// Second Child Menu ul //
$output .= "<ul>";
while($schild = mysqli_fetch_assoc($schild_set)) {
// First Child Menu li //
$output .= "<li>";

$output .= "<a";
if ($schild_array && $schild["schild_id"] == $schild_array["schild_id"]) {
$output .= " class=\"seleccionado\"";
}
$output .= " href=\"?schild=";
$output .= $schild["url"];
$output .= "\">";
$output .= htmlentities($schild["titulo"]);
$output .= "</a>";

$output .= "</li>";
}

mysqli_free_result($schild_set);
$output .= "</ul>";
// Second Child Menu ul //

$output .= "</li>";
// First Child Menu li //

}

mysqli_free_result($fchild_set);

$output .= "</ul>";
// First Child Menu ul //

$output .= "</li>";
// Parent Menu li //

}

mysqli_free_result($parent_set);

$output .= "</ul>";
// Parent Menu ul //

return $output;

}
?>

========================================================

And in index.php, I call the function with:

<?php require_once ('includes/public_menu_pt.php'); ?>
<?php find_selected_page_pt(); ?>

chorn
php-forum Active User
php-forum Active User
Posts: 427
Joined: Fri Apr 01, 2016 2:18 am

Thu Jan 11, 2018 10:28 pm

I dont really get what this code does, where exactly do you have the problem?

I would also recommend to refactor this code to make it more readable, e.g.:

- seperate logic from what it looks like, skip out the HTML to a dedicated function
- do not use GLOBAL so you make this more comprehensible
- is it necessary to query the database so often?
- instead of using SELECT * you can write the exact columns you need, and anybody else would get a slight inside of what your database looks like

Post Reply