|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Pmike86 Smarty n00b
Joined: 18 Nov 2015 Posts: 2
|
Posted: Wed Nov 18, 2015 2:08 pm Post subject: Using Index page to route user navigation |
|
|
Hello all,
Newbie to Smarty here and I have, what may seem like, a dumb question.
I would like to be able to route the user's navigation through my index.php file however, I am unsure as to whether I am making a dumb php error or a dumb smarty error. I was hoping someone could help me.
For a general overview of code:
1) If a user is logged in a SESSION variable is set
2) If user is logged in and the user has clicked a link route them to apporpriate page with smart->display("whateverPage.tpl");
Here is some code which will focus primarily on routing the user to the "favorites.tpl" page if they click the "Favorites" link in the navigation. I am attempting to use my Index for routing as a means of codensing my entire project.
Please be easy on me Thanks!
Code: |
if(empty($_SESSION['user_id']) && empty($_SESSION['first_name'])) // If there is a session set
{
if(isset($_POST['clicked'])) //If the session is set and the user has clicked a navigation link switch on which link was clicked
{
$navigation_linked_clicked = $_POST['clicked'];
switch ($naviagtion_linked_clicked)
{
case "f":
ShowFavorites($smarty, $link_array);
break;
case "t":
ShowTasks($smarty, $link_array);
break;
case "a":
ShowAdministration($smarty, $link_array);
break;
}
}
ShowLogIn($smarty); //If the session is set but the user has not clicked a link - ie the user just logged in
}
else //There is not a session - ie this is the user's first visit to our page or the user has logged out.
{
ShowLoggedInUser($smarty, $link_array);
}
function ShowFavorites($smarty, $link_array)
{
$welcome_msg = "Your Favorites";
$user_id = $_SESSION['user_id'];
$user_name = $_SESSION['first_name'];
$privilege = $_SESSION['privilege'];
$logged_in = true;
$user = new User();
$favorites_array = $user->GetFavorites($mysqli, $user_id);
$smarty->assign('user_name', $user_name);
$smarty->assign('welcome_msg', $welcome_msg);
$smarty->assign('logged_in', $logged_in); //logged_in sets our SMARTY logic for display purposes
$smarty->assign('link_array', $link_array); //Link array holds values for the predetermined links the Admin/Owner sets
$smarty->assign('favorites_array', $favorites_array);
switch ($privilege)
{
case 0: //Normal User
break;
case 1: //Owner
$admin = true;
$smarty->assign('admin', $admin);
break;
case 2: //Admin - Not Current Used
$admin = true;
$smarty->assign('admin', $admin);
}
$smarty->display('favorites.tpl');
}
function ShowLogIn($smarty)
{
$welcome_msg = "Please Log In To Continue";
$logged_in = false;
$smarty->assign('welcome_msg', $welcome_msg);
$smarty->assign('logged_in', $logged_in); //logged_in sets our SMARTY logic for display purposes
$smarty->display('index.tpl');
}
|
Lastly, just in case you were curious as to what I am doing to send the $_POST['clicked']:
Code: |
$('#favorites_link').on('click', function(event)
{
$.ajax({
url: "index.php",
data: {clicked: "f"},
type: "POST",
success: function(data)
{
alert("succes");
},
error: function(data)
{
alert("faile");
}
});
});
|
|
|
Back to top |
|
Pmike86 Smarty n00b
Joined: 18 Nov 2015 Posts: 2
|
Posted: Wed Nov 18, 2015 4:08 pm Post subject: |
|
|
I believe I have answered my own question. I will post the code in case anyone has this issue in the future. Some functions have changed since the original but the logic remains the same.
Quick run down. Use smarty->fetch(), echo the output, append output on ajax success to <body> tag
Code: |
if(CheckSession()) // If there is a session set
{
if(isset($_POST['clicked'])) //If the session is set and the user has clicked a navigation link switch on which link was clicked
{
$navigation_linked_clicked = $_POST['clicked'];
switch ($navigation_linked_clicked)
{
case "f":
ShowFavorites($smarty, $link_array, $mysqli);
break;
case "t":
ShowTasks($smarty, $link_array);
break;
case "a":
ShowAdministration($smarty, $link_array);
break;
default:
break;
}
}
else
{
ShowLoggedInUser($smarty, $link_array);
}
}
else //There is not a session - ie this is the user's first visit to our page or the user has logged out.
{
ShowLogIn($smarty);
}
|
Function changes
Code: |
function ShowFavorites($smarty, $link_array, $mysqli)
{
$welcome_msg = "Your Favorites";
$user_id = $_SESSION['user_id'];
$logged_in = true;
$user = new User();
$favorites_array = $user->GetFavorites($mysqli, $user_id);
$smarty->assign('user_name', SetSmartyUserFirstName());
$smarty->assign('welcome_msg', $welcome_msg);
$smarty->assign('logged_in', $logged_in); //logged_in sets our SMARTY logic for display purposes
$smarty->assign('link_array', $link_array); //Link array holds values for the predetermined links the Admin/Owner sets
$smarty->assign('favorites_array', $favorites_array);
$smarty->assign('admin', SetSmartyUserPrivilege($_SESSION['privilege']));
$page = $smarty->fetch('favorites.tpl');
echo($page);
}
|
AJAX function
Code: |
$('#favorites_link').on('click', function(event)
{
event.preventDefault();
$.ajax({
url: "index.php",
data: {clicked: "f"},
type: "POST",
success: function(data)
{
$("body").html(data);
},
error: function(data)
{
}
});
});
|
|
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|