Redirect to controller - Codeigniter - php

I have many views in a folder in views folder, for example in views/admin folder.
When I code
if (!empty($users->users)):
foreach ($users->users as $user) {
$link = site_url('ContentController/showActivities/' . $user->id);
$what = $user->banned == 0 ? 'b':'u';
$banned_t = $user->banned == 0 ? 'Banned':'Un-banned';
echo '<tr id="user_'.$user->id.'">';
echo '<td><div class="userimg"><img src="' . $user->profile_pic->medium . '"></div></td>';
echo '<td>'.$user->name . '</td>';
echo '<td>';
echo $user->last_login == '' ? 'never' : $user->last_login;
echo '</td>';
echo $user->banned == 0 ? '<td><span class="label label-success">Active</span></td>' : '<td><span class="label label-danger">Banned</span></td>';
echo '<td>'.$user->email.'</td>';
echo '<td><div class="btn-group">
<button type="button" onclick="Buckty.user.edit($(this));" data-toggle="modal" data-target="#editUser" data-id="'.$user->id.'" class="btn btn-danger">Edit</button>
<button type="button" class="btn btn-danger dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu" role="menu">
<li><a onclick="javascript:ref('.$user->id.')">Show Activities</a></li>
<li><a onclick="Buckty.user.ban($(this));" data-id="'.$user->user_hash.'" data-what="'.$what.'">'.$banned_t.'</a></li>
<li><a onclick="Buckty.user.remove($(this));" data-id="'.$user->id.'">Delete</a></li>
</ul>
</div></td>';
echo '</tr>';
}
endif;
And this is the code that i want to redirect to controller
<li><a onclick="javascript:ref('.$user->id.')">Show Activities</a></li>
Javascript:
function ref(id) {
window.open('ContentController/showActivities/'+id, '_blank');
}
But, when i click "Show Activities" menu, it always redirect to http://localhost/file-sharing/admin/ContentController/showActivities/1 and it doesn't redirect to ContentController/showActivities controller. It should redirect to that controller if the url is http://localhost/file-sharing/ContentController/showActivities/1
Anybody know what should I do in this case? Thanks in advance.

Being Owner of this platform. Let me clear you few things. Site url is not being defined by base_url() or site_url() for functions and compatibility reasons.
First of all , i would not recommend you to not use controller names , try to organise and make routes for your controllers inside routes.php but now let's talk about your problem.
site_url() is your current url you are on. But you have very simple access to global settings for everything.
Introduction to global variables
$user || this contains currently logged in user's data , you can use it anywhere inside views
$this->current_user || this contains similar data to above variable , but this should be used for controllers.
$site || this contains site settings , every site settings data you have saved in database (from site_url to ads settings )
$site->site_url || this is how you use your site url inside this platform
$this->site || this should be used inside controllers to fetch site settings
so according to your problem , you should be using $site->site_url and not site_url();
basically change this :
$link = site_url('ContentController/showActivities/' . $user->id);
to this
$link = $site->site_url.'ContentController/showActivities/' . $user->id;
or if we talk about javascript then global variable for javascript is also there. you should be using site_url as site link so edit the below :
function ref(id) {
window.open('ContentController/showActivities/'+id, '_blank');
}
to this
function ref(id) {
window.location.replace(site_url + 'ContentController/showActivities/'+id, '_blank');
}
here is introduction to global variables for javascript :
site_url || this contains your site url (from site settings )
user_data || this contains current user's data (everything only password is not there )
site_info || this contains site settings data.
tran || this contains multi language translations
current_folderĀ || this contains current folder's hash id , on which you are currently
current_folder is 0 if it's on root folder , else it if folderĀ if it's on un-functional view.

Try this :
function ref(id) {
window.open('<?php echo base_url()?>ContentController/showActivities/'+id, '_blank');
}

Related

PHP creating url with parameters

I am new to PHP and want to create a link with parameters.
So I used this :
<li>
<!-- $GLOBALS["ROOT_PATH"] is where my index.php file located -->
<a href="<?php echo $GLOBALS["ROOT_PATH"]."/Views/pages/profile.php"; ?>">
Profile
</a>
</li>
but when I click on the page it doesn't send me to the page or do anything. When I look at the URL it's something like file:///C:/bla/bla/bla/Views/pages/profile.php
Edit:
Basically, I want to use this in my header.php but my files are :
index.php
Views/pages/profile.php
and so on.
When I use the relative path for the header the path changes for these pages. How can I solve this?
So I solved it using some tricky way but it works :
function createLink($url,$text,$class){
echo "<li>";
// Gets the current php file name.
$basename = substr(strtolower(basename($_SERVER['PHP_SELF'])),0,strlen(basename($_SERVER['PHP_SELF']))-4);
if($basename !== "index"){
$url = "../../".$url;
}
echo '<a href="'. $url . '">';
echo '<span class="'.$class.'"></span>';
echo ' '.$text.'';
echo "</a>";
echo "</li>";
}
So it just add the relative path if the file name is not index.php.

How to make the class active only for the pages that are open

