get file name for url php - php

I wanted to know how to get the current file name in PHP to be able to load the same file in a different directory.
index.php
works.php
lang/de/
index.php
works.php
lang/pl/
index.php
works.php
<div id="language">
EN
<a class="lang" href="index.php">DE</a>
<a class="lang" href="../pl/index.php">PL</a>
</div>
The current method is redirecting the lang URL, allways to index.php.
I would love to know how to redirect from works.php to lang/de/works.php without redirecting to lang/de/index.php
Sorry if i'm getting you confused

I think this is what you want:
<?php
$page = basename($_SERVER['PHP_SELF']);
?>
<div id="language">
EN
<a class="lang" href="<?php echo $page;?>">DE</a>
<a class="lang" href="../pl/<?php echo $page;?>">PL</a>
</div>
You would save the current file name in the $page variable. Then echo it out and replace index.php in your links.

You should really google this stuff.
<?php basename($_SERVER['PHP_SELF']); ?>
GOOGLE
Now that you added some code, here ya go.
<div id="language">
EN
<a class="lang" href="<?php echo basename($_SERVER['PHP_SELF']); ?>">DE</a>
<a class="lang" href="../pl/<?php echo basename($_SERVER['PHP_SELF']); ?>">PL</a>
</div>
FYI, basename($_SERVER['PHP_SELF']); get's the name of the current file and returns just that. In other words, it will return "index.php" or "works.php"

Related

Change logo destination URL for only one page

