I need to include some js, css type items in specific pages.
So far I've done the following:
define("DIR_ADMIN", "admin");
function fusion($location, $page, $type) {
$dirAdmin = DIRECTORY_SEPARATOR.DIR_ADMIN;
$change = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $location);
$changed = ucfirst($change);
$result = $dirAdmin.$changed;
if($type=='js'){
echo "<script src='$result'></script>\n";
} elseif($type=='css'){
echo "<link rel='stylesheet' href='$result' />";
} elseif($type=='favicon'){
echo "<link rel='shortcut icon' href='$result' />";
} elseif($type=='logo'){
echo "<img src='$result' alt='logo'>";
} else {
echo $result;
}
}
In HTML I called the function like this:
<?php fusion("/assets/vendors/js/vendor.bundle.base.js", "clients", "js"); ?>
First is the file URL, then the page, and finally the file type.
What I'm not able to come up with is the part that receives the value $page.
I created a routing system in php, so all files are accessed like this: index.php?page=clients, except in the case of the homepage which is just index.php.
I need that when the value of $page is set for example with clients it should load all items that have this same when the clients route is accessed.
In some cases the route may have another word attached, for example: index.php?page=clients-add, in which case it should check if there is only the word clients in $_GET.
And if I set the value of $page to all, it should display on all pages. How can I do this?
Within your fusion function you need to pick up which page you're on and then including the displaying of content after that.
Use this snippet to help dictate the page you're on.
$curr_page = "home";
if ( isset( $_GET["page"] ) ) {
$curr_page = $_GET["page"];
}
if ( $page == "all" || $page == $curr_page ) {
//echo items here
}
Related
I would like to make navbar, in which background colour of one element would change if user would be on that subpage.
Code for checking if url contains chosen string:
<?php
$url = $_SERVER['REQUEST_URI'];
//echo ($url);
if (strpos($url, 'index.php') == true) {
//echo 'Current page contains index.php';
$atm = "#2275A8";
} else {
$atm = "#00427A";
}
?>
This is part from same file saved as .php file
<?php
header("Content-type: text/css");
?>
.ico1 {
background: <?php echo $atm; ?>;
}
If i put my "ckecking part" od code on index, it is working nicely; returning true or false, but for some reason it's not passing parameter into style file.
So now i put "checking part" into style file but, no mater on which subpage i am, its returning same result (always true even if 'index' is not part of url).
Any idea on how to deal with this? :)
Define classes with style you want in style.css file and change the class of element you want to change style of dynamically in php file like in the example below.
style.css
.ico1 {
background: color1;
}
.ico2 {
background: color2;
}
index.php (or the file containing navbar you want to change)
<?php
$url = $_SERVER['REQUEST_URI'];
if (strpos($url, 'index.php') == true) {
//echo 'Current page contains index.php';
$atm = "ico1";
} else {
$atm = "ico2";
}
?>
...
<div class="<?php echo $atm;?>"> // navbar
Why don't you have the two css classes and make a conditional in the element you want the class be applied:
CSS
.ico1-0 {
background: #2275A8;
}
.ico1-1 {
background: #00427A;
}
PHP
[...]
<div class="<?php echo (strpos($url, 'index.php') === true) ? 'ico1-0' : 'ico1-1'; ?>"></div>
[...]
strpos will never return true. Either it will return false or it return integer. So try like below.
if (strpos($url, 'index.php') !== false)
I'm coding a menu bar for a website in php. Because I don't want to have to edit it multiple times on the half a dozen or so pages I'll have I've decided to put it in it's own separate header.php file and just include_once(header.php) in the various pages.
My problem is that the menu is going to be slightly different depending on which page it's included in. Right now I'm dealing with it by having the following in my header.php file with $PageTitle being defined in the individual pages:
if ($PageTitle == "Home"){
echo '<li class="active">Home</li>';
}
else{
echo '<li>Home</li>';
}
if ($PageTitle == "About"){
echo '<li class="active">About</li>';
}
else{
echo '<li>About</li>';
}
...
The active class simply highlights the menu of the current page (Like the menu bar on the top of StackOverflow). It works fine but I'm curious if there is a better perhaps more efficient way to doing this. Thanks guys.
Try this:
//list of menu headers
$headers = new array();
//populate the array with your headers here ...
foreach($headers as $val)
{
if( $PageTitle == $val )
echo '<li class="active">'.$val.'</li>';
else
echo '<li>'.$val.'</li>';
}
for current class you can also use jquery if you want to:
$(function(){
var path = location.href;
if ( path )
$('.side_menu a[href="' + path + '"]').attr('class', 'current');
});
i have a large number of files with several id's in each file. For example file1.php contains a number of paragraphs, each paragraph has a unique id. (id="1",id="2",id="3" etc...) I would like the ability to create a link to a page (page A.php) and pass the location of one of these id's in the url of the link to display in a php include on page A.php The result that i'm looking for is to have the entire file (file1.php) show up inside of page A.php with the specific id that is passed in the url being highlighted. Is this possible? or do I need to use Java Script and an iframe?
Here is what I ended up with:
The url: http://mydomain/thispage.php?xul=http://mydomain.com/folder1/folder2/file.php&id=Abc150:176
The code:
Stylesheet .vrsehilite{styling}
<script type="text/javascript">var x = <?php echo json_encode($_GET["id"]); ?>;</script>
<?php
$invdmn = "<h2>Error: Invalid Domain</h2>";
$filnf = "<h2>Error: File Not Found</h2>";
$pthinv = "<h2>Error: The Path is invalid</h2>";
$idinv = "<h2>Error: The ID is invalid</h2>";
$oops = "<br/><h2>Oops! Something went wrong.<br/><br/>Please click the back button or use the menu to go to a new page.</h2>";
$testdomain = substr_compare ($_GET['xul'],"http://mydomain.com",0,20,FALSE); //make sure the domain name is correct
if ($testdomain == 0) {
$flurl = $_GET['xul'];
} else {
echo $invdmn . " " . $oops;
}
$flurl_headers = #get_headers ($flurl);
if ($flurl_headers[0] == 'HTTP/1.1 404 Not Found') {
echo $filnf . " " . $oops; //Make sure the file exist
} else {
$surl = str_replace (".com/",".com/s/",$flurl);
} //add some characters to url at point to explode
list($url1, $path) = explode ("/s/",$surl); //explode into array of 2 [0]url to domain [1] path
$testpath = substr_compare ($path,"file1/file2/",0,10,FALSE); //make sure the path is correct
if ($testpath == "0") {
$aid = preg_match ("/^[A-Z][a-z]{2}(?:[1-9][0-9]?|1[0-4][0-9]|150):(?:[1-9][0-9]?|1[0-6][0-9]|17[0-6])$/", $_GET['id']);
} else { //make sure the id is valid
echo $pthinv . " " . $oops;
}
if ($aid == 1) {
include($path);
echo "<script type='text/javascript'>";
echo "document.getElementById(x).className = 'vrsehilite';";
echo "document.getElementById(x).scrollIntoView();";
echo "window.scrollBy(0,-100);";
echo "</script>";
} else {
echo $idinv . " " . $oops;
}
?>
Never ever include arbitrary files submitted by the user. Instead, you should only include files from a pre-defined set of your choice. Perhaps something like this:
PHP
$files = array ('file1.php', 'file2.php', 'view.php', 'edit.php');
$id = (int)$_GET['id'];
if (isset ($files[$id])) {
include $files[$id];
} else {
/* Error */
}
Or you could use a regular expression to accept only certain filenames, in this case 1 or more lower case letters followed by 0 or more digits.
$m = array ();
if ( preg_match ('#^http://domain.example/folder1/folder2/([a-z]+[0-9]*\\.php)$#', $m)
&& file_exists ($m[1])) {
include $m[1];
} else {
/* Page not found */
}
You may want to check the return value of include. You may also want to move the folders into the subpattern (...) or use regular expressions for the folder names.
If all you need is to highlight a certain paragraph in a page, you should add a URL fragment that poins to the paragraph's id, and add CSS to style it. Eg:
URL
http://domain.example?id=1#p1
HTML
<p id=p1>This is the target paragraph.
CSS
p:target { /* Style the targeted <p> element */ }
Hey everyone, I am having a problem with a website I'm trying to build; basically I have a class that I call from my header file that loads all of the link and script tags. The link tags show up across all browsers, but the script tags only show up in safari and chrome, they do no show up in firefox or IE.
<script type='text/javascript' src='...'>
now I have tried removing the "<" at the front of the tag just to see what happens, and it will show up as plain text then, but as soon as I put the "<" back it is again MIA.
So here is what's going on in the php. My header.php file calls the cms object function, located in cms.php, and those functions call other functions in my system.php file.
Now, again the link tags work w/out a hitch and I call them the exact same way ... it is just the dumb script tags. When I call the load_js("config"); function in my header.php it will load multiple tags as well. If it was just 1 tag I would put the script tags in the html rather than in the php, but I don't think I can do that when I'm producing multiple tags.
Any help would be great! Thanks in advance as well!
header.php
<?php echo $this->load_css("config"); ?>
<?php echo $this->load_js("config"); ?>
cms.php
function load_js($name){
// ...
return header_script($name.".js");
// ...
}
function load_css($name){
// ...
return header_link($name.".css");
// ...
}
system.php
function header_script(){
// 0 = src
$num = func_num_args();
if($num == 0){
return;// if no arguments, can't successfully build header_script.
}
if($num == 1){
return "<script type='application/javascript' src='".func_get_arg(0)."'></script>\n";
}
}
function header_link(){
$num = func_num_args();
// 0 = rel
// 1 = type
// 2 = href
if($num < 3){
return; // can't successfully build link.
}
if($num == 3){
return "<link rel='".func_get_arg(0)."' type ='".func_get_arg(1)."' href='".func_get_arg(2)."' />\n";
}
}
Firstly, application/javascript is not recognized by some browsers. You should change that to text/javascript
Secondly, as mentioned in comments (Justin Johnson), you should use parameters in your function definition with default values:
function header_script($name = '')
{
if ($name != '') {
return '<script type="text/javascript" src="'.$name.'"</script>';
}
}
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; ?>