Here is my pages
index.php
blog.php
contact.php
I am using the common file.
Here is my problem
I want to show the class='active' only the pages that are open how can i do that
<a href='index.php' class='active'>Home</a>
<a href='blog.php' class='active'>Blog</a>
<a href='contact.php' class='active'>Contact</a>
If i show the class='active' for all pages it is not the correct but how can it show it for only the pages that is currently opened by identifying it by url
My url will be something like this
www.mywebsite.com/index.php
www.mywebsite.com/blog.php
www.mywebsite.com/contact.php
Note :
I am not using any framework i am just using core php
You can do this by using the following steps
Identifying the url
http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]
Exploding with / for getting the page name
Taking the extension i.e., .php by using substr
And putting it the remaining with the condition
So,
$Url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$Exploded = explode('/', $Url);
$LastPart = end($Exploded);
$ExactName = substr($LastPart, 0, -4);
And you will get the $ExactName as index or blog or contact.
So, from here you can make the conditions to display the class='active' as you required.
Condition :
I have done it simply altogether as $Page
$Page = substr(end(explode('/', "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]")), 0, -4);
$Class = 'class="active"';
<a href="index.php"<?php if($Page=='index' || $Page==''){ echo $Class; } ?> >Home</a></li>
<a href="about.php" <?php if($Page=='about'){ echo $Class; } ?>>About</a>
<a href="contact.php" <?php if($Page=='contact'){ echo $Class; } ?>>Contact</a></li>

Streamline these if statements and output them with their values

Is there a way that I can streamline my process when I'm working with theme options on Wordpress?
Currently I use a setup like this when I'm creating a social links menu for example (With the option value being the link URL);
$twitter = of_get_option('twitter');
$facebook = of_get_option('facebook');
$google-plus = of_get_option('google-plus');
if ($twitter){
echo '<li class="twitter"><i class="fa fa-twitter"></i></li>';
}
if ($facebook){
echo '<li class="facebook"><i class="fa fa-facebook"></i></li>';
}
if ($google-plus){
echo '<li class="google-plus"><i class="fa fa-google-plus"></i></li>';
}
I'm sure there must be an easier, more streamlined, way to go about doing this?
Any help would be greatly appreciated.
I'd wrapp it in a function.
function getOption($op) {
$link = of_get_option($op);
if($link)
echo '<li class="'.$op.'"><i class="fa fa-'.$op.'"></i></li>';
}
getOption('twitter');
getOption('facebook');
getOption('google-plus');

Fail to using if/else to hover image by dynamic url

I'm using the basic way to doing the hover image as the CSS method doesn't work for me. Current I'm using the if/else statement to do so. If the contain the URL like abc.com it will hover the image.
But now I only can hover the group url but if there is sub categories in groups I won't able to hover, how can I do it all the activity inside the group, the image will hover?
How to doing if the URL contain the words or path. For example abc.com/groups/* it will hover the groups. Similar like we doing searching in MySQL the words/variable as using "%".
<?php
$request_url = apache_getenv("HTTP_HOST") . apache_getenv("REQUEST_URI");
$e = 'abc.com/dev/';
$f = 'abc.com/dev/groups/';
$g = 'abc.com/dev/user/';
?>
<div class="submenu">
<?php
if ($request_url == $e) {
echo '<div class="icon-home active"></div>';
} else {
echo '<div class = "icon-home"></div>';
}
?>
<?php
if ($request_url == $f) {
echo '<div class="icon-groups active"></div>';
} else {
echo '<div class = "icon-groups"></div>';
}
?>
</div>
I propose a javascript way to do so, with jQuery
$("a[href*='THE_URL_PATTERN_YOU_WANT_TO_MATCH']").children(".icon-home").addClass("active");
BTW, it is NOT a good idea to wrap a div into a a tag.

Working with PHP_SELF

Apologies for the bad title. I need a class to be added to an A tag depending on if the user is on respective page. So to clarify, here is the code:
<?php
$basename = substr(strtolower(basename($_SERVER['PHP_SELF'])),0,strlen(basename($_SERVER['PHP_SELF']))-4);
?>
And then I use this code in the menu:
<li><a href="index.php"<?php if ($basename == 'index') { echo ' class="current"'; } ?>>Home</a></li>
<li><a href="about.php"<?php if ($basename == 'about') { echo ' class="current"'; } ?>>About</a></li>
As you can see, depending on if the user is on index.php or about.php, the class=current will be inserted. This works fine normally, but I am using this code in Wordpress where all the pages are this type of URL: index.php?page_id=X
So the about page URL is index.php?page_id=9, meaning that it will always input the class into the index one. Only solutions that I know of is that the $basename == 'index' can in anyway be full URL, e.g. $basename == 'index.php?page_id=X' but I couldnt make that work.
Help! Note that I am not experienced with PHP so any replies with detail would be appreciated!
the current file: __FILE__
the current folder of your file dirname(__FILE__).DIRECTORY_SEPARATOR // in 5.3: __DIR__
$page = $_GET[page_id];
I believe this will return for example 9 if the url is ?page_id=9
Considering you're using Wordpress, I'd suggest that you make a variable towards the top of your page that determines that page's current location.
$path_parts = pathinfo($_SERVER['PHP_SELF']);
$current = strtolower($path_parts['filename']);
Then take that variable and set it in the <a></a>, as such:
<a <?php if($current == 'about') echo 'class="current"'; ?> href="#">About</a>
Or something like this, it's a start anyway.
additional information: http://php.net/manual/en/function.pathinfo.php
function GetFileName()
{
$currentFile = basename($_SERVER["PHP_SELF"]);
$parts = Explode('.', $currentFile);
return $parts[0];
}
$basename = GetFileName();
<li>
<a href="index.php" <?php if($basename =="index") echo "class='current'"; ?>>Home</a>
</li>
<li>
<a href="about.php" <?php if($basename =="about") echo "class='current'"; ?>>About</a>
</li
>

Categories