How do you change the logo to link to the home URL for all pages except one? I want one page to link to another page when the logo is clicked.
Here is the PHP code for the logo:
<div class="section-boxed section-header">
<?php do_action('pexeto_before_header'); ?>
<div id="logo-container">
<?php
$logo_image = pexeto_option('retina_logo_image') ? pexeto_option('retina_logo_image') : pexeto_option('logo_image');
if(empty($logo_image)){
$logo_image=get_template_directory_uri().'/images/logo#2x.png';
}
?>
<img src="<?php echo $logo_image; ?>" alt="<?php esc_attr(bloginfo('name')); ?>" />
</div>
What about creating a second section of PHP - with a slightly different DIV ID that is called when that paticular page is loaded, rather than this one which is called on all the other pages,
Copy, paste, change DIV ID <div id="logo-container2">, change link address.
In HTML - on the single page that takes them elsewhere - call
<div id="logo-container2">
Would that work?
Try to use template_tag is_page as condition
<div class="section-boxed section-header">
<?php do_action('pexeto_before_header'); ?>
<div id="logo-container">
<?php
$logo_image = pexeto_option('retina_logo_image') ? pexeto_option('retina_logo_image') : pexeto_option('logo_image');
if(empty($logo_image)){
$logo_image=get_template_directory_uri().'/images/logo#2x.png';
}
// Default logo url to home
$logo_url = esc_url(home_url('/');
// if is page about or id 5 anything inside is_page()
if(is_page('about') $logo_url = esc_url(home_url('about');
?>
<img src="<?php echo $logo_image; ?>" alt="<?php esc_attr(bloginfo('name')); ?>" />
</div>
I believe you should be able to use the get_permalink method to check which page you are on, and use an if statement to tell it what the href should be.
<a href="<?= (get_permalink() == '/my-page') ? esc_url(home_url('/go-to-page')) : esc_url(home_url('/')); ?>">
Haven't tested this, but it should work.

Image doesnt show up in php

In this little snippet of code ,i show how i take the "foto1" column of my database and transfer the value of it to a variable in c# named $foto.
The $foto contains the path of the image corresponding to the product that is showing up. Ive tried to copy and paste the path and ditch out the php part and it works. But when i put it in img src it gives me like the broken image thing.And i cant figure out why it does that.
All help is aprecciated .
Have a nice day :)
<div class="row shop_box-top">
<?php
$ligaBD=odbc_connect('basededadospap','','');
$sql="SELECT * FROM produto WHERE nome_produto LIKE '%ADIDAS%'";
$resultado=odbc_exec($ligaBD,$sql);
?>
<div class="row shop_box-top">
<?php
while (odbc_fetch_row($resultado))
{
$nome = odbc_result($resultado,2);
$preco= odbc_result($resultado,4);
$foto = odbc_result($resultado,9);
?>
<div class="col-md-3 shop_box"><a href="stansmithflatwhite.html">
<img src="<?php echo $foto; ?>" class="img-responsive" alt=""/>
<span class="new-box">
<span class="new-label">Novo</span>
</span>
<div class="shop_desc">
<h3><?php echo $nome; ?></h3>
<span class="actual"><?php echo $preco; ?></span><br>
</div>
</a></div>
<?php }?>
depends of what path contains the $foto var. If is the absolute path, you have to retrive the relative path.
Try also to append an / or an http[s] in front of the path
<img src="/<?php echo $foto;?>">
So it would be : //path/to/photo
As I can see it in your comment, your image paths contain spaces, so a possible solution can be to use urlencode() before echoing them.
Try passing full path to img tag like http://localhost/xyz/images/Cal�ado/Adidas/Homem/Stan Smith/ADIDAS STAN SMITH - RED/ch-adidas-stan-smith-red-5.jpg.
Replace "localhost/xyz" with your website directory path.

Value of PHP variables not transferring with include statement

OK, I know that I am being stupid about this, but I can't seem to figure out how I am being so.
I have variables for social media links in a separate .php file so that my client can change them without having to see too much code.
I include that .php file in my normal code where I want the links to be. But, for some reason the value of the variable is not being transmitted. I even tried to make a "global" variable. Here is my code.
File: socialMediaURLs.php
<?php
global $facebook = 'https://www.facebook.com/MenCoachingMen';
$googlePlus = 'https://plus.google.com/104275309033865331192/posts';
$twitter = 'https://twitter.com/MenCoachingMen';
$rss = 'http://mencoachingmen.org/category/podcast/feed/';
$vimeo = 'https://vimeo.com/mencoachingmen';
$youtube = 'https://www.youtube.com/channel/UCy_Pth5x-O7rcX9nMx1e8qw';
?>
File: socialLinks.php
<?php include './socialMediaURLs.php'; ?>
<div class="social_links_wrapper">
<a href="<?php echo $facebook;?>">
<div class="sl_facebook">
<i class="fa fa-facebook fa-5x"></i>
</div>
</a>
<a href="<?php echo $googlePlus;?>">
<div class="sl_googlePlus">
<i class="fa fa-google-plus fa-5x"></i>
</div>
</a>
<a href="<?php echo $twitter;?>">
<div class="sl_twitter">
<i class="fa fa-twitter fa-5x"></i>
</div>
</a>
<a href="<?php echo $rss;?>">
<div class="sl_rss">
<i class="fa fa-rss fa-5x"></i>
</div>
</a>
<a href="<?php echo $vimeo;?>">
<div class="sl_vimeo">
<i class="fa fa-vimeo-square fa-5x"></i>
</div>
</a>
<a href="<?php echo $youtube;?>">
<div class="sl_youtube">
<i class="fa fa-youtube fa-5x"></i>
</div>
</a>
</div>
I ran a test like so:
<?php
if($facebook){
echo $facebook;
}else{
echo 'Facebook NULL';
}
?>
I put this after the include statement and before the rest of the code. And it prints out 'Facebook NULL'. So, I know that the value is not being transmitted. Now, if I put in the included .php file (where the variables are stored) a echo "Hello World!"; line, "Hello World!" does print out on the screen. So, I know that the file is being included correctly (ie the path is correct).
I then placed this code after the 'include' and before the rest of the code:
$facebook = 'https://www.facebook.com/MenCoachingMen';
When I do that, the URL is included within the page. So, I know that my php statements within the code is correct. That means that it has to be a transmission of the value of the variable after including it. Please help. I know this must be a stupid mistake somewhere. Thank you.
A global variable must be used inside a function.
function name(){
global $globalvar = "some info";
}
to pass a variable OUT of it and into the script. You can't pass variables to other scripts using global variables. Consider using $_SESSION['name'] variables to pass variables from script to script.
http://php.net/manual/en/book.session.php
<?php include 'socialMediaURLs.php'; ?> <--- top main public_html/www
<?php include 'directory1/socialMediaURLs.php'; ?> <-- one folder deep
<?php include 'directory1/directory2/socialMediaURLS.php'; ?> <-- two folders deep
//calling a file FROM two folders deep TO A FILE IN the root folder!
<?php include('../../socialMediaURLS.php'; ?> <--- main index public_html/www
//ETC......
<?php include('../../directory1/socialMediaURLS.php'; ?>
I hope this helps. Think of it like DIR and CD commands.

Append a GET variable on to a page with another GET variable

I have a profile.php page with a variable uid?=12 and i have a language bar on that is on a separate page called lang.inc.php:
Profile Page:
<div class="langbar">
<?php include 'inc/lang.inc.php'; ?>
</div>
lang.inc.php
<center>
<a class="flag_USA" title="English" href="<?php echo basename($_SERVER['PHP_SELF']); ?>"><img src="css/images/us.png"></a> <span> </span>
<a class="flag_France" title="French" href="<?php echo basename($_SERVER['PHP_SELF']); ?>?lang=fr"><img src="css/images/fr.png"></a> <span> </span>
<a class="flag_dutch" title="Dutch" href="<?php echo basename($_SERVER['PHP_SELF']); ?>?lang=de"><img src="css/images/de.png"></a> <span> </span>
<a class="flag_Italy" title="Italian" href="<?php echo basename($_SERVER['PHP_SELF']); ?>?lang=it"><img src="css/images/it.png"></a> <span> </span>
<a class="flag_Italy" title="" href="<?php echo basename($_SERVER['PHP_SELF']); ?>?lang=es"><img src="css/images/sp.png"></a> <span> </span>
<?php echo $_SERVER['PHP_SELF']; ?>
</center>
As you can see im trying to append the ?lang=fr for example to the end of the profile.php?uid=12 so it looks like this profile.php?uid=12&lang=fr but all it's doing is profile.php?lang=fr.
I'm pretty sure $_SERVER['PHP_SELF'] does not include get variables, just the file path, so you'd have to add the uid to the href yourself.
<?php
$basename = basename($_SERVER['PHP_SELF']); // Lets store the basename
$basename_with_uid = $basename . "?uid=" . $_GET['uid']; // and append the uid from the URL. Make sure to do some validation if $_GET['uid'] exists.
?>
<a class="flag_France" title="French" href="<?php echo $basename_with_uid; ?>&lang=fr"><img src="css/images/fr.png"></a>
REQUEST_URI is probably what you are after:
<a class="flag_France" title="French" href="<?php echo $_SERVER['REQUEST_URI']; ?>&lang=fr"><img src="css/images/fr.png"></a>
If your page was profile.php?uid=12, the output should be:
profile.php?uid=12&lang=fr
Ref: PHP $_SERVER variables
edit You may still need to add basename() around REQUEST_URI, depending on what you are doing

PHP CMS not working after change of directory

I built(using a tutorial) my CMS in a sub directory on my server and it worked fine. Admin, Archive, full display and synopsis.
I then moved all of this into my main directory, changed the index.php filename to another and used 'include' to display it within a DIV of another page.
It displays the main index.php element of the page but doesn't return articles when they are clicked.
I have searched through the files to change any instances of 'index.php' in the code but there are none.
All admin functions working perfectly and the output URL onclick of blog entry is the same as the previous files in the previous directory. Of course without formatting but that is no problem.
In what part did I make a mistake? I would like to avoid using an iframe to resolve this issue.
The code that the index.php file is including. (homepage.php)
<?php include "templates/include/header.php" ?>
<ul id="headlines">
<?php foreach ( $results['articles'] as $article ) { ?>
<li>
<h2>
<span class="pubDate"><?php echo date('j F', $article->publicationDate)?></span><?php echo htmlspecialchars( $article->title )?>
</h2>
<p class="summary">
<?php if ( $imagePath = $article->getImagePath( IMG_TYPE_THUMB ) ) { ?>
<img class="articleImageThumb" src="<?php echo $imagePath?>" alt="Article Thumbnail" />
<?php } ?>
<?php echo htmlspecialchars( $article->summary )?>
</p>
</li>
<?php } ?>
</ul>
<p>Archivo de Articulos</p>
<?php include "templates/include/footer.php" ?>

Categories