Value of PHP variables not transferring with include statement - php

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.

Related

How to call one line of text from a php file to use in another?

I'm trying to use the title of an article in one place and be able to place it anywhere by calling it from the php file.
I tried to include the code from news.php and echo it in the footer, but it doesn't seem to work. What am I doing wrong?
news.php
<div class="entry-title">
<h4>
<a href="news/news-article-5-21-2019">
<?php $str1 = "NEWS TITLE" ?></a>
</h4>
</div>
footer.php
<div class="entry-title">
<h4>
<a href="news/news-article-5-21-2019">
<?php include
'news.php';
echo $str1;
?></a>
</h4>
</div>
I expected footer.php to output the "NEWS TITLE" from news.php, but didn't get anything.

Anchor Tag automatically calling href Without Click in Codeiginitor

Here in My View:
<div class="dropdown">
<button class="btn dropdown-toggle" type="button" data- toggle="dropdown">Dropdown Example
<span class="caret"></span></button>
<ul class="dropdown-menu">
<?php
foreach($site as $sites)
{
echo '<li >"'.$sites->site_title.'" </li>';
}
?>
</ul>
</div>
<?php
}
?>
i want to redirect when user click on $sites->site_title
but how it working is it automatically redirects to url
enter code here"<li><a href='shop/viewSiteId?id=".$sites->site_id."'>".$sites->site_title."</a></li>";
and get that id by using GET method
A redirect is the programmatic way to send a browser to a URL. In other words, a call to redirect is like clicking a link. They are not intended to work together in the way you have tried.
<?php
foreach($site as $sites) : ?>
<li>
<a href='<?= base_url("shop/viewSiteId?={$sites->site_id}"); ?>'><?=$sites->site_title; ?></a>
</li>
<?php endforeach;
If you are not familiar with the syntax, know that <?= is the shortcut way to write <?php echo
I've also used PHP Alternative Syntax for Control Structures and dropped in and out of the PHP processor a bunch of times. For me, that is the easier way to read and write this kind of thing. (Your mileage may vary.)

How to redirect to a particular block of html page?

I'm working on a simple project which has a requirement to show a particular block of html by passing an div id to that url.
I have tried so many solutions, but none of them worked for me.
Here is my code.
<div class="row">
<a onclick="redirect_tooltip();">
<i class="fa fa-question-circle" aria-hidden="true"></i>
</a>
</div>
Whenever I click the icon it calls a Javascript function, through that function I tried to pass a url with another page division id
function redirect_tooltip()
{
var source_type = $("#_src_type").val();
if(source_type == "source")
{
window.location.replace(base_url+'/help/tooltip#rtmp_4');
//I have already difined the base url
}
}
My html code:
<div id="rtmp_4">
<h4>Some Title</h4>
<h5> <?php echo trim($tooltip_rtmp_server[1]);?></h5>
<p> <?php echo trim($tooltip_rtmp_server[3]);?> </p>
<h5> <?php echo trim($tooltip_rtmp_stream_name[1]);?></h5>
<p> <?php echo trim($tooltip_rtmp_stream_name[3]);?> </p>
</div>
I need to display only the rtmp block whenever the url executes. Please give me a suggestions like where I am doing the mistake.

Simple php navigation includes

I'm trying to make a simple php page using includes and I'm finding it hard to navigate the urls using ../ to locate the right path to the folder.
Is there an easy way to construct a simple php navigation. Only doing it locally without using mysql and etc.
<a class="list-group-item" href="index.php">Overview <i class="fa fa-tachometer fa-fw"></i></a>
<a class="list-group-item" href="ticket/tickets.php">Tickets <i class="fa fa-ticket fa-fw"></i></a>
<? php
$indexURL = ('project/*');
?>
<div id="sidebar" class="sample">
<div class="search">
<input type="search" placeholder="Search..."> <i class="fa fa-search fa-2x"></i>
</div>
<div class="list-group">
<a class="list-group-item" href="<?php echo '$indexURL' ?>/index.php">Overview <i class="fa fa-tachometer fa-fw"></i></a>
This line will write out $indexURL, literally. If you want the value of $indexURL, remove the single quotes.
<?php echo '$indexURL' ?>
To get absolute path to your document root use:
$_SERVER['DOCUMENT_ROOT'];
This should return c:\wamp\www
Or /opt/www
Or your location to apache document root.
To test this simply type:
echo $_SERVER['DOCUMENT_ROOT'];
To get the folder location use
echo basename(__DIR__);
will return the current directory name only
echo basename(__FILE__);
will return the current file name only
echo basename(dirname(__FILE__));
Will return the location of the php file that is running the code.
Lets take 3 files for example
home.php
test/pics.php
test/images/editor.php
in your pics.php you need to use functions in editor.php, so you use
Pics.php:
require_once dirname(__FILE__) . '/images/editor.php';
Home.php
require_once 'test/pics.php';

Avoid showing icons that don't contain a value

I've already been searching for a couple of hours for a solution. What I want to do is make icons that don't got a value (in the php) not show up.
Here's an image of what I currently have.
So for instance if only twitter and facebook got values, they should only appear on the screen.
Below is the code, and I hope someone got a solution for this.
Cheers!
<ul class="social-buttons">
<div class="wrapping">
<a class="social" href="http://twitter.com/<?php echo $profile_data['twitter']; ?>" target="_blank"><li class="twitter"></li></a>
<a class="social" href="http://facebook.com/<?php echo $profile_data['facebook']; ?>" target="_blank"><li class="facebook"></li></a>
<a class="social" href="skype:<?php echo $profile_data['skype']; ?>?chat"><li class="skype"></li></a>
<a class="social" href="http://instagram.com/<?php echo $profile_data['instagram']; ?>"><li class="instagram"></li></a>
<a class="social" href="http://dribbble.com/<?php echo $profile_data['dribbble']; ?>"><li class="dribbble"></li></a>
<a class="social" href="http://linkedin.com/in/<?php echo $profile_data['linkedin']; ?>"><li class="linkedin"></li></a>
</div>
</ul>
You need to use the if statement with !empty(). The !empty() checks if a variable is NOT empty. Than proceed with the code. Like the example given here:
<?php
if(!empty($profile_data['twitter'])){
?>
<a class="social" href="http://twitter.com/<?php echo $profile_data['twitter']; ?>" target="_blank"><li class="twitter"></li></a>
<?php
}
?>
If the variable is empty, it wont give the outputed code, in your case the <a> with the icon.
i think you can do like:
<ul class="social-buttons">
<div class="wrapping">
<?php if $profile_data['twitter'] {?>
<a class="social" href="http://twitter.com/<?php echo $profile_data['twitter']; ?>" target="_blank"><li class="twitter"></li></a>
<?php } ?>
....
</ul>
Imo, a probably better way to do this is to perform a pre-processing of the data i.e. $profile_data, before you use it in your view so the view would no longer need to handle the processing logic. After which, the view can output your links by using a more concise construct e.g. for loop, that does not use any conditional branching.

Categories