Can a session variable be get and appended to a URL (PHP) - php

I had set and get the current page URL in a variable using session.
$currentURL = Mage::helper('core/url')->getCurrentUrl();
$currentPageSplit = explode('?___',$currentURL);
$currentPageURL = $currentPageSplit[0];
Mage::getSingleton('core/session')->setCurrentPage($currentPageURL);
session_start();
$curr = Mage::getSingleton('core/session')->getCurrentPage();
Now that I want this variable $curr to be appended to another URL used for switching from one page to another.
<a class="desktop" href="<?php echo $curr.'/?switch-view=desktop';?>">View desktop version</a>
I am unable to do this. Can someone tell if this syntax is right or what else could be done.
Thanks in adv

yes possible.
<?php
$sample=$_SESSION['alpha'];
echo 'click';
?> `
Try this.

To use the url in same page where you declare your session variable
<?php
$view = 'switch-view=desktop';
$_SESSION['view'] = $view;
$curr = $_SESSION['view'];
?>
<a class="desktop" href="<?php echo $curr;?>">View desktop version</a>
If you need to use the session in another page
<?php
session_start();
$curr = $_SESSION['view'];
?>
<a class="desktop" href="<?php echo $curr;?>">View desktop version</a>

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.

HTML disable enter in my code

I have this code
<a href="
?page=<?php echo $_GET['page'] ?>
&tab=<?php echo $_GET['tab'] ?>
&subtab=list"
Nazwa</a>
Chrome renders the above code as this:
?page=nurk-edycja-tresci-home &tab=artykuly &subtab=list
With a lot of whitespaces :C I know a cause comming from my coding style but for me this improves readability a lot. Is there any solution to reconcile my style with browsers? ;)
Keeping the white-space server-side
It also doesn't look like your HTML syntax is valid, you're missing a >
When the PHP is preprocessed, it won't return any of the whitecaps in the code so you should be good:
<a href="<?php
echo '?page' . $_GET['page'];
echo '&tab' . $_GET['tab'];
echo '&subtab=list";
?>">Nazwa</a>
You could also put the whitespace in <?php ?> so the server will process it and then return it to the client
<a href="<?php
?>?page=<?=$_GET['page'] ?><?php
?>&tab=<?=$_GET['tab'] ?><?php
?>&subtab=list"
>Nazwa</a>
You can also replace <?php echo with <?=
How I would do it
<a href="<?= '?page=', $_GET['page'], '&tab', $_GET['tab'], '&subtab=list' ?>">
This syntax is very readable and also let's you put newlines:
<a href="<?=
'?page=', $_GET['page'],
'&tab', $_GET['tab'],
'&subtab=list'
?>">
JavaScript?
If you really like your code right now, you can use JavaScript to get rid of the spaces:
(function () {
window.onload = function () {
var elems = document.getElementsByTagName('a'),
i = 0;
for (; i < elems.length; i += 1) {
elems[i].href = (elems[i].href || '').replace(/\s/g, '');
}
}
}());
Even shorter:
window.onload = function () { Array.prototype.forEach.call(document.getElementsByTagName('a'), function (t) { t.href = (t.href || '').replace(/\s/g, '') }) }
ECMAScript2015 (Harmony):
window.onload = () => Array.from(document.querySelectorAll('a[href]')).map(t => (t.href = a.href.replace(/\s/g, ''));
You can solve it by moving the PHP close tag, (and the opening quote), although it still doesn't look very nice:
<a href=
"?page=<?php echo $_GET['page']
?>&tab=<?php echo $_GET['tab']
?>&subtab=list">
Nazwa</a>
But personally, I would choose to build the url earlier, so you just have to output a small variable in your HTML:
<?php
// I know you can use array items in strings too, but I like to split it up.
$page = $_GET['page'];
$tab = $_GET['tab'];
$url = "?page=$page&tab=$tab&subtab=list";
?>
<a href="<?=$url?>">
Nazwa
</a>
[opinion] I think the last way is better. You have just a simple variable to output, which is constructed somewhere else. This way it's also much easier to move the HTML to a separate template. Such template should contain as little logic as possible and concatting variables should not be there. [/opinion]

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