So i have a problem, I never did this before.
I have this website http://82.196.14.71/obrigatorionaover/ .
I want to generate ajax webpages but I want permanent links too for the browser history.
This is the body of the first page (releituras.php):
<body>
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/obrigatorionaover/php/nav.php";
include_once($path);
?>
<div id="float">
//this is a connect
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/obrigatorionaover/xxxxxxxxxxxx.php";
include_once($path);
?>
<?php
try {
$stmt = $db->query('SELECT * FROM sessoes ORDER BY id DESC');
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<div style="background-image:url('."http://localhost:8888/obrigatorionaover/".$row["img"].')" class="rect sessao" id=sessao'.$row["id"].'>'.'<h3 class="titulo">'.$row["numero_sessao"].'</h3>'.'<p class="descricao">'.$row["descricao"].'</p>'.'</div>';
}
} catch(PDOException $ex) {
echo "An Error occured!"; //user friendly message
}
?>
</div>
<script src="/obrigatorionaover/js/script.js"></script>
</body>
I clean the div float and insert new content dynamically like this, (sessao.php is similar to the query from releituras.php)
function sessao(data){
$("#float").html("");
$.get("/obrigatorionaover/php/sessao.php?id=1").done(function(data) {
$("#float").append(data);
});
}
The url in the browser doesn't change, it is possible to change the url dynamically without changing the content?
The browser history doesn't change either (I know I can inject it but i don't have valid physic php page). How can I inject it? Should I create physic php pages automatically when inserting information into the database?
Can I have something like virtual php pages? If yes how to do it?
What you need - is to use HTML anchors together with ajax navigation.
When you load something with ajax, modify anchor. This will not reload page, but will give you really new window.location.
When page is loaded without ajax, parse that anchor (if exists) to determine what ajax page to display.
That is basic idea, a direction for your thoughts since I do not know the details of your project implementation.
Loading with AJAX and going to anchor in loaded content
Related
I am developing a one page website and I would like to load in each section when the window scrolls to that specific section. Now I know you can lazyload images but I want to lazy load the entire section. The only way I think it would be possible is if I put my html code into jQuery then load it in when the scroll position is reached. But I would prefer not to do this.
This is a wordpress website and I am loading each page through into the homepage using
<?php require_once(''); ?>
so my page is layed out something like this
<?php get_header(); ?>
<?php require_once('section_one.php'); ?>
<?php require_once('section_two.php'); ?>
<?php get_footer(); ?>
So could I use php to only load these sections in when the scroll position is reached or is there a better way with jQuery? Also I want web crawlers etc to still be able to see my whole page. So if jQuery is disabled I want the full page to show. Any guidance or tutorials on this would be very helpful thanks
Create a method in controller that will render a sub-view based on the section number and return you the HTML. Or in your case create a file that will accept a GET request with section number, and render the output of needed section file as its done in most PHP frameworks (see below). That way you can make AJAX request when scrolling position is of necessary value, and insert returned HTML into the page.
<?php
$section_number = $_GET['section'];
ob_start();
if(file_exists(__DIR__ . 'section_' . $section_number . '.php')) {
include(__DIR__ . 'section_' . $section_number . '.php');
$var=ob_get_contents();
ob_end_clean();
echo $var;
}
echo '';
Render PHP file into string variable
May I suggest that you load the content and hide it with CSS instead? And then make a scroll spy solution to display the content when the section enters viewport? Why force someone to wait while the contents loads?
I am trying to include a specific css file depending on the URL provided by the browser.
My website has the following format:
Header information
php include for content
Footer information
I am doing this so when I change my header menu links, I don't have to change every page on the website.
The php include code is as follows:
<?php
$page = $_GET['page'];
$pages = array('main', 'contact');
if (!empty($page)) {
if(in_array($page,$pages)) {
$page .= '.php';
include($page);
}
else {
include('404.php');
}
}
else {
include('main.php');
}
?>
The URL is made up as follows
index.php?page= for example index.php?page=contact
As part of the php include code, when the page=???? element isn't within the array it loads page 404.php which is an error page.
As can be seen, the array currently contains two pages, main (the home page content) and contact (the contact form). If a user tries to load a page called test (index.php?page=test) my webpage will display the 404 page (however doesn't load index.php?page=404 - the URL in browser stays as index.php?page=test.
What I'm trying to achieve is to load 404.css file when page=test(or any other page name not included in the array) is loaded.
Does anyone know how i'd go about achieving this? I've tried writing an if statement in the header to load 404.css however I was using strstr that fetches the browser URL (so test, not 404), therefore the correct css file doesn't get loaded.
Any help is much appreciated (or a better way to achieve what i'm trying to do).
Thanks in advance
Iain
An easy way of doing this is to make use of the ob_* php functions like bellow:
PHP get all the information from within the ob_start and the ob_get_clean functions and concatenate them all together as a single variable $_page_content, that variable can be used anywhere you need.
<?php
$page = $_GET['page'];
$pages = array('main', 'contact');
if (!empty($page))
{
if(in_array($page, $pages))
{
ob_start('ob_gzhandler');
?>
<link rel="stylesheet" href="css/<?=$page?>.css" type="text/css">
<?
include($page . '.php');
$_page_content = ob_get_clean();
echo $_page_content;
}
else
{
include('404.php');
}
}
else
{
include('main.php');
}
?>
I'm not really sure what the correct term is for this, but I guess it's tabbed URLs or something similar. Anyway, I see it a lot on the web and I'm tying to figure out how the effect is aheived.
A good example are the tabs found when editing your Github profile at
https://github.com/settings/profile
Let's say we have menu with some tabs:
<ul>
<li>tab1</li>
<li>tab2</li>
<li>tab3</li>
</ul>
and then a content div:
<div class="tab-content">
</div>
From what I can see, when the respective tab link is clicked, an ajax request is sent out to fetch the data which then gets inserted into the content area. Which is pretty much standard from what I understand.
However, the part I don't get is how when the url is loaded in a new tab or window, it goes straight to the respective tab.
For example, if you visit:
https://github.com/settings/notifications
you'll go straight to the notifications tab.
I'm trying to implement a similar tab setup with PHP. The ajax part isn't really an issue, I can get that working quite easily. However, getting the url to load the respective tab on a fresh request, is what I'm stuck at.
The way I was thinking of doing it is simply checking the URL and then loading the tab, like this:
<div class="tab-content">
<?php
$URL = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if ($URL = "http://example.com/tabs/tab1") {
echo "this is tab1 content";
}
else if ($URL = "http://example.com/tabs/tab2") {
echo "this is tab2 content";
}
// etc etc
?>
</div>
But if I have 20 tabs, I don't really want to put 20 if/else statements in my page. The only other way I was thinking is placing the URL values in an array, and looping through it. Is there a better way of setting up a tab system like this with PHP?
You can also put your data into database and then query database. But without using database you can also for example put tab content into files and do something like this:
<?php
$URL = $_SERVER['REQUEST_URI'];
$file = 'path_with_content' . DIRECTORY SEPARATOR . str_replace('/' , DIRECTORY_SEPARATOR, $URL).'.php';
if (file_exists($file)) {
echo file_get_contents($file);
}
So you simple create directory path_with_content/tabs and for each tab you create file inside this directory with the same name as last part as last part of your url (tab1, tab2 and so on) with .php extension (so files should have names tab1.php, tab2.php and so on).
you can use an array such as
$navigation = Array('Home' => 'index.php', 'About' => 'about.php');
then if you make an global variable like something as: $curPage
and you load the array
for each loop
and then check the $curPage is looped and make it another font colour
the $curPage will be 'Home' or 'About' then show it with another font or bg colour
this I my paste about it - it's not the best one yet it is working - forgot to put out the numbering xD - http://pastebin.com/H1XcUiiE
TIP: Instead of putting it up in ever page you can create a navigation.php file
and include it into the area of the navigation bar all the time.
I hope someone can help me with this issue. So, I have a large website (1000+ webpages) and I want to put an iframe window on every page. My aim is to make this iframe load specific content according to the parent url address of each page on my website.
For example, when the user on my site loads www.mysite.com/Page-1, I want the iframe window to show ExternalPage-A, or when the user loads www.mysite.com/Page-2, I want the iframe to show ExternalPage-B.
Can someone give me an advise how to do it? Should I make a separate php document that will load in the iframe, where to put all the external links? Then, I should take somehow the parent url and tell the php document which link to be displayed. I am not very familiar with php and that is why I am asking.
Thanks!
Don't use $_SERVER['HTTP_REFERER'] because it won't always contain any data. This is set at the user's end, it cannot be trusted and isn't always set.
You would be better with your referring URL's and corresponding iframe URL's in a database. But putting a database to one side for a moment you could do something like this:
<?php
function get_iframe_url ($page) {
$page = parse_url ($page, PHP_URL_PATH);
/* Substitute the below IF statement for a database lookup
if you have lots of pages */
if ($page == '/news.php') {
return 'http://www.bbc.co.uk/news/';
} elseif ($page == '/contact.php') {
return 'http://www.yellowpages.com/';
}
}
?>
Then you can include this on every page:
<iframe src="<?php echo get_iframe_url ($_SERVER['REQUEST_URI']); ?>"></iframe>
I have many sub pages, there urls like www.domain.com/sub/page1.php www.domain.com/sub/page2.php... Now when I type there url in browser. they all redirect to www.domain.com/sub/index.php, and the current sub page will show in a div dom in the index.php.
My knowledge is very limited. I only know jqeury.load and php header Location. but it is difficult redirect to index.php then tell index.php, which is the current sub-pages then do a jqeury.load.
One more note: Also should think SEO. maybe jqeury.load is bad for a search spider. maybe should use hash url.
So I ask for a help. is there anybody could give me some good suggestion? or simple worked examples?
Thanks.
In every PHP file (e.g page1.php) use this code:
<?php
if(!defined('INCLUDE')) {
header('Location: index.php?site=' . $_SERVER['PHP_SELF']); //Send the user to the index.php page
die();
}
?>
Insert the content you want here...
And in the index.php file:
<?php
define('INCLUDE', true);
if(isset($_GET['site'])) {
$site = $_GET['site'];
} else {
$site = 'page100.php'; //Put the name of the default page if the user visits index.php here
}
?>
<html>
<head><!-- HEAD CONTENT HERE --></head>
<body>
<div><?php
include($site); //Put the page requested into the div
?></div>
</body>
</html>