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))
Related
I'm having trouble getting a PHP script to work. It doesn't seem to be working accurately. I'm trying to make a script to only show HTML or Javascript on the homepage only. This script will be used for multiple things. I did google this, but most answers are for WP. This is vanilla PHP.
Here's what I used:
<!-- Add Only On The Homepage Or Else -->
<?php
$currentpage = $_SERVER['REQUEST_URI'];
if($currentpage=="/" || $currentpage=="/index.php" || $currentpage=="" ) {
echo '
<header id="main-header" class="home">
';
}
else {
echo '
<header id="main-header">
';
}
?>
The results are that it shows the content of else, even though it's the homepage. I tested it on "/" and "/index.php".
I commonly use this snippet:
index.php
if (preg_match('/^m\.[^\/]+(\/(\?.*|index\.html(\?.*)?)?)?$/i', $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'])) {
readfile('index.html');
} else {
// Your framework bootstrapper or your own logic
}
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 } ?>
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
>
My website's pages are (or will) all be identical, except for the content of the #main div. So I was thinking in the <head> I could do <?php $page = "home.html"?>, where home.html is the bare contents (not a full html page - no <html>, <head> etc.) of the main div for the home page. So then inside <div id="main"> and </div> I could have <?php include($page);?>. Then the reason I'm asking this question is I want to change $page when a link is clicked. How can I do this? Thanks.
You could make the links be like this:
Other Page
Then also in the top of the page where you set page to the value of $_GET['page']
<?php
if (isset($_GET['page']))
{
$page = $_GET['page'] . ".html";
} else {
$page = "home.html";
}
?>
Your link has to be like:
home.php?page=test
You will get the value in your page variable like this:
if(isset($_GET['page']))
$page = $_GET['page'].'.html';
else
$page = 'index.html';
You can do something like:
http://yoursite.com/index.php?page=test.html or http://yoursite.com/index.php?page=test (and append .html later).
For instance:
<?php $page = preg_replace('/[^A-Za-z]/','', $_GET['page']);
$path = 'pages/'.$page.'.html'
if (file_exists($path)){ $output = file_get_contents($path); }
else { header("HTTP/1.0 404 Not Found");
$output = file_get_contents('pages/404.html'); }
?>
(Putting the rest of your file after the php, for the 404 header to work.)
Then, you also can use apache rewrites:
Rewrite Engine On
Rewrite Rule ^([A-Za-z]+)$ index.php?page=$1
Then use links like http://mysite.com/page_name
Then in the main div:
<?php echo $output; ?>