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
>
Related
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.
I am trying to load my content dynamically via PHP. Here is my code, which for some reason doesnt work. Can you give me some hint, what am I doing wrong?
The goal is :
Click on Hypertext in nav
Get dynamically content from file page-statistiky in folder /podstranky
Code:
<nav>
<ul>
<li>statistiky</li>
</ul>
</nav>
<?php
if (isset($_GET['stranka']))
$stranka = $_GET['stranka'];
else
$stranka = 'page-statistiky';
if (preg_match('/^[a-z0-9]+$/', $stranka)) {
$vlozeno = include('podstranky/' . $stranka . '.php');
if (!$vlozeno)
echo('Podstránka nenalezena');
} else
echo('Neplatný parametr.');?>
You regex doesn't include provision for the dash i.e. -'
Change this:
if (preg_match('/^[a-z0-9]+$/', $stranka))`
to
if (preg_match('/^[a-z0-9\-]+$/', $stranka))
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>
I'm finishing up my new portfolio and am trying to figure out a way to get a 'next' and 'prev' URLs for the child pages of the parent, which is called 'Work'.
The reason I want URLs is because the actual clickable link is an SVG at larger viewport sizes and I want to maintain all of my css access to the SVGs.
I'm including an image of how the navigation looks (looks like an aside, but it's actually a outside of the ).
I know that pages in worpress 'aren't meant to be paginated' but this is the same thing as creating a new post type and using the pagination there, except im doing some other things where I want access to templates. I tried using this plugin:
http://binarym.com/2009/next-page-not-next-post/
Which works, but I can't get my SVGs in there as opposed to text. If anyone has a way to replace the text in those strings with my SVG paths, that's an acceptable fix too. Here's a code snippet of what that looks like using that plugin:
<nav role="navigation" class="project-pagination">
<a href="/work">
<?php include (TEMPLATEPATH . '/images/_svgs/nav_gallery.svg'); ?>
</a>
<?php
$nextPage = next_page_not_post('Next Page', 'true', 'sort_column=post_date&sort_order=desc');
$prevPage = previous_page_not_post('Previous Page', 'true', 'sort_column=post_date&sort_order=desc');
if (!empty($nextPage) || !empty($prevPage)) {
if (!empty($nextPage)) echo $nextPage;
if (!empty($prevPage)) echo $prevPage;
}
?>
</nav>
Thanks all!
I found the answer here: Magic Town
<nav role="navigation" class="project-pagination">
<a href="/work" aria-label="View All Projects" alt="View All Projects">
<?php include (TEMPLATEPATH . '/images/_svgs/nav_gallery.svg'); ?><span>View All Projects</span>
</a>
<?php
$pagelist = get_pages("child_of=".$post->post_parent."&parent=".$post->post_parent."&sort_column=menu_order&sort_order=asc");
$pages = array();
foreach ($pagelist as $page) {
$pages[] += $page->ID;
}
$current = array_search($post->ID, $pages);
$prevID = $pages[$current-1];
$nextID = $pages[$current+1];
?>
<?php if (!empty($nextID)) { ?>
<?php include (TEMPLATEPATH . '/images/_svgs/nav_next.svg'); ?><span>Next Project</span>
<?php }
if (!empty($prevID)) { ?>
<?php include (TEMPLATEPATH . '/images/_svgs/nav_prev.svg'); ?><span>Previous Project</span>
<?php } ?>
I have some problems to put a URL-function within an if-statement properly. The following method works fine outside of my if-statement and the newly created link refers to this URL: "h**p://www.blog.com/?location=Bern&date=1"
<?php $rasp = $_GET["location"]; ?>
<a href="<?php echo preg_replace("/&date=(0|1|2|3)/", "", $_SERVER[\REQUEST_URI\])."&date=0"; ?> " >Heutel</a>
If-Statement works also just fine when I put simply an URL (e.g. https://www.google.com/) in between the quotation marks, then if I put the URL-function above in there, the newly created link refers to this: "h**p://www.blog.com/%3C?php%20echo%20preg_replace%28". Actually it should refer to the URL above "h**p://www.blog.com/?location=Bern&date=1"
URL-Function within if-statement:
<?php $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (false !== strpos($url,'date=1')) { echo
'<?php $rasp = $_GET["location"]; ?>
<a href="<?php echo preg_replace("/&date=(0|1|2|3)/", "", $_SERVER[\REQUEST_URI\])."&date=0"; ?> " >Heutel</a> ' ;
} else {
echo 'No cars.';
} ?>
Any ideas?
Solution thanks to IMSop:
<?php
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (false !== strpos($url,'date=1')) {
?>
<a href="<?php echo preg_replace("/&date=(0|1|2|3)/", "", $_SERVER['REQUEST_URI'])."&date=0"; ?> " >Heutel</a>
<?php
}
else {
echo 'No cars.';
}
This sequence makes no sense:
echo '<a href="<?php echo ...
A quoted string and direct output outside the <?php ... ?> markers are completely different things. The ' starts a string, which will continue until you put another '; the <?php would start a block of PHP code if you weren't in one, but you already are - if you weren't the echo wouldn't mean anything.
To join multiple strings together, you can use the . ("concatenation") operator:
echo '>Heutel';
Alternatively, drop out of PHP mode like you were before, but with the if in place:
<?php
// In PHP mode...
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (false !== strpos($url,'date=1')) {
// Now leaving PHP mode, but still inside the if condition...
?>
<a href="<?php echo preg_replace("/&date=(0|1|2|3)/", "", $_SERVER['REQUEST_URI'])."&date=0"; ?> " >Heutel</a>
<?php
// Re-enter PHP mode to close off the if statement
}
There's even an alternative syntax for control syntax which some people prefer to use in cases like this:
<?php
if ($some_condition) :
?>
your output here
<?php
endif;
?>
which is exactly the same as
<?php
if ($some_condition) {
?>
your output here
<?php
}
?>