Simple php navigation includes - php

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';

Related

How to load all images in a folder into HTML

I am looking to load all images from specific folders into my website. Currently, I have to copy and paste this HTML code and change each individual file name. Is there a way that I can dynamically load each image into this same HTML?
<div class="item">
<div class="item__content">
<img src="img/Underwater/1.jpg" alt="img02" />
<h3 class="item__title">Retro irony sartorial <span class="item__date">05/05/2015</span></h3>
<div class="item__details">
<ul>
<li><i class="icon icon-camera"></i><span>Canon PowerShot S95</span></li>
<li><i class="icon icon-focal_length"></i><span>22.5 mm</span></li>
<li><i class="icon icon-aperture"></i><span>ƒ/5.6</span></li>
<li><i class="icon icon-exposure_time"></i><span>1/1000</span></li>
<li><i class="icon icon-iso"></i><span>80</span></li>
</ul>
</div>
</div>
</div>
This while use glob to search for jpgs within your designated file path. Just change the file extension (if needed) and the image folder that you are pulling the images from.
<?php
$directory = "imagepath";
$images = glob($directory . "/*.jpg");
foreach($images as $image)
{
echo "<img src=".$image.">";
}
?>
If you are looking to do it using only plain HTML, that won't be possible.
To load all you images dynamically, you'd have to use server-side code, PHP or Javascript (through Node.js for example).

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.)

Hiding GET parameters in php

I am working on local machine and my URL is:
http://localhost:91/GlobalVision/index.php?mm=1&sm=1
<? $mm=$_GET["mm"];
$sm=$_GET["sm"]; ?>
<ul class="sidebar-menu">
<li class="header">MAIN NAVIGATION</li>
<li class=" <? if($mm==1) echo "active" ?> treeview">
<a href="#">
<i class="fa fa-dashboard"></i> <span>Dashboard</span> <i class="fa fa-angle-left pull-right"></i>
</a>
<ul class="treeview-menu">
<li <? if($mm==1 && $sm==1) echo "class=\"active\"" ?>><i class="fa fa-circle-o"></i> Dashboard1</li>
<li <? if($mm==1 && $sm==2) echo "class=\"active\"" ?>><i class="fa fa-circle-o"></i> index2</li>
</ul>
</li>
</ul>
i have created this file as separate menu file and included in other php files.
i wanted url as:
http://localhost:91/GlobalSDK/index
I tried writing:
^([a-zA-Z]+)/$ index.php?mm=$1&sm=$2 [L]
this rules in .htaccess file but it gives me error.
What should be the rule to achieve this. Even when index page get change it should display only page name not parameters. What should be the rule for this?
Try these methods
1) Use a form and POST the information.
2) Use session variables to carry information from page to page.
3) Use "encoded" or non-sensical information in the QueryString in
place of the real data.

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.

get file name for url 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"

Categories