PHP creating url with parameters - php

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.

Related

Why does a browser concatenate the href when I set it dynamically?

I am working on a language switcher, and I use PHP to change the URL from a language to another. I want the language switching button (i.e. link) to change its href attribute when the page loads. Here's my code:
<?php
$actual_link = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
if (strpos($actual_link, 'fr') !== false) {
$actual_lang = 'fr';
$new_link = str_replace('/fr/','/en/',$actual_link);
}
else {
$actual_lang = 'en';
$new_link = str_replace('/en/','/fr/',$actual_link);
}
echo "
<div class='top-bar'>
<a class='language-selector' id='language-switch-btn' href='#'>Language</a>
</div>
<script type='text/javascript'>
document.getElementById('language-switch-btn').href = '".$new_link."';
</script>
";
?>
However, my problem is that the JS code concatenates the $new_link to the $actual_link, resulting in the href attribute like this: localhost/website.com/fr/localhost/website.com/en/ when I would want it to be only localhost/website.com/en/
Any idea why it doesn't replace '#' by $newlink ? Thank you
$_SERVER['HTTP_HOST'] doesn't include the protocol, and without the protocol links are relative.
You can prefix the href with // to allow any http protocol (either http or https), but as mentioned in the comment you also don't need JS to do this.
echo "
<div class='top-bar'>
<a class='language-selector'
id='language-switch-btn'
href='//".$new_link."'>Language</a>
</div>
";

Base_url not working?

Just a simple quetsion?
How to make CI
<?= base_url();?>
working with 'String'
The full codes as follows:
<?php if($_SESSION['admin'] == 1||$_SESSION['admin']== 0){
echo "<a class='btn-common' href='<?= base_url();?>usr/logout'>LOGOUT</a>";
}
else{
echo "<a class='btn-common2'></a>";
}
Base_url is not woking
<?base_url();?>
You can't use a php method inside this string, you need to concatenate the string with the result instead:
echo "<a class='btn-common' href='" . base_url() . "usr/logout'>LOGOUT</a>";
Also like this. pass your path to base_url() as argument:
<a class='btn-common' href='<?php echo base_url("usr/logout");?>'>LOGOUT</a>;
Don't forget to load url helper.using
$this->load->helper('url');
You need to concatenate the string.
echo "<a class='btn-common' href='".site_url('usr/logout')."'>LOGOUT</a>";
I would recommend to use site_url with your controller in parameter (which will use your config file to build url automatically).
With Codeigniter, you can use $this->session->userdata('admin') for retrieving session data too.
if($this->session->userdata('admin')===1 || $this->session->userdata('admin')===0){
echo "<a class='btn-common' href='".site_url('usr/logout')."'>LOGOUT</a>";
}else{
echo "<a class='btn-common2'></a>";
}
You should be using codeigniter links
<?php echo anchor("usr/logout",'Logout', 'title="Logout"');?>
You dont need to use base url, Codeigniter enters that for you

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>

display file name in while loop without url

I want to display only the file name without displaying the url.
I want to replace the file name instead of 'document '.
while($fet=mysql_fetch_assoc($sql1))
{
$i=$i+1;
$next=$fet['f_name'];
echo '<h4><a class="astext" href="'.$next.'" title="'.$next.'" target="_blank" download>Document'.$i.'</a></h4>';
}
if I replace the word 'document' to $next it shows full url as http://www.sample/txt/1/sample.doc I need to display sample.doc.
echo '<h4><a class="astext" href="'.$next.'" title="'.$next.'" target="_blank" download>"'.$next.'"</a></h4>';
There are two options.
Assume you have $next=http://www.sample/txt/1/sample.doc
Option 1:
This works if you think http://www.sample/txt/1/ is same to all folders.
ltrim($next,"http://www.sample/txt/1/");
Option 2:
use basename($next)
This will extract the sample.doc
<?php
$link = "http://www.sample/txt/1/sample.doc";
$linkArray = explode('/', $link);
echo $linkArray[count($linkArray)-1];
?>
Magesh Kumaar also provide good one
<?php
$link = "http://www.sample/txt/1/sample.doc";
echo basename($link);
?>
I included $next1 = basename($next);
while($fet=mysql_fetch_assoc($sql1))
{
$i=$i+1;
$next=$fet['f_name'];
$next1 = basename($next);
echo '<h4><a class="astext" href="'.$next.'" title="'.$next.'" target="_blank" download>'.$next1.'</a></h4>';
}
Its working now.
<?php
$url = "http://www.sample/txt/1/sample.doc";
echo strstr($url,"sample.");
?>
For More Details